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 

Why is this before trigger not working?please help

trigger Accnumberupdate3 on contact (before insert ,before update) {
          
               
            list<contact> con = new list<contact>();
             Set <id> ids = new set<id>();
       
			  for( contact c : trigger.new){
			  if(c.Finalize_Number__c == true){
               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){
                system.debug('for condition');
            if(c.Finalize_Number__c == true){
               
                c.Acc_Number__c = c.account.acc_number__c;
        
                    }
        
             }
    
    
    

}


OR

 

trigger Accnumberupdate3 on contact (before insert ,before update) {
          
               
            
       
			  for( contact c : trigger.new){
			 
                system.debug('for condition');
            if(c.Finalize_Number__c == true){
               
                c.Acc_Number__c = c.account.acc_number__c;
        
                    }
        
             }
    
    
    

}
 







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.
the above both before trigger seems fine logically to me?am i missing something?should i be using after trigger?I tried with after trigger and  it works,what is the problem with this before triggers?

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

Akshay_DhimanAkshay_Dhiman
Hi Nitin,
It's not working on before trigger because , what you are  doing in the above trigger is trying to add the contact Id in the set of Id in line number 9.
 
ids.add(c.Id);}


It might be working on Update case but it won’t be working on Insert because the trigger is on before Insert and it is fired even before the Contact record is inserted, and when the record is not inserted the Id of the Contact record is not generated , so your List of Contact at Line number 11
con = [select acc_number__c,finalize_number__c,account.acc_number__c from contact where id in :ids];



It would be empty because there will be no such records of that particular Id and it will show you no output.
And its working on after trigger because the trigger is been fried after the record is been inserted , and when it is inserted it will generate an Id and it would be having an Id and your Contact list will definitely get the record and you can see the result.

Regards ,
Akshay
Please mark my answer as a solution if it was helpful
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
NiknitNiknit

Thanks guys i had doubts about that, its cleared now, but i had posted a second example i made as

trigger Accnumberupdate3 on contact (before insert ,before update) {
          
               
            
       
			  for( contact c : trigger.new){
			 
                system.debug('for condition');
            if(c.Finalize_Number__c == true){
               
                c.Acc_Number__c = c.account.acc_number__c;
        
                    }
        
             }
    
    
    

}

i made this before as i had doubt about ids, so i had made without ids,but why isnt this code working cos i didnt use any ids. so it should be working right?

alsi@raj, could you please explaining your code example, like what does this do AccIds.add(con.AccountId); and why we using maps - map<Id,Account> RelatedAccounts = new map<Id,Account>([SELECT Id, Name,acc_number__c FROM Account WHERE Id IN :AccIds]); instead of list, i have never used maps, also Account Related=RelatedAccounts.get(con.AccountId); .

 

Thanks

Maharajan CMaharajan C
Hi Nitin,

Can you please let me know if my trigger work for you? So it will make me happy!!!

trigger UpdateChildbasedonParent on Contact (before insert,before update)
{
set<id> AccIds=new set<id>();    // To Store the Account IDs from the contacts
for(contact con:trigger.new)
{
AccIds.add(con.AccountId);        // Stored the Account IDs from the contacts in the set object
}
map<Id,Account> RelatedAccounts = new map<Id,Account>([SELECT Id, Name,acc_number__c FROM Account WHERE Id IN :AccIds]); // It 
Query the Account details from the salesforce for only the Stored AccIds in set object
for(contact con:trigger.new)
{
Account Related=RelatedAccounts.get(con.AccountId);  // Get the values and assigned in to Related variable from the map object which have the details for Account in the Contact you insert or update.
If(con.Finalize_Number__c == true)
con.Acc_Number__c=Related.acc_number__c;  //Assign the Account value to contact
}


If you use a List, you'll get a List collection that consists of queried SObjects which you used in the "FROM" clause.

If you use a Map, you'll get a Map collection that consists of queried SObjects IDs as the key set and SObjects as the value set. In other words, the map elements will be "ID => SObject"

Using a map is generally more benefical for the developer, because:
You can obtain the queried records' ID collection (as a Set) from map by: myMap.keySet();
You can obtain the SObject list from map by: myMap.values();

x-----------------x
I tried your codin it saves fine but wont update anything

c.Acc_Number__c = c.account.acc_number__c; I thing You can't use like this directly in Coding first you have to query the acc_number__c from Account in code then it give proper o/p.

If it helps to you mark this as a best answer!!!

Thanks,
​Raj
 
NiknitNiknit

Hi Raj,

Your code works fine.

understanding this code will help me good, so just trying to understand to the very last to close this discussion.

i dont know Accountid, in AccIds.add(con.AccountId); , should it be con.account.id to access the ids of accounts related?

accountid seems like a field in contact.is it?is there similarly contactid,opportunityid as well?

 

I understand the later part though, thanks.

Maharajan CMaharajan C
con.account.id is not a correct one.

Simply i will Explain to you here i added the new or Exis contact->Account Id to the Set Object then compare this with map object which have the query(details of Account record) based on Set Object Account Ids so we can assign the parent object record values to child records.

If it helps to you mark this as a best answer!!!

Thanks,
​Raj
Maharajan CMaharajan C
Can i have any update?
NiknitNiknit

Thanks Maharaja C,sorry for the late reply.

i think i understand a bit.

so if i use con.oppourtunityid will I get ids of opportunity related to contacts?

also i you could explaing your statement above "Simply i will Explain to you here i added the new or Exis contact->Account Id to the Set Object then compare this with map object which have the query(details of Account record) based on Set Object Account Ids so we can assign the parent object record values to child records."

i didnt understand it quite clearly and its bugging me.

Thanks again.