AnsweredAssumed Answered

Generate unique ID

Question asked by community-support on Jan 13, 2014
Latest reply on Jan 30, 2014 by Jason Eggers
Hello

Want to generate unique ID for a particular field (opportunity Id) in opportunity module

Can anyone tell me how to do that ?

debug('add_code function fired after save.');

   if(!preg_match($pattern, $bean->fetched_row[self::CODE_FIELD])) {

    $db =  DBManagerFactory::getInstance();

    $query = 'select ' . self::CODE_FIELD . ' from ' . self::CUSTOM_TABLE .

       ' where (' . self::CODE_FIELD . '  '' or ' . self::CODE_FIELD . ' is not null) order by right(' . self::CODE_FIELD . ', 4) desc limit 1';

    $result = $db->query($query, true, 'Error selecting most recent ' . self::CODE_FIELD . ' CODE');

    if ($row=$db->fetchByAssoc($result)) {

     $last_code = $row[self::CODE_FIELD];

    } else {

     //no codes exist, generate default - PREFIX CURRENT_YEAR SEPARATOR FIRST_NUM

     $last_code = self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):'') . self::CODE_SEPARATOR . self::FIRST_NUM . self::SUFFIX;

    }

    $GLOBALS['log']->debug('Last Code: ' . $last_code);

    preg_match($pattern, $last_code, $matches);

    $num=$matches[1];

    $num++;

    $pads = self::ZERO_PADDING - strlen($num);

    $new_code = self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):'') . self::CODE_SEPARATOR;

    //preform the lead padding

    for($i=0; $i debug('New Code: ' . $last_code);

    //write to database

    $query = 'INSERT INTO ' . self::CUSTOM_TABLE . ' (id_c, ' . self::CODE_FIELD . ') VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE ' . self::CODE_FIELD . '='$new_code'';

    $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');

   } else {

    $GLOBALS['log']->debug('Code Exists...Exiting ' . $bean->fetched_row[' . self::CODE_FIELD . ']); 

   }

  }

}

?>

I am working on this code but it doen not show its result

(from mahichhabra)

Outcomes