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
Shawn Reichner 29Shawn Reichner 29 

Trigger Creating Duplicate records

Hello Developers,

I have created a trigger that based on criteria on an Account record, will create child account records.  I then have some Process Builder processes checking a checkbox on the Parent account record when the children are created to hopefully run the trigger only once per child record.  The first child account record gets created and the process does what it needs to do just fine, but in my below trigger code, the second scenario of the "Complete" child record (The second if statement in the code) if both Anywhere and Complete checkboxes are selected when the parent Accoutn gets created,then I get two Child accounts for Complete.  This started after introducing the processes to only fire each scenario of the trigger once as the checkboxes on the parent record could be selected at different times.  I have spent many hours looign over this and can not figure out why two "Complete" child accounts are getting created only when both scenarios of my trigger have thier criteria met.   Can you please help me awesome Devs!!!!

Here is my trigger code...
 
trigger CreateChildAccount on Account (after insert, after update){

    String TriggerRan = 'False';
    String TriggerRanComplete = 'False';
    
    Id AcctRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Prospect').getRecordTypeId();
    List<Account> ChildAnywhere = new List<Account>();
    List<Account> ChildComplete = new List<Account>();
        
    For(Account a : Trigger.new){
        
        If(a.Armor_Anywhere__c == true && a.ChildTriggerRan__c != True && TriggerRan == 'False'){
          ChildAnywhere.add(new Account(name = a.Name + '_Anywhere',ParentId = a.Id,
          Parent_Account_Custom__c = a.Id,Additional_Partner_Application_Comments__c = a.Additional_Partner_Application_Comments__c,
          Application_Type__c = a.Application_Type__c,Cloud_Environment__c = a.Cloud_Environment__c,
          IS_Company_Category__c = a.IS_Company_Category__c,Channel__c = a.Channel__c,
          IS_Company_Facebook_Fans__c = a.IS_Company_Facebook_Fans__c,IS_Company_Facebook_Profile_URL__c = a.IS_Company_Facebook_Profile_URL__c,
          IS_Company_Summary__c = a.IS_Company_Summary__c,IS_Company_Twitter_Followers__c = a.IS_Company_Twitter_Followers__c,
          IS_Company_Twitter_Profile_URL__c = a.IS_Company_Twitter_Profile_URL__c,Original_Comments__c = a.Original_Comments__c,
          Primary_Customer_Size__c = a.Primary_Customer_Size__c,DBA_if_Applicable__c = a.DBA_if_applicable__c,
          IS_Estimated_Annual_Revenue__c = a.IS_Estimated_Annual_Revenue__c,IS_Estimated_Employees__c = a.IS_Estimated_Employees__c,
          IS_Estimated_Online_Ad_Budget__c = a.IS_Estimated_Online_Ad_Budget__c,IS_Estimated_PPC_Budget__c = a.IS_Estimated_PPC_Budget__c,
          IS_Founded_Year__c = a.IS_Founded_Year__c,How_Did_you_Hear_About_Armor__c = a.How_did_you_hear_about_Armor__c,
          IS_InboundScore__c = a.IS_InboundScore__c,Industry_Firehost__c = a.Industry_FireHost__c,
          Created_By_Conversion__c = a.Created_By_Conversion__c,Lead_Record_Type__c = a.Lead_Record_Type__c,
          Lead_Source__c = a.Lead_Source__c,IS_Company_LinkedIn_URL__c = a.IS_Company_LinkedIn_URL__c,
          IS_Location__c = a.IS_Location__c,My_Company_Size_Is__c = a.My_Company_Size_Is__c,
          NAICS_Code__c = a.NAICS_Code__c,Partner_Categories__c = a.Partner_Categories__c,
          Partner_Submitted__c = a.Partner_Submitted__c,Primary_Customer_Industries__c = a.Primary_Customer_Industries__c,
          Original_Interests__c = a.original_interests__c,Referral_Id__c = a.Referral_ID__c,
          ReferralId__c = a.ReferralId__c,Referrer_Code__c = a.Referrer_Code__c,
          Referring_Partner__c = a.Referring_Partner__c,Relationship_Entity__c = a.Relationship_Entity__c,
          X5_Largest_Customers__c = a.X5_Largest_Customers__c,Company_Size__c = a.Company_Size__c,
          Company_Success_Story__c = a.Company_Success_Story__c,Customer_Count__c = a.Customer_Count__c,
          Customers_Placed_at_a_Hosting_Company__c = a.Customers_Placed_at_a_Hosting_Company__c,Level_of_Engagement__c = a.Level_of_Engagement__c,
          Past_Partnerships__c = a.Past_Partnerships__c,Primary_Services__c = a.Primary_Services__c,
          Target_Market__c = a.Target_Market__c,Target_Market_Size__c = a.Target_Market_Size__c,
          Solution_Competencies__c = a.Solution_Competencies__c,Source_Territory__c = a.Source_Territory__c,
          Submitting_Partner_User__c = a.Submitting_Partner_User__c,mkto_custom_UTM_Campaign__c = a.mkto_custom_UTM_Campaign__c,
          mkto_custom_UTM_Medium__c = a.mkto_custom_UTM_Medium__c,mkto_custom_UTM_Source__c = a.mkto_custom_UTM_Source__c,
          IS_Website_Traffic_Rank__c = a.IS_Website_Traffic_Rank__c,RecordTypeId = AcctRecordTypeId,ChildTriggerRan__c = True));
          TriggerRan = 'True';
        }
        
        If(a.Armor_Complete__c == true && a.ChildTriggerRanComplete__c != True && TriggerRanComplete == 'False'){
          ChildComplete.add(new Account(name = a.Name + '_Complete',ParentId = a.Id,
          Parent_Account_Custom__c = a.Id,Additional_Partner_Application_Comments__c = a.Additional_Partner_Application_Comments__c,
          Application_Type__c = a.Application_Type__c,Cloud_Environment__c = a.Cloud_Environment__c,
          IS_Company_Category__c = a.IS_Company_Category__c,Channel__c = a.Channel__c,
          IS_Company_Facebook_Fans__c = a.IS_Company_Facebook_Fans__c,IS_Company_Facebook_Profile_URL__c = a.IS_Company_Facebook_Profile_URL__c,
          IS_Company_Summary__c = a.IS_Company_Summary__c,IS_Company_Twitter_Followers__c = a.IS_Company_Twitter_Followers__c,
          IS_Company_Twitter_Profile_URL__c = a.IS_Company_Twitter_Profile_URL__c,Original_Comments__c = a.Original_Comments__c,
          Primary_Customer_Size__c = a.Primary_Customer_Size__c,DBA_if_Applicable__c = a.DBA_if_applicable__c,
          IS_Estimated_Annual_Revenue__c = a.IS_Estimated_Annual_Revenue__c,IS_Estimated_Employees__c = a.IS_Estimated_Employees__c,
          IS_Estimated_Online_Ad_Budget__c = a.IS_Estimated_Online_Ad_Budget__c,IS_Estimated_PPC_Budget__c = a.IS_Estimated_PPC_Budget__c,
          IS_Founded_Year__c = a.IS_Founded_Year__c,How_Did_you_Hear_About_Armor__c = a.How_did_you_hear_about_Armor__c,
          IS_InboundScore__c = a.IS_InboundScore__c,Industry_Firehost__c = a.Industry_FireHost__c,
          Created_By_Conversion__c = a.Created_By_Conversion__c,Lead_Record_Type__c = a.Lead_Record_Type__c,
          Lead_Source__c = a.Lead_Source__c,IS_Company_LinkedIn_URL__c = a.IS_Company_LinkedIn_URL__c,
          IS_Location__c = a.IS_Location__c,My_Company_Size_Is__c = a.My_Company_Size_Is__c,
          NAICS_Code__c = a.NAICS_Code__c,Partner_Categories__c = a.Partner_Categories__c,
          Partner_Submitted__c = a.Partner_Submitted__c,Primary_Customer_Industries__c = a.Primary_Customer_Industries__c,
          Original_Interests__c = a.original_interests__c,Referral_Id__c = a.Referral_ID__c,
          ReferralId__c = a.ReferralId__c,Referrer_Code__c = a.Referrer_Code__c,
          Referring_Partner__c = a.Referring_Partner__c,Relationship_Entity__c = a.Relationship_Entity__c,
          X5_Largest_Customers__c = a.X5_Largest_Customers__c,Company_Size__c = a.Company_Size__c,
          Company_Success_Story__c = a.Company_Success_Story__c,Customer_Count__c = a.Customer_Count__c,
          Customers_Placed_at_a_Hosting_Company__c = a.Customers_Placed_at_a_Hosting_Company__c,Level_of_Engagement__c = a.Level_of_Engagement__c,
          Past_Partnerships__c = a.Past_Partnerships__c,Primary_Services__c = a.Primary_Services__c,
          Target_Market__c = a.Target_Market__c,Target_Market_Size__c = a.Target_Market_Size__c,
          Solution_Competencies__c = a.Solution_Competencies__c,Source_Territory__c = a.Source_Territory__c,
          Submitting_Partner_User__c = a.Submitting_Partner_User__c,mkto_custom_UTM_Campaign__c = a.mkto_custom_UTM_Campaign__c,
          mkto_custom_UTM_Medium__c = a.mkto_custom_UTM_Medium__c,mkto_custom_UTM_Source__c = a.mkto_custom_UTM_Source__c,
          IS_Website_Traffic_Rank__c = a.IS_Website_Traffic_Rank__c,RecordTypeId = AcctRecordTypeId,ChildTriggerRanComplete__c = True));
          TriggerRanComplete = 'True';
        }
        
    }
	
    If(ChildAnywhere.size()>0){
    insert ChildAnywhere;
    }
    
    If(ChildComplete.Size()>0){
    insert ChildComplete;  
    }
    
}

Here is a snapshot of each Process to supposedly stop the trigger form firing again on subsequent updates to the parent account record...

For Anywhere Scenario...
Criteria - 

User-added image
Record Update Action - 

User-added image

Complete Criteria - 

User-added image

Complete Record Update Action - 

User-added image

Both process pretty much have the same criteria and actions...

Criteria...On Account object when record created / For Anywhere process the criteria is lookign for ChildTriggerRan checkbox firld to be true AND Parent ID Lookup field not being NULL / For Complete it is looking for ChildTriggerRunComplete checkbox to be true AND Parent Id Lookup field not NULL

Then Record Update Action is  / For Anywhere - Update the Parent Record ID populated only if the ChildTriggerRan field is currently false  and update that field to True / For Complete it will update the Parent Record ID populated only if the ChildTriggerRanComplete field is currently false and update that field to True. 





Thnak you for your time, I hope some one can help me as I am stumped on this one!!!

Shawn