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
Salesforce####Salesforce#### 

Need to Re write the below simple trigger using "MAPS "

currently below code is working , i want to re write the same in maps !!! 

//I'd like to update a boolean field  " CheckMate__c " on contact to True when a  specific opportunity stage is closed lost. 

trigger ClosedLost on Opportunity (after insert ,after update)
{
list<contact> cont = new list<contact>();

for(opportunity opp : trigger.new)
 {
     if(opp.stagename == 'Closed Lost')
     {
     cont = [select id , name , CheckMate__c from contact where Accountid =:opp.accountid ];
     
     for(contact  c :cont)
      {
     c.CheckMate__c = true ;
       }
      update cont; 
     }
  
 }

}
Best Answer chosen by Salesforce####
Nayana KNayana K
trigger ClosedLost on Opportunity (after insert ,after update)
{
	list<contact> cont = new list<contact>();
	Set<Id> setAccId = new Set<Id>();
	for(opportunity opp : trigger.new)
	{
		if(Trigger.isAfter)
		{
			if(opp.stagename == 'Closed Lost' && opp.AccountId != null && (Trigger.isInsert || (Trigger.isUpdate && opp.stagename != 		Trigger.oldMap.get(opp.Id).stagename)))
			{
				setAccId.add(opp.AccountId);
			}
		}
	}
	
	if(!setAccId.isEmpty())
	for(Contact objContact : [SELECT Id, CheckMate__c FROM Contact WHERE AccountId IN: setAccId AND CheckMate__c  = FALSE])
	{
		objContact.CheckMate__c = TRUE;
		cont.add(objContact);
	}
	
	if(!cont.isEmpty())
		update cont;
}

Though I have not used map (used Set<Id>) , above code is optimized.
 

All Answers

Nayana KNayana K
trigger ClosedLost on Opportunity (after insert ,after update)
{
	list<contact> cont = new list<contact>();
	Set<Id> setAccId = new Set<Id>();
	for(opportunity opp : trigger.new)
	{
		if(Trigger.isAfter)
		{
			if(opp.stagename == 'Closed Lost' && opp.AccountId != null && (Trigger.isInsert || (Trigger.isUpdate && opp.stagename != 		Trigger.oldMap.get(opp.Id).stagename)))
			{
				setAccId.add(opp.AccountId);
			}
		}
	}
	
	if(!setAccId.isEmpty())
	for(Contact objContact : [SELECT Id, CheckMate__c FROM Contact WHERE AccountId IN: setAccId AND CheckMate__c  = FALSE])
	{
		objContact.CheckMate__c = TRUE;
		cont.add(objContact);
	}
	
	if(!cont.isEmpty())
		update cont;
}

Though I have not used map (used Set<Id>) , above code is optimized.
 
This was selected as the best answer
EldonEldon
Hi,

If you want to bulkify your code use the below trigger.
 
trigger DevComOpp1 on Opportunity (after insert,after update) {
    
    list<contact> cont = new list<contact>();
    list<id> OppIds = new list<id>();
                
    for(opportunity opp : trigger.new)
    {
        if(opp.stagename == 'Closed Lost')
        {
            OppIds.add(opp.accountid);
        }
    }
    list <contact> UpdatedCont = new list<contact>();
    cont = [select id , name,CheckMate__c from contact where Accountid in :OppIds];
    for(contact c : cont){
        contact TempCont = c;
        TempCont.CheckMate__c  = 1;
        UpdatedCont.add(TempCont);
    }
   update UpdatedCont;
	
}


Let me know if you have any issues.
Mark it as best answer if it works.

Regards