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
NiknitNiknit 

I am not able to code a trigger to update a field in contact with the value from field of account?

Hi, I am new to this.
what i am trying to do is make a trigger in which acc_number__c is updated in Contact from the account field acc_number__c if the checkbox field final_number__c is selected in the contact object.
I was confused as to use after or before for this.
i tried with after as follows :
trigger Accnumberupdate2 on contact (after insert ) {
            
            list<contact> conlist = new list<contact>();
            list<contact> con = new list<contact>();
           Set <id> ids = new set<id>();
       for(contact c : trigger.new){
              if(c.Finalize_Number__c == true){
                system.debug('If condition');
                ids.add(c.Id);
                              }
       }
            con = [select acc_number__c,finalize_number__c,account.acc_number__c from contact where id
                    in :ids];
           
                
        for(contact c : con){
               
                c.Acc_Number__c = c.account.acc_number__c;
                conlist.add(c);

        
             }
    
    
 update conlist;   

}

the above code worked.
but when i use the below code with before, it doesnt work, the field doesnt get updated.
trigger Accnumberupdate3 on contact (before insert ,before update) {
          
    acc_number__c,finalize_number__c,account.acc_number__c from contact where id in :ids];
                
        for(contact c : trigger.new){
                system.debug('for condition'+ c.Acc_Number__c);
            if(c.Finalize_Number__c == true){
             c.Acc_Number__c = c.account.acc_number__c;
         
                    }
        
               }
    
    
    

}


I read that i should be using after if i need to update a related object but before if its the same object, since i am trying to uopdate the same object i think before should be done but i may be wrong.
If anyone can please tell what is the problem with before trigger and why is it not working ,that would be great because i feel logically it is fine.I think i need to understand when to use before and after and if someone could make me understand with this example ill be able to understand it..
Thanks
Best Answer chosen by Niknit
Maharajan CMaharajan C
Hi Nitin,

You can't get the record Id before it inserted.Here you are trying to get the record Id during before the contact created .Only you can do after the record creation so it works fine for after event in trigger.

Here you have to use the Account Ids Like below in Before event!!!

trigger UpdateChildbasedonParent on Contact (before insert,before update)
{
set<id> AccIds=new set<id>();
for(contact con:trigger.new)
{
AccIds.add(con.AccountId);
}
map<Id,Account> RelatedAccounts = new map<Id,Account>([SELECT Id, Name,acc_number__c FROM Account WHERE Id IN :AccIds]);
for(contact con:trigger.new)
{
Account Related=RelatedAccounts.get(con.AccountId);
If(con.Finalize_Number__c == true)
con.Acc_Number__c=Related.acc_number__c;
}


Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj

All Answers

Maharajan CMaharajan C
Hi Nitin,

You can't get the record Id before it inserted.Here you are trying to get the record Id during before the contact created .Only you can do after the record creation so it works fine for after event in trigger.

Here you have to use the Account Ids Like below in Before event!!!

trigger UpdateChildbasedonParent on Contact (before insert,before update)
{
set<id> AccIds=new set<id>();
for(contact con:trigger.new)
{
AccIds.add(con.AccountId);
}
map<Id,Account> RelatedAccounts = new map<Id,Account>([SELECT Id, Name,acc_number__c FROM Account WHERE Id IN :AccIds]);
for(contact con:trigger.new)
{
Account Related=RelatedAccounts.get(con.AccountId);
If(con.Finalize_Number__c == true)
con.Acc_Number__c=Related.acc_number__c;
}


Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
This was selected as the best answer
Maharajan CMaharajan C
Can you please Let me know did you try this and if it works or not!!!

If it works don't forget to mark the best answer which will help others!!!

Thanks,
​Raj