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
Kerstin KoehlerKerstin Koehler 

Have records created on Children Accounts associated to a Parent Account

Hello All!

I need help determining the best way to solve a business need. 

We have a Director of National Accounts that creates a promotion (custom object) at the HQ Account Record. The goal is to replicate that promotion record to the children that are tied to that parent HQ so that all individual owners of those child records can reference and see what promotion has been authorized at the top HQ level by our Director of National Accounts.

Premier support said I would need some coding, but i do not have any experience. Any ideas would be greatly appreciated of how I could accomplish this buisness need. Thank you in advance!!
LBKLBK
Hi Kerstin,

An AFTER INSERT, AFTER UPDATE trigger would be ideal for you to achieve this.

However, you need to take extra care to avoid cyclic triggers because you will be inserting / updating records in the same (Promotion) object as the trigger itself.

I have couple of questions here. Based on your answers I can help you out with the trigger.

1. Does this promotion created on HQ Account has to go through any kind of approval process, before made available for the Child accounts? Or, can the promotion for child accounts be created as soon as the HQ promotion is created?

2. Are you using the default ParentId based relationship in Account object?

3. What happens when the HQ promotion is updated at a latet point of time? Do the child promotions also go through the same changes?

4. Will all the values, except the Account, be the same for the HQ and Child promotions?

Let me know.
Kerstin KoehlerKerstin Koehler
Hello LBK! thank you for responding!

1. No, there will not be any type of approval process. The Director of National accounts will just create a Promotion record and click save which should start the process/trigger.

2. Yes I am using the default Parent Account Field on the Account Object

3. I asked our Director, and he said that 99.9% of the time he will not go back in and make a change to the promotion. So if we dont need to build that logic in then I would say leave it out, but if not too difficult to write the logic in, it probably wouldnt hurt to add. 

4. Yes, all the values will be the same. Whatever is entered at the HQ point, will also need to be the same on the child - easily mapped fields

Side note - while speaking with our Director today he brought up another point that I was curious if we could add. He would select States authorized for this promotion - is there a way that based on the states he selects the promotions will only replicate to the children accounts that are in those specific states?

Again, if that is way too complicated, we can just write it to replicate to all then I would just really highlight the field of AUTHORIZED ONLY IN THE FOLLOWING STATES: __,___,__.

Thank you again for all of your help! I truly appreciate it!
LBKLBK
Hi Kerstin,

Here is the sample trigger for you.
trigger addPromotionsToChildAccounts on Promotion__c (after insert){
	
	Set<Id> setParentAccountIds = new Set<Id>();
	for (Promotion__c objPromotion : trigger.new){
			setParentAccountIds.add(objPromotion.Account__r.Id)
	}
	
	//New promotions to be inserted for the child accounts
	List<Promotion__c> lstNewPromotions = new List<Promotion__c>(); 
	
	//fetch  the child accounts of the parent account for which the Promotion is created.
	List<Account> listChildAccounts = [SELECT Id, ParentId, BillingState FROM Account WHERE ParentId =: setParentAccountIds]; 
	
	for (Account objAccount : listChildAccounts){
		for (Promotion__c objPromotion : trigger.new){
			if(objAccount.ParentId == objPromotion.Account__r.Id){
				String sBillingState = (String)objAccount.BillingState;
				//if the billing state is matching the Allowed_States__c list
				if(objPromotion.Allowed_States__c == null || (objPromotion.Allowed_States__c != null && objPromotion.Allowed_States__c.contains(sBillingState))){
					Promotion__c objNewPromotion = new Promotion__c();
					objNewPromotion.Account__c = objAccount;
					
					objNewPromotion.Name = objPromotion.Name;
					//....
					//Please add all your Promotion__c custom fields here, in the following manner.
					//objNewPromotion.CUSTOM_FIELD_NAME__c = objPromotion.CUSTOM_FIELD_NAME__c;
					//....
					
					lstNewPromotions.add(objNewPromotion);
				}
			}
		}
	}
	
	if(lstNewPromotions.size() > 0){
		insert lstNewPromotions;
	}
}
There are couple of important points to take care of.

1. Allowed States - I have added an IF condition in line 19 to check if the child account belongs to an allowed state.
I have assumed that the "Allowed States" field in Promotion__c is a multi-picklist field and it's API name is Allowed_States__c.
Change the field name, if it is different.

2. Between line 24 and 27, you have to add all your Promotion__c fields in the format mentioned.

I have already added Account__c and Name fields here, take care of the rest.

If there are any differences in the API names of the Custom object or Custom fields, you need to change the code accordingly.

Let me know how it goes.
Kerstin KoehlerKerstin Koehler
Thank you for your help with this!! 

I copied and pasted the code along with the adding in the other fields and this is waht I have so far. Hopefully you can open this image and zoom in (didnt know hwo to copy and paste the code like you did)

I am however receiving an error message - 

1) addPromotionsToChildAccounts - line 6 - expecting a semi-colon, found'}' 

My other question to you would be will I need to have test coverage or code coverage - or a test class? for this trigger?

Thank you again! you are a life saver!


User-added image
LBKLBK
Hi Kerstin,

You are on track. I just missed a semi colon in the line 5.

Can you add a ; in the end of the line 5?

It should look like this after adding the semi colon.
setParentAccountIds.add(objPromotion.Account__r.Id);

On a different note about posting code here, do you see a button with "< >" symbol in the top of this editor?

Click on it. When the popup opens, just paste your code there and say OK.

Let me know how it goes.
 
Kerstin KoehlerKerstin Koehler
Hi LBK,

that worked to fix the problem on line 6, however I am now experiencing another error message on line 21. 

objNewPromotion.Account__c = objAccount;

(error is saying Illegal assignment from Account to Id)

Note* our Account Field on the Promotion Object is a Master -Detail Lookup custom field. 

Thanks again for you help with this! 
 
trigger addPromotionsToChildAccounts on Promotion__c (after insert){
    
    Set<Id> setParentAccountIds = new Set<Id>();
    for (Promotion__c objPromotion : trigger.new){
            setParentAccountIds.add(objPromotion.Account__r.Id);
    }           
    
    //New promotions to be inserted for the child accounts
    List<Promotion__c> lstNewPromotions = new List<Promotion__c>();
    
    //fetch  the child accounts of the parent account for which the Promotion is created.
    List<Account> listChildAccounts = [SELECT Id, ParentId, BillingState FROM Account WHERE ParentId =: setParentAccountIds];
    
    for (Account objAccount : listChildAccounts){
        for (Promotion__c objPromotion : trigger.new){
            if(objAccount.ParentId == objPromotion.Account__r.Id){
                String sBillingState = (String)objAccount.BillingState;
                //if the billing state is matching the Allowed_States__c list
                if(objPromotion.Allowed_States__c == null || (objPromotion.Allowed_States__c != null && objPromotion.Allowed_States__c.contains(sBillingState))){
                    Promotion__c objNewPromotion = new Promotion__c();
                    objNewPromotion.Account__c = objAccount;
                     
                    objNewPromotion.Name = objPromotion.Name;
                    
                    objNewPromotion.Start_Date__c = objPromotion.Start_Date__c;
                    
                    objNewPromotion.End_Date__c = objPromotion.End_Date__c;
                    
                    objNewPromotion.Allowed_States__c = objPromotion.Allowed_States__c;
                    
                    objNewPromotion.Promotional_Item__c = objPromotion.Promotional_Item__c;
                    
                    objNewPromotion.Promotional_Item_Details__c = objPromotion.Promotional_Item_Details__c;
                    
                    objNewPromotion.POS_Authorized__c = objPromotion.POS_Authorized__c;
                    
                    objNewPromotion.POS_Item__c = objPromotion.POS_Item__c;
                                                      
                                    
                    lstNewPromotions.add(objNewPromotion);

                }

            }

        }

    }
    
    if(lstNewPromotions.size() > 0){
        insert lstNewPromotions;
    }
}


 
LBKLBK
Can you change the following line. objNewPromotion.Account__c = objAccount; with objNewPromotion.Account__c = objAccount.Id;
Kerstin KoehlerKerstin Koehler
Hello LBK!  I was on vacation last week so I apologize for the late response. I was able to write the trigger successfully however when I went to test this was the error message I received. Any idea on what may be happening or how I should resolve?

thank you in advance! I appreciate the help!
User-added image