0

Hey i m very new to apex

BMI__c and CMI__c are two custom objects

I need to poplulate Id of a record from CMI__c to lookup Field of BMI__c in a batch apex class where bmlist is list of all records of BM Object and CMlist is the list of all records of CM OBject

i used this nested list loop ,could you please

for(BMI__c bm:bmlist)
{

for(CMI_c cm:cmlist)

{  if(bm.id==cm.LookupFieldsValue__c)

{
bm.cmLookUpfield__c=bm.id;
}
}
}
update bmlist

1
  • I think using lookup field value from 'bm' should be compared with 'id' field of 'cm' . And then update the bmlist . Commented Apr 27, 2023 at 11:33

1 Answer 1

1

Whenever you have a for-for-if loop, you should be using a Map object:

Map<Id, BMI__c> bmiMap = new Map<Id, BMI__c>(bmList);
for(CMI__c cm: cmList) {
  BMI__c bmiRecord = bmiMap.get(cm.LookupFieldsValue__c);
  if(bmiRecord != null) {
    bmiRecord.cmLookupField__c=bmiRecord.Id;
  }
}
update bmList;

However, it looks like this isn't strictly what you're trying to do. I believe you meant to assign the CMI to the BMI, as follows:

   bmiRecord.cmLookupField__c=cm.Id;

Thus linking the records in a 1:1 relationship.

1
  • yes doing the same but this is an batch classs in which i am creating new cm__c object records with mapping of all the fields of bm__c object and after creation of the cm__c records after ...then the above code comes into play
    – saurabh
    Commented Apr 27, 2023 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .