You need to sign in to do that
Don't have an account?

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();
}
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();
}
Below is the modifed code that updates the opportunities description field based on the modified account.
Hope this might help you to solve the issue.
Thanks.
All Answers
Below is the modifed code that updates the opportunities description field based on the modified account.
Hope this might help you to solve the issue.
Thanks.
You made it simple, by accessing this email field directly from Owner :)
Many Thanks,
Unna