You need to sign in to do that
Don't have an account?
Esti Leiser
how to access the accountname from the contact (in a trigger)
Hi! I'm working on a trigger, and hit an issue that I can't figure out.
I have a map of String,String, where the first string refers to a contact name, and the second one refers to the account name of that contact. I am populating the map inside a loop, where the variable I have to work with is c - referring to a contact.
How do I get the account name from the contact, to add it to the map?
Here's my code so far:
Map<String,String> accountsMap = new Map<String,String> (); for (Contact c : existingContacts){ accountsMap.put(c.FirstName + ' ' + c.LastName, c.AccountName); }
but c.AccountName does not seem to exist.
Any ideas? Thanks!
You need to query first to fetch all Account Names related to those contacts, would be some thing like this:
Set<Id>AccountIdset=new Set<id>();
for(contact c:existingContacts){
if(c.AccountId!=null)
AccountIdset.add(c.AccountId);
}
Map<Id,Account>AccountInfoMap=new Map<Id,Account>([select id,Name from Account where id in : AccountIdset]);
Map<String,String> accountsMap = new Map<String,String> ();
for (Contact c : existingContacts){
accountsMap.put(c.FirstName + ' ' + c.LastName,AccountInfoMap.get(c.AccountId).Name);
}
Hope this helps:
Thanks,
balaji
All Answers
You need to query first to fetch all Account Names related to those contacts, would be some thing like this:
Set<Id>AccountIdset=new Set<id>();
for(contact c:existingContacts){
if(c.AccountId!=null)
AccountIdset.add(c.AccountId);
}
Map<Id,Account>AccountInfoMap=new Map<Id,Account>([select id,Name from Account where id in : AccountIdset]);
Map<String,String> accountsMap = new Map<String,String> ();
for (Contact c : existingContacts){
accountsMap.put(c.FirstName + ' ' + c.LastName,AccountInfoMap.get(c.AccountId).Name);
}
Hope this helps:
Thanks,
balaji
Here's my trigger: and my test: Any idea why?
Thanks,
balaji
Would posting the debug log help?
I updated the test so that it runs SOQL to pull up the new lead, and used that lead for the assert, and IT PASSED!
Thanks for your help!