function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajar KumarRajar Kumar 

update records in the list with other list values

Hi Guys,

I have two list opps(opprtunity list) and ocList(OpportunityContactRole ) . I want to update one string field on opportunity(records in opps) with the value of contact name of contact role(records in.ocList)  for example opps[0].test__c = ocList [0].Contact.Name
How can I achieve for all the records?
Please help
Thanks
List<Opportunity> opps = new List<Opportunity>();
        for (integer i=0; i<50; i++)
		{
            Opportunity opp = new Opportunity();
            opp.AccountId=acc.Id;
            opp.Name='My Opportunity'+i;
            opp.StageName='Prospecting';
            opp.CloseDate=date.today().adddays(i);
            opp.Open_Tasks__c = 0;
            //opp.LastActivityDate = System.today()+15;
            opps.add(opp);
        }
  insert opps
List<OpportunityContactRole> ocList = new List<OpportunityContactRole>();
for(integer i=0; i<opps.size(); i++){
      OpportunityContactRole ocr = new OpportunityContactRole();
      ocr.ContactId = con.Id;
      ocr.OpportunityId = oppList[i].Id;
      ocr.IsPrimary = TRUE;
      ocr.Role = 'Decision Maker';
      ocList.add(ocr);
      } 
insert ocList;

 
Raj VakatiRaj Vakati
Try like this  .. Remove hardcoded ID from this example 
 
List<Opportunity> opps = new List<Opportunity>();
for (integer i=0; i<50; i++)
{
    Opportunity opp = new Opportunity();
    opp.AccountId='0014600001UDnJO';
    opp.Name='My Opportunity'+i;
    opp.StageName='Prospecting';
    opp.CloseDate=date.today().adddays(i);
    //  opp.Open_Tasks__c = 0;
    //opp.LastActivityDate = System.today()+15;
    opps.add(opp);
}
insert opps;
List<OpportunityContactRole> ocList = new List<OpportunityContactRole>();
for(integer i=0; i<opps.size(); i++){
    OpportunityContactRole ocr = new OpportunityContactRole();
    ocr.ContactId = '0034600001P8c7z';
    ocr.OpportunityId = opps[i].Id;
    ocr.IsPrimary = TRUE;
    ocr.Role = 'Decision Maker';
    ocList.add(ocr);
} 
insert ocList;



for( integer i=0; i<opps.size();i++){
    
    opps[i].Description  = ocList[i].Contact.Name;
}


update opps ;