You need to sign in to do that
Don't have an account?
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
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
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
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);
}
}
}
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
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);
}
}
}