Check if record already exists not to cause duplicates

i want to create a record if it doesn’t exist

 $qBean = BeanFactory::getBean('E14_LOB');
 $beanList = $qBean->retrieve_by_string_fields(
       array(
       'name' => $app_list_strings['lob_list'][$list[$i]],
      'opportunityid_c'=>$bean->opportunityid_c,                                                                                                        

      )
   );
  if($qBean->id==null){
 bean created

i am using this code but still causing duplicate records
so what am i doing wrong

Check out this code:


$qBean = BeanFactory::getBean('E14_LOB');

$searchCriteria = array(
    'name' => $app_list_strings['lob_list'][$list[$i]],
    'opportunityid_c' => $bean->opportunityid_c
);

$qBean = $qBean->retrieve_by_string_fields($searchCriteria);

if (empty($qBean->id)) {
 
    $qBean = BeanFactory::newBean('E14_LOB');
    $qBean->name = $app_list_strings['lob_list'][$list[$i]];
    $qBean->opportunityid_c = $bean->opportunityid_c;
    
    $qBean->save();
}

@hserour you should be looking at the returned $beanList, not at $qBean.

can you please tell me how

this is also creating duplicates

image

If you’re not spending even a minute to think about this and try solutions yourself, how can you ask me to spend several minutes explaining things to you? I have my own work to accomplish, sorry.

Check out this code:

$qBean = BeanFactory::getBean('E14_LOB');

$searchCriteria = array(
    'name' => $app_list_strings['lob_list'][$list[$i]],
    'opportunityid_c' => $bean->opportunityid_c
);


$beanList = $qBean->get_by_string_fields($searchCriteria);

if (empty($beanList)) {

    $qBean = BeanFactory::newBean('E14_LOB');
    $qBean->name = $app_list_strings['lob_list'][$list[$i]];
    $qBean->opportunityid_c = $bean->opportunityid_c;
    
    $qBean->save();
} else {
    $qBean = $beanList[0]; 
}