+ Start a Discussion
Unnamalai SubramanianUnnamalai Subramanian 

Update Opportunity Description as Opportunity Owner email Address

Hi,

I am trying to update the Old Account's Opportunity description field as Opportunity Owner's email address, for all the contacts whose Account Id is updated. Here is the code I tried. When I tried hard coding description as account Id it worked. But how to get opportunity owner's email address?

trigger accountchange on Contact (after insert, after update) {
  //This queries all Accounts related to the incoming Contact records in a single SOQL query.                                      
  // List<Contact> contactwithAccounts = [select id, firstname, AccountId from Contact where Id IN :Trigger.newMap.keySet()];

  Map<Id, Opportunity> OppToUpdate = new Map<Id,Opportunity>();
  Map<Id,Contact> Mp = new Map<Id,Contact>(); // Store Account Id and contact object
    //indexed for loop
    for(integer i=0; i<trigger.new.size(); i++){
        if(trigger.new[i].AccountId != trigger.old[i].AccountId){  
            System.debug('trigger.old[i].AccountId : '+trigger.old[i].AccountId + ':' + trigger.old[i].Account);
            Mp.put(trigger.old[i].AccountId,trigger.new[i]);   // store account Id and contact object                          
      }                   
    }
    // Update Opportunity of old account with description as opty owner email id
    List<Id> OId = new List<Id>();
    for(Opportunity opp: [Select Id, Description, OwnerId from Opportunity where AccountId =:Mp.Keyset()]){
            opp.Description = '----Added through Trigger - Opty Owner Id' + opp.OwnerId + '--Contact Owner Id';
            OId.add(opp.OwnerId);     // get Opportunity owner id in OId List.       
            OppToUpdate.put(opp.Id, opp);        
    }
    
    Map<Id,User> us1 = new Map<Id,User>([SELECT Id,Email FROM User where id =: OId]); 
    // Throws compilation error as line 27:16 no viable alternative at character'' for above line.
    for (Id userId : us1.Keyset()){
        for(Opportunity op : OppToUpdate){
            if(userId == OppToUpdate.get(op).OwnerId){
                op.Description = us1.get(userId).Email;
            }
        }
    }    
    
    //Now outside the FOR Loop, perform a single Update DML statement. 
   update OppToUpdate.values();
}
Best Answer chosen by Unnamalai Subramanian
SaranSaran
Hi

Below is the modifed code that updates the opportunities description field based on the modified account.
 
trigger accountchange on Contact (after insert, after update) {
	
	list<opportunity> oppList = new list<opportunity>();
	set<id> accIdSet = new set<id>();
	
	for(contact newCon : trigger.new)
	{
		if(trigger.isUpdate && trigger.isAfter)
		{
			Contact oldCon = trigger.newMap.get(newCon.Id);
			if(newCon.AccountId != oldCon.AccountId)
				accIdSet.add(newCon.AccountId);  // use this line if you want to update newAccounts opportunity description
				accIdSet.add(oldCon.AccountId); // use this line if you want to update oldAccounts opportunity description
		}
	}

	for(opportunity opp : [select id, Owner.Email, description, AccountId from opportunity where AccountId IN: accIdSet])
	{
		opp.Description = opp.owner.Email;
		oppList.add(opp);
	}

	if(oppList.size() > 0)
		update oppList;
}

Hope this might help you to solve the issue.

Thanks.

All Answers

SaranSaran
Hi

Below is the modifed code that updates the opportunities description field based on the modified account.
 
trigger accountchange on Contact (after insert, after update) {
	
	list<opportunity> oppList = new list<opportunity>();
	set<id> accIdSet = new set<id>();
	
	for(contact newCon : trigger.new)
	{
		if(trigger.isUpdate && trigger.isAfter)
		{
			Contact oldCon = trigger.newMap.get(newCon.Id);
			if(newCon.AccountId != oldCon.AccountId)
				accIdSet.add(newCon.AccountId);  // use this line if you want to update newAccounts opportunity description
				accIdSet.add(oldCon.AccountId); // use this line if you want to update oldAccounts opportunity description
		}
	}

	for(opportunity opp : [select id, Owner.Email, description, AccountId from opportunity where AccountId IN: accIdSet])
	{
		opp.Description = opp.owner.Email;
		oppList.add(opp);
	}

	if(oppList.size() > 0)
		update oppList;
}

Hope this might help you to solve the issue.

Thanks.
This was selected as the best answer
Unnamalai SubramanianUnnamalai Subramanian
Thank you! It worked! Now, when to use Map and List? I was trying to get the Owner's email address through separate SOQL query and was not sure how to assign it to Map and iterating it in for loop. 

You made it simple, by accessing this email field directly from Owner :)

Many Thanks,
Unna