You need to sign in to do that
Don't have an account?
Trigger Help - Concatenate child records on a Text Area on Parent Object
Hello, I'm trying to finalize a trigger to update. I am receiving an error on foreign key invalid relationships.
- Implementation_Site__c is child to Grant__c
- Country__c field is on Implementation_Site__c
- Countries__c is on Grant__c - this is the field that needs to be populated with infinite number of Country__c values coming from child records.
Current code:
//This Trigger will fire after insert, update, delete and undelete
trigger trgConCatCountries on Implementation_Site__c (after insert, after update, after delete, after undelete) {
//If the event is insert or undelete, this list takes New Values or else it takes old values
List<Implementation_Site__c> ProjStrategyList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
//to store Project Ids
List<Id> ProjectIds = new List<Id>();
//Loop through the Records to store the project Id values from the Implementation Site
for (Implementation_Site__c proj_Strat : ProjStrategyList) {
ProjectIds.add(proj_Strat.Grant__c);
}
//Sub-query to get the projects and all its Child Records where Id is equal to the Ids stored in ProjectIds
//Implementation_Sites__r is the Child Relationship name appended by '__r' as it is a custom object
List<Grant__c> ProjectList = [
select
id,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r),
Grant__c
from
Grant__c
where
id in :ProjectIds];
//Loop through the List and store the Child Records as a String of values in Long Text Area Field i.e Countries__c
for (Grant__c proj : ProjectList) {
if(proj.Implementation_Sites__r.size() > 0)
{
proj.Countries__c = string.valueOf(proj.Implementation_Sites__r[0].Country__c);
for(integer i=1;i < proj.Implementation_Sites__r.size();i++)
{
proj.Countries__c = proj.Countries__c + '; ' + string.valueOf(proj.Implementation_Sites__r[i].Country__c);
}
}
else
proj.Countries__c = null;
}
//update the List
update ProjectList;
}
Help! Thanks!
List<Grant__c> ProjectList = [
select
id,Grant__c,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r)
from
Grant__c
where
id in :ProjectIds];
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
Write the query in this way
List<Grant__c> ProjectList = [
select
id,Grant__c,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r),
from
Grant__c
where
id in :ProjectIds];
?
List<Grant__c> ProjectList = [
select
id,Grant__c,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r)
from
Grant__c
where
id in :ProjectIds];
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Awesome! I've just removed Grant__c from the call and it works perfectly. Thank you!
List<Grant__c> ProjectList = [
select
id,Grant__c,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r)
from
Grant__c
where
id in :ProjectIds];
Final Code
//This Trigger will fire after insert, update, delete and undelete
trigger trgConCatCountries on Implementation_Site__c (after insert, after update, after delete, after undelete) {
//If the event is insert or undelete, this list takes New Values or else it takes old values
List<Implementation_Site__c> ProjStrategyList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
//to store Project Ids
List<Id> ProjectIds = new List<Id>();
//Loop through the Records to store the project Id values from the Implementation Site
for (Implementation_Site__c proj_Strat : ProjStrategyList) {
ProjectIds.add(proj_Strat.Grant__c);
}
//Sub-query to get the projects and all its Child Records where Id is equal to the Ids stored in ProjectIds
//Implementation_Sites__r is the Child Relationship name appended by '__r' as it is a custom object
List<Grant__c> ProjectList = [
select
id,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r)
from
Grant__c
where
id in :ProjectIds];
//Loop through the List and store the Child Records as a String of values in Long Text Area Field i.e Countries__c
for (Grant__c proj : ProjectList) {
if(proj.Implementation_Sites__r.size() > 0)
{
proj.Countries__c = string.valueOf(proj.Implementation_Sites__r[0].Country__c);
for(integer i=1;i < proj.Implementation_Sites__r.size();i++)
{
proj.Countries__c = proj.Countries__c + '; ' + string.valueOf(proj.Implementation_Sites__r[i].Country__c);
}
}
else
proj.Countries__c = null;
}
//update the List
update ProjectList;
}
Credit goes to Asish as well for pointing out that.
Thanks
In my Case i have Account as parent with Market type as mpl
And Account Market as child with Market type as a field, now if accout A have 5 Market type records as :-
Account Market1 with Market type 'Auto'
Account Market2 with Market type 'SUV'
Account Market3 with Market type 'RV'
Account Market4 with Market type 'Auto'
Account Market5 with Market type 'RV'
Then Markert type (MPL) on account should have unique pair of values:- Auto;SUV;RV
Thanks in advance for your help
Here is the pseudo code for populating the multipicklist values that are unique.
List<Account> AccountList = [select id,Name,Market_Type__c,(select id,Name, Market_Type__c from Account_Market__r) from Account where id in :Accds];
Map<Id,SET<String>> mapTemp = new Map<Id,SET<String>>();
for(Account acc : AccountList){
SET<String> fieldVals = SET<String>();
for(Account_market__c am : acc.Account_Market__r){
fieldVals.add(am.Market_Type__c);
mapTemp.put(acc.id,fieldVals);
}
}
for(Account acc : AccountList){
List<String> listTemp =new List<String>(mapTemp.get(acc.id));
for(Integer i=0;i<listTemp.size();i++){
if(i == listTemp.size() -1)
acc.market_Type__c = listTemp[i];
else
acc.market_Type__c = String.join(listTemp[i], ';');
}
update AccountList;
Just noting that this issue helped me resolve a problem within our org. When trying to do a mass update via data loader I am recieving the "Too many SQL queries: 101. Any guidance? I am currently attempting to rewrite this ..
/// Code modified below
/This Trigger will fire after insert, update, delete and undelete
trigger trgConCatCountries on Implementation_Site__c (after insert, after update, after delete, after undelete) {
//If the event is insert or undelete, this list takes New Values or else it takes old values
List<Implementation_Site__c> ProjStrategyList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
//to store Project Ids
List<Id> ProjectIds = new List<Id>();
//Loop through the Records to store the project Id values from the Implementation Site
for (Implementation_Site__c proj_Strat : ProjStrategyList) {
ProjectIds.add(proj_Strat.Grant__c);
}
//Sub-query to get the projects and all its Child Records where Id is equal to the Ids stored in ProjectIds
//Implementation_Sites__r is the Child Relationship name appended by '__r' as it is a custom object
List<Grant__c> ProjectList = [
select
id,
(select id,Name, Grant__r.Name, Country__c, District_State__c from Implementation_Sites__r)
from
Grant__c
where
id in :ProjectIds];
//Loop through the List and store the Child Records as a String of values in Long Text Area Field i.e Countries__c
for (Grant__c proj : ProjectList) {
if(proj.Implementation_Sites__r.size() > 0)
{
proj.Countries__c = string.valueOf(proj.Implementation_Sites__r[0].Country__c);
for(integer i=1;i < proj.Implementation_Sites__r.size();i++)
{
proj.Countries__c = proj.Countries__c + '; ' + string.valueOf(proj.Implementation_Sites__r[i].Country__c);
}
}
else
proj.Countries__c = null;
}
//update the List
update ProjectList;
}