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
Supraja KallakuriSupraja Kallakuri 

Update Account when a Contact is created

Idea - When a contact is created, check if the associated account's primary_contact_sfid__c field is filled or not. If it is empty, put the contact's id in there. When I test the code, there is a stem assetion failure. Please advise. Thanks!

trigger updateacc on Contact (after insert) {
    
   List<ID> accountids = New List<ID>();
   
  
    for(Contact c : trigger.new){
        if(c.Accountid != null){
            accountids.add(c.AccountId);
        }
        }
     
      List<Account> acclist = [SELECT ID, Primary_Contact_SFID__C FROM Account WHERE ID IN: accountids];  
      List<Account> updateacc = New List<Account>(); 

    
    for(Contact c : trigger.new){
        for(Account acc: acclist){
            if(c.AccountId ==acc.id && c.Account.Primary_Contact_SFID__c == null){
                acc.Primary_Contact_SFID__c =''+c.id;
                updateacc.add(acc);
            }
        }
    }

  
    update (updateacc);
}
    
ManojjenaManojjena
Hi Supraja,
Try with below code it wil help !!
trigger updateacc on Contact (after insert) {
    List<ID> accountids = New List<ID>();
    for(Contact con : trigger.new){
        if(con.Accountid != null){
            accountids.add(con.AccountId);
        }
    }
	if(!accountids.ieEmpty())
    Map<Id,Account> accMap=new Map<Id,Account>( [SELECT ID, Primary_Contact_SFID__C FROM Account WHERE ID IN: accountids AND Primary_Contact_SFID__C==null]); 
    if(!accMap.isEmpty()){
		List<Account> updateacc = New List<Account>(); 
		for(Contact con : trigger.new){
			if(con.AccountId =!=null && accMap != null && accMap.containsKey(con.AccountId)){
				Account acc=new Account(id=con.AccountId,Primary_Contact_SFID__C=con.Id);    
			    updateacc.add(acc);
			}
		}
	}
	if(!updateacc.isEmpty()){
		try{
		  update updateacc;
		}catch(DmlException de){
		  System.debug(de);
		}
	}
}
Let me know if it helps !!
Thanks
Manoj
 
Supraja KallakuriSupraja Kallakuri
@Manoj

Thanks for your quick response. Unfortunately, when I ran the test class, it throws a system assertion error which means the rigger is not updating the record. 

@istest
public class testmerch {
      static testMethod void validatemerch() {    
          
    Account a = New Account();
    a.Name ='BW';
    a.Primary_Contact_SFID__c ='';
    a.Management__c = 'Independent';
    Insert a;          
  
          
    Account b = New Account();
    b.Name ='BW2';
    b.Management__c = 'Independent';
     b.Primary_Contact_SFID__c ='';
    Insert b;          
          
         
          Contact con = New Contact();
          con.LastName = 'test';
          con.Account = a;
          Insert con;         
          
          system.assertequals(a.Primary_Contact_SFID__c, con.id+'');       
          
          Contact con2 = New Contact();
          con2.LastName = 'test2';
          con.Account = a;
          Insert con2;          
          
          system.assertequals(a.Primary_Contact_SFID__C, con.id+'');          
         
 
}
}

Thanks!
ManojjenaManojjena
Hi Supraja,
Modify your code as below it will work .
 
trigger updateacc on Contact (after insert) {
    List<ID> accountids = New List<ID>();
    for(Contact con : trigger.new){
        if(con.Accountid != null){
            accountids.add(con.AccountId);
        }
    }
     Map<Id,Account> accMap;
     List<Account> updateacc = New List<Account>();
    if(!accountids.isEmpty())
        accMap=new Map<Id,Account>( [SELECT ID, Primary_Contact_SFID__C FROM Account WHERE ID IN: accountids AND Primary_Contact_SFID__C=null]); 
    if(!accMap.isEmpty()){
        
        for(Contact con : trigger.new){
            if(con.AccountId !=null && accMap != null && accMap.containsKey(con.AccountId)){
                Account acc=new Account(id=con.AccountId,Primary_Contact_SFID__C=con.Id);    
                updateacc.add(acc);
            }
        }
    }
    if(!updateacc.isEmpty()){
        try{
          update updateacc;
        }catch(DmlException de){
          System.debug(de);
        }
    }
}
Test class
@isTest
public class testmerch {
    static testMethod void validatemerch() {    
        Account acc = New Account();
            acc.Name ='BW';
            acc.Primary_Contact_SFID__c ='';
            acc.Management__c = 'Independent';
            Insert acc;          
        
        System.assertequals(a.Primary_Contact_SFID__c,'');              
        Contact con = New Contact();
            con.LastName = 'test';
            con.AccountId = acc.Id;
            Insert con;         
        System.assertequals(con.LastName,'test');       
        a=[SELECT id,Primary_Contact_SFID__c FROM Account WHERE Id=:a.Id];
        System.assertEquals(a.Primary_Contact_SFID__c,con.Id);
    }
}
Basically In contact the account relationship name is AccountId and we ahould assign account variable .Id to it .

Let me know if it helps !!
Thanks
Manoj