You need to sign in to do that
Don't have an account?
RDN_LHR
Message Edited by RDN_LHR on 12-09-2008 07:31 AM
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
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.