0

I have text field on contact object FFCN_Account_Name__c, i want to update standard Account name look up field on contact with this text field, but when contacts are getting inserted it's only updating one record, how to update all records one time

trigger FFCNAccountName_Contact on Contact (before insert, before update ){

    Map<String, Contact> ConMap = new Map<String, Contact>();

    for(Contact con:Trigger.new)
    {
        ConMap.put(con.FFCN_Account_Name__c, con);

    }

    List<Account> accLst = [SELECT ID, Name FROM Account where Name in :ConMap.keySet()];

    for(Account acct : accLst){
        ConMap.get(acct.Name).AccountId = acct.ID;
    }
}
4
  • Please explain your problem. What are you trying to do? A trigger will work on current record at a time(just for one record). Or for multiple records in bulk operation. Commented Sep 6, 2017 at 6:24
  • @SaumyaGaikwad Thanks for responding, how to add bulk operations? I get account and related contacts from other instance through connections but through this trigger only one contact is getting related and rest are not, iam new to apex so not sure how to add bulk opertions
    – Admin_BA
    Commented Sep 6, 2017 at 6:29
  • Is the FFCN_Account_Name__c field is text type? another concern is whether the FFCN_Account_Name__c field value is unique? because maps eliminate duplicate values Commented Sep 6, 2017 at 6:33
  • It's text field that has account name and through trigger i want to update the standard account name lookup field with this field
    – Admin_BA
    Commented Sep 6, 2017 at 6:35

1 Answer 1

2

What you were skipping here is the second loop inside the Account loop.

for(Account acct : accLst){
        for(Contact co : Trigger.new){
            if(co.FFCN_Account_Name__c == acct.Name)
                co.accountid = acct.id;
        }        
    }

You must iterate through the accounts as well as the contacts list you need to manipulate.

The working solution :

trigger FFCNAccountName_Contact on Contact (before insert, before update ){

    Set<String> conaccounts = new Set<String>();

    for(Contact con:Trigger.new)
    {
        conaccounts.add(con.FFCN_Account_Name__c);
    }

    List<Account> accLst = [SELECT ID, Name FROM Account where Name in :conaccounts];
    System.debug(accLst);
    for(Account acct : accLst){
        for(Contact co : Trigger.new){
            if(co.FFCN_Account_Name__c == acct.Name)
                co.accountid = acct.id;
        }        
    }

}

And the test class

@istest public class FFCNAccountName {
    static testmethod void myTest() 
    { 
        Account acc = new Account(Name = 'test', phone = '1234567890'); 
        insert acc; 
        List<Contact> cnt =  new Contact[]{ 
            new Contact(LastName = 'Test Name1' , FFCN_Account_Name__c = acc.Name), 
                new Contact(LastName = 'Test Name2' , FFCN_Account_Name__c = acc.Name)};

        insert cnt; 
    } 
}
2
  • Thank you, that worked. Is it hard to implement test class for this?
    – Admin_BA
    Commented Sep 6, 2017 at 7:06
  • Not at all. Just create multiple contacts and insert them in one go! Commented Sep 6, 2017 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .