You need to sign in to do that
Don't have an account?
Help on Trigger
Hi,
I have one scenario where Account has one custom field Email__c(Long Text) and this account has multiple Contacts. Each contact Email field. I want to write a Trigger on Contact when there is new contact comes it's email field also populate in the Account's custom field which is Email__c and all the email of contacts are separated by ; when we delete this contact it's Email also gets deleted from Account's custom field which is Email__c.
Please find enclosed code:-
trigger emailupdate on Contact (after insert, after update, after delete) {
if(trigger.IsAfter && trigger.isInsert)
{
List<Account> li = new list<Account>();
List<Id> ids = new List<Id>();
//String allEmailOfContacts = '';
for(Contact c : trigger.new){
ids.add(c.AccountId);
}
Map< Id, Account> accountMap = new Map<Id, Account>([Select Id,Email__c from Account where Id In:ids]);
for(Contact c: trigger.new)
{
Account a = accountMap.get(c.AccountId);
if(a != null)
{
a.Email__c += c.Email+ ';';
//allEmailOfContacts = allEmailOfContacts + c.Email + ';';
li.add(a);
}
}
update li;
}
}
the above code may also be suitable for onDelete, hopefully the logic makes sense.
Note, the code has not been compiled, only written in a basic text editor.
regards
Andrew
When I am trying the above code I am getting this error.
Could you please help me out to resolve this issue.
Thanks
I have made a look for your trouble in the above code. You are getting the error at line no-23,25,27,32.
At line no-23, there is a need to change a bit. For each contact loop, we have also specified the AccountId of that contact.
I have corrected the code. Please try it once.
Replace your all code with this, I am sure it will work.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com (http://www.ajaydubedi.com )
Hi @Ajay K Dubedi
I am getting this issue:-Method does not exist or incorrect signature: void getKey() from the type Map<Id,Account>
Could you please suggest me how can I resolve this problem.
Thanks