• Aaron Blair 9
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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!

 

  • August 09, 2013
  • Like
  • 1