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
RDN_LHRRDN_LHR 

Why does this work "before update" but only half work "before insert?"

This trigger adds a few values to an account from its parent account.  When there is no ParentId, it should put the information about itself in the same fields.
 
When updating an Account, this works as it should -- if ParentId == null, do one thing, otherwise do the other thing.  When inserting an account, it only works if there is a ParentId.  If ParentId == null, it just does nothing.
 
Why would it work differently on insert than update?  If it works differently, why does the "else" portion still evaluate properly and work as expected, but do nothing when evaluating the "if" portion?
 
 
trigger accountSetUltimateParent on Account (before insert,  before update) {
// Sets custom account fields by fetching values from the parent account


Set<Id>  ParAccSet = new Set<Id>();

 for ( Account acct : Trigger.new ) 
  {
  ParAccSet.add(acct.ParentId);
  }
 

Map<Id, Account> ParAccMap = New Map<Id, Account>([select Id, ParentId, Custom_Account_ID__c,SF_Account_ID_of_Parent__c,
      Global_Tiering__c,Parent_Account_Location__c, Country_Picklist__c,State__c
      from Account where Id in : ParAccSet]);


 for ( Account acct : Trigger.new ) 
  {

   
  if (acct.ParentId == null)     
    { 
      acct.Ultimate_Parent_Id__c = acct.Id;
      acct.SF_Account_ID_of_Parent__c = acct.Custom_Account_ID__c;
      if(acct.Country_Picklist__c == 'United States') 
             {acct.Parent_Account_Location__c = acct.State__c;}
      else 
             {acct.Parent_Account_Location__c = acct.Country_Picklist__c;}     
    }
  
  
  else
    {
      acct.Ultimate_Parent_Id__c = ParAccMap.get(acct.ParentId).Id;
      acct.SF_Account_ID_of_Parent__c = ParAccMap.get(acct.ParentId).Custom_Account_ID__c;
      if(ParAccMap.get(acct.ParentId).Country_Picklist__c == 'United States') 
             {acct.Parent_Account_Location__c = ParAccMap.get(acct.ParentId).State__c;}
        else {acct.Parent_Account_Location__c = ParAccMap.get(acct.ParentId).Country_Picklist__c;}
      acct.Global_Tiering__c = ParAccMap.get(acct.ParentId).Global_Tiering__c;
    }
  }
}


Message Edited by RDN_LHR on 12-09-2008 07:31 AM
Drew1815Drew1815
I recommend putting in System.debug messages to help you troubleshoot the problem.
They will help you understand what is happening on insert and on update. And it will help you understand what part of the if/else statements are causing your issue.
WhyserWhyser
The reason why it doesn't work on a before insert is as follows:
 
// This line makes the Ultimate Parent ID = Account Id if the Account's Parent Id = null
acct.Ultimate_Parent_Id__c = acct.Id;
 
This is the line that fails.
 
Think about what the Trigger.New values hold before an insert. Since the data hasn't been inserted into the Salesforce database, there will be no ID associated with the trigger.new values. What you're essentially doing in that line of code is assigning the Ultimate Parent Id a null value.
 
In order to do this properly, you will need to differentiate between Trigger.isInsert and Trigger.isUpdate, and you will probably have to change your trigger call to "after insert" instead of "before insert".
 
You will have an ID in trigger.new in the "after insert" trigger, and you can set the ultimate parent Id then, but you will have to run a DML call to update the account with the new value.
 
Hope that makes sense.