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
SFDC GuestSFDC Guest 

Apex trigger to update child field value if it is null

Hi, I am updating parent accountnumber in child object contact. But i want to update the field contact - accountnumber field only when accountnumber in contact is null. if we provide any new value in accountnumber of contact field, then new value should be updated not parent account-accountnumber.
Below is class which will update accountnumber of Account in contact even we are providing other value in Contact object.
I mean if we are providing any value in contact-accountnumber field, then new should be updated, it should not override with parent account-accountnumber. Please help.


public class accountnamepopualtion
{
public static void popualteacc(List<Contact> coonlist)
{
set<Id> accountids= new set<Id>();
Map<id,string> accmap= new Map<id,string>();
for(contact con:coonlist)
{
 
accountids.add(con.accountid);
}
for(Account acc:[select id,accountnumber from account where id in:accountids])
{
accmap.put(acc.id,acc.accountnumber );
 
}
for(contact con:coonlist)
{
if(accmap.containskey(con.accountid) && con.Account_Number__c==null)
{
con.Account_Number__c=accmap.get(con.accountid);
}
}
}
}


trigger contactTrigger on contact(before insert, before update, after insert, after update )
{
 if (Trigger.isBefore)
      {
            if (Trigger.isInsert  )
            {
              accountnamepopualtion.setAccountName(trigger.new);
             }

}
}

Thanks in advance
Best Answer chosen by SFDC Guest
Srinivasa Chary TaduriSrinivasa Chary Taduri
Added code below.

public class accountnamepopualtion
{
    public static void popualteacc(List<Contact> coonlist)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlist)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(contact con:coonlist)
        {
            if(accmap.containskey(con.accountid) && con.Account_Number__c==null)
            {
                con.Account_Number__c=accmap.get(con.accountid);
            }
        }
    }
    
    
    public static void popualteaccUpdate(List<Contact> coonlistNew, List<Contact> coonlistOld)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlistNew)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(Integer i=0; i<coonlistNew.size(); i++)
        {
            if(accmap.containskey(coonlistNew[i].accountid) && (coonlistNew[i].Account_Number__c==null || coonlistNew[i].Account_Number__c != coonlistOld[i].Account_Number__c))
            {
                coonlistNew[i].Account_Number__c=accmap.get(coonlistNew[i].accountid);
            }
        }
    }
    
}


trigger contactTrigger on contact(before insert, before update, after insert, after update )
{
    if (Trigger.isBefore)
    {
        if (Trigger.isInsert  )
        {
            accountnamepopualtion.popualteacc(trigger.new);
        }
        
        if(Trigger.isUpdate)
        {
            accountnamepopualtion.popualteaccUpdate(trigger.new, trigger.Old);
        }
    }
    
}

All Answers

Srinivasa Chary TaduriSrinivasa Chary Taduri
Added code below.

public class accountnamepopualtion
{
    public static void popualteacc(List<Contact> coonlist)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlist)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(contact con:coonlist)
        {
            if(accmap.containskey(con.accountid) && con.Account_Number__c==null)
            {
                con.Account_Number__c=accmap.get(con.accountid);
            }
        }
    }
    
    
    public static void popualteaccUpdate(List<Contact> coonlistNew, List<Contact> coonlistOld)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlistNew)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(Integer i=0; i<coonlistNew.size(); i++)
        {
            if(accmap.containskey(coonlistNew[i].accountid) && (coonlistNew[i].Account_Number__c==null || coonlistNew[i].Account_Number__c != coonlistOld[i].Account_Number__c))
            {
                coonlistNew[i].Account_Number__c=accmap.get(coonlistNew[i].accountid);
            }
        }
    }
    
}


trigger contactTrigger on contact(before insert, before update, after insert, after update )
{
    if (Trigger.isBefore)
    {
        if (Trigger.isInsert  )
        {
            accountnamepopualtion.popualteacc(trigger.new);
        }
        
        if(Trigger.isUpdate)
        {
            accountnamepopualtion.popualteaccUpdate(trigger.new, trigger.Old);
        }
    }
    
}
This was selected as the best answer
SFDC GuestSFDC Guest
Hi Srinivas,
Thanks for reply.
I tried above code, it's updating parent account number when child accountNumber is empty. tnat's fine. but when i give some other value in child contact -accountnumber, it should populate the given new value. this is not working. overriding with parent accountNumber. Plz  provide solution
SFDC GuestSFDC Guest
plz. it's urgent.
Srinivasa Chary TaduriSrinivasa Chary Taduri
Updated the code.

public class accountnamepopualtion
{
    public static void popualteacc(List<Contact> coonlist)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlist)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(contact con:coonlist)
        {
            if(accmap.containskey(con.accountid) && con.Account_Number__c==null)
            {
                con.Account_Number__c=accmap.get(con.accountid);
            }
        }
    }
    
    
    public static void popualteaccUpdate(List<Contact> coonlistNew, List<Contact> coonlistOld)
    {
        set<Id> accountids= new set<Id>();
        Map<id,string> accmap= new Map<id,string>();
        for(contact con:coonlistNew)
        {
            accountids.add(con.accountid);
        }
        for(Account acc:[select id,accountnumber from account where id in:accountids])
        {
            accmap.put(acc.id,acc.accountnumber ); 
        }
        for(Integer i=0; i<coonlistNew.size(); i++)
        {
            if(accmap.containskey(coonlistNew[i].accountid) && (coonlistNew[i].Account_Number__c==null || coonlistNew[i].Account_Number__c != coonlistOld[i].Account_Number__c))
            {
                coonlistNew[i].Account_Number__c=coonlistNew[i].Account_Number__c;
            }
        }
    }
    
}


trigger contactTrigger on contact(before insert, before update, after insert, after update )
{
    if (Trigger.isBefore)
    {
        if (Trigger.isInsert  )
        {
            accountnamepopualtion.popualteacc(trigger.new);
        }
        
        if(Trigger.isUpdate)
        {
            accountnamepopualtion.popualteaccUpdate(trigger.new, trigger.Old);
        }
    }
    
}
SFDC GuestSFDC Guest
Still not working srini. it's updating parent value not the given value.
Srinivasa Chary TaduriSrinivasa Chary Taduri
Please check if other code is trying to update the same.
SFDC GuestSFDC Guest
Thank You so much Srinivas for your valuable. Greatly appreciated. It's working in both scenarios.