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
tylerotylero 

Trigger to copy Custom Object record from one Account to another Account

I am new to coding and I am working on my first trigger.

 

I have an object called "Affiliations" and another called "Addresses." The Affiliations object enables multiple Business Account relationships for Person Accounts instead of one Business Account per Person Account. The Addresses object enables an Account to have multiple Addresses.

 

Business rules:

1. Every Business Account only has 1 physical address.

2. Person Accounts may have many addresses.

3. Person Accounts may have one or more Business Account affiliation(s).

4. Business Accounts may have one or more Business Account affiliation(s).

5. Each Person Account affiliated to a Business Account should inherit the physical address of the Business Account.

 

I am trying to achieve item #5 with a trigger. Whenever an Affiliation is created between a Business Account and Person Account, I want the Address record of the Business Account to be copied/cloned and made an attribute of the Person Account. I can get the trigger to create an Address record on the Person Account when an Affiliation record is created, but don't know what to do from there to get the values from the Business Account address on the new Address record.

 

This is what I have so far...

 

 

 

trigger createAffiliationAddress on Affiliation_SC__c (after insert, after update){

 


List<Address_SC__c> copyParentAddress = new List<Address_SC__c>();

List<Address_SC__c> parentAddress = [SELECT Id, Account_SC__c, Address_Line_1_SC__c, Address_Line_2_SC__c, City_SC__c,
Country_SC__c, Postal_Code_SC__c, State_SC__c
FROM Address_SC__c
WHERE Account_SC__c = :Trigger.new[0].id];

 

for (Affiliation_SC__c newAffiliation: Trigger.new){
copyParentAddress.add (new Address_SC__c(
Account_SC__c = newAffiliation.To_Account_SC__c));

 

// Rather than insert the addresses individually, add the addresses to a list and bulk insert it. This makes the
// trigger run faster and allows us to avoid hitting the governor limit on DML statements.

}
insert copyParentAddress;
}

 

 

 

Will you please help?

Best Answer chosen by tylero
cmoylecmoyle
Hey tylero, I'm going to take a swing at this.
I'm going to assume you have a lookup from addresses to accounts. I also am interpreting the affiliation object as a junction object between 2 accounts. I haven't tested any of this code so I may have made some syntax errors.


trigger createAffiliationAddress on Affiliation_SC__c(before insert){
//Get unique set of business addresses
Set<ID> businessAccountIdSet = new Set<ID>();
for(Affiliation_SC__c affiliation : Trigger.new){
businessAccountIdSet.add(affiliation.Business_Account_Id__c);
}

//Map addresses to business accounts
Map<ID> accountMap = new Map<ID>( [SELECT Id, (SELECT Id, Address_Line_1_SC__c, Address_Line_2_SC__c, City_SC__c, Country_SC__c, Postal_Code_SC__c, State_SC__c FROM Address_SC__r) FROM Account WHERE Id IN :businessAccountIdSet]);

//Iterate through affiliations and clone addresses from business accounts to person accounts
List<ADDRESS_SC__C> personAccountAddressList = new List<ADDRESS_SC__C>();
for(Affiliation_SC__c affiliation : Trigger.new){
List<ADDRESS_SC__C> affiliationAddressList = accountMap.get(affiliation.Business_Account_Id__c).Address_SC__r;
for(Address_SC__c address : affiliationAddressList){
Address_SC__c clonedAddress = address.clone(false, true, false, true);
clonedAddress.Account_SC__c = affiliation.Person_Account_Id__c;
personAccountAddressList.add(clonedAddress);
}
}
insert personAccountAddressList;

}



I'm writing this on my ipad in the airport so I can't check this, but the above code should guide you in creating cloned records and associating them with affiliated accounts. The above code is also bulkified and should be safe from hitting any dml or soql limits.

All Answers

cmoylecmoyle
Hey tylero, I'm going to take a swing at this.
I'm going to assume you have a lookup from addresses to accounts. I also am interpreting the affiliation object as a junction object between 2 accounts. I haven't tested any of this code so I may have made some syntax errors.


trigger createAffiliationAddress on Affiliation_SC__c(before insert){
//Get unique set of business addresses
Set<ID> businessAccountIdSet = new Set<ID>();
for(Affiliation_SC__c affiliation : Trigger.new){
businessAccountIdSet.add(affiliation.Business_Account_Id__c);
}

//Map addresses to business accounts
Map<ID> accountMap = new Map<ID>( [SELECT Id, (SELECT Id, Address_Line_1_SC__c, Address_Line_2_SC__c, City_SC__c, Country_SC__c, Postal_Code_SC__c, State_SC__c FROM Address_SC__r) FROM Account WHERE Id IN :businessAccountIdSet]);

//Iterate through affiliations and clone addresses from business accounts to person accounts
List<ADDRESS_SC__C> personAccountAddressList = new List<ADDRESS_SC__C>();
for(Affiliation_SC__c affiliation : Trigger.new){
List<ADDRESS_SC__C> affiliationAddressList = accountMap.get(affiliation.Business_Account_Id__c).Address_SC__r;
for(Address_SC__c address : affiliationAddressList){
Address_SC__c clonedAddress = address.clone(false, true, false, true);
clonedAddress.Account_SC__c = affiliation.Person_Account_Id__c;
personAccountAddressList.add(clonedAddress);
}
}
insert personAccountAddressList;

}



I'm writing this on my ipad in the airport so I can't check this, but the above code should guide you in creating cloned records and associating them with affiliated accounts. The above code is also bulkified and should be safe from hitting any dml or soql limits.
This was selected as the best answer
tylerotylero

Thank you so much, cmoyle! Your swing hit it out of the park.

 

I had a difficult enough time writing it on my laptop; I am amazed you did it on your iPad waiting at the airport. I can't thank you enough.

 

Here's the final code:

 

 

 

trigger createAffiliationAddress on Affiliation_SC__c(before insert){

//Get unique set of business addresses
Set<Id> businessAccountIdSet = new Set<Id>();
for(Affiliation_SC__c affiliation : Trigger.new){
businessAccountIdSet.add(affiliation.From_Account_SC__c);
}

 

//Map addresses to business accounts
Map<Id, Account> accountMap = new Map<Id, Account>( [SELECT Id,
(SELECT Id, Name, Account_SC__c, Address_Line_1_SC__c, Address_Line_2_SC__c, City_SC__c,
Country_SC__c, Postal_Code_SC__c, State_SC__c, Type_SC__c FROM Addresses__r)
FROM Account WHERE Id IN :businessAccountIdSet]);

 

//Iterate through affiliations and clone addresses from business accounts to person accounts
List<Address_SC__c> personAccountAddressList = new List<Address_SC__c>();
for(Affiliation_SC__c affiliation : Trigger.new){

List<Address_SC__c> affiliationAddressList = accountMap.get(affiliation.From_Account_SC__c).Addresses__r;
for(Address_SC__c address : affiliationAddressList){
Address_SC__c clonedAddress = address.clone(false, true, false, true);
clonedAddress.Account_SC__c = affiliation.To_Account_SC__c;
personAccountAddressList.add(clonedAddress);
}
}
insert personAccountAddressList;
}

 

 

 

Thank you again!

 

- tylero