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
Esti LeiserEsti 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!

Best Answer chosen by Esti Leiser
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Esti Leiser:

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

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Esti Leiser:

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
This was selected as the best answer
Esti LeiserEsti Leiser
Thanks! I updated my code to query the account name as well. For some reason, my test is still not passing, and I can't figure out why!
Here's my trigger:
trigger ChangeLeadCompanyForDups on Lead (before insert) {
   
   	List<String> leadStrings = new List<String> ();
    for(Lead record:Trigger.new) {
        if (record.Company == 'unknown'){
            leadStrings.add(record.FirstName + ' ' + record.LastName);
        }
    }
      
	List<Contact> existingContacts = [SELECT id, FirstName, LastName, Account.Name FROM Contact WHERE Name IN :leadStrings AND AccountNameIsSame__c != True];

    Map<String,String> accountsMap = new Map<String,String> ();
    for (Contact c : existingContacts){
        accountsMap.put(c.FirstName + ' ' + c.LastName, c.Account.Name);
        system.debug('>>> added to accountsMap: '+ c.FirstName + ' ' + c.LastName + ' with the account name of ' + c.Account.Name);
    }
    
    String fullName = '';
    
    for (Lead l : Trigger.new) {
    	fullName = l.FirstName + ' ' + l.LastName;  
        if(accountsMap.keyset().contains(fullName)) {
            system.debug('>>> option in list, fullName: '+fullName+ ' with the account of ' + accountsMap.get(fullName));
            l.Company = accountsMap.get(fullName);
        }
    }
}
and my test:
@isTest
public class TestChangeLeadCompanyForDups {

    @isTest
  static void testLeadInsert(){
	
    test.startTest();
    Account A1 = new Account(Name = 'Test Account');
    insert A1;
    
      system.debug('>>> Account: ' + A1);

    Contact C1 = new Contact(LastName='Tester',FirstName ='First', AccountID=A1.ID, Email='tester@nowhere.com');
    insert C1;
      
      system.debug('>>> Contact: ' + C1);
         
    Lead L1 = new Lead(LastName='Tester',FirstName ='First', Company='unknown', Email='tester@nowhere.com');
    insert L1;
      
      
      system.debug('>>> Lead: ' + L1);
      
    system.AssertEquals(A1.Name,L1.Company);
    test.stopTest();
    } 

}
Any idea why?

 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
is it failing to assert or any where else?? can you add a debug log and see whats happening??

Thanks,
balaji
Esti LeiserEsti Leiser
It's failing to assert. The debug log seems to indicate that everything is going according to plan...

Would posting the debug log help?
Esti LeiserEsti Leiser
Okay, I think I got it! The problem was in the test, not in the trigger. I was testing the value of the lead that I then inserted, but not of the lead after it was inserted. (I don't totally follow the issue, to be honest!)

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!