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
HamptonHampton 

Code Error: Updating Field on Parent with Value from Child

Good Morning:

 

I am getting a code error here and not sure where I went wrong.

 

I have custom object SFU__c which is the parent to Penetration_Summary__c.

 

I want to take custom fields Non_Video_Subs__c and Video_Penetration__c from Penetration_Summary and update Non_Video_Addresses__c and Current_Penetration__c on SFU__C where Penetration_Summary__c.Current__c=TRUE

 

Here is the code I have:

 

trigger SFUMetrics on Penetration_Summary__c (after insert, after update) {

Set<Id> PenetrationSummaryId = new Set<Id>(); 

for (Penetration_Summary__c penetration : Trigger.new) {
    if(penetration.Current__c = TRUE) {
        PenetrationSummaryId.add(penetration.Id);
    }
} 

Set<Id> SFUId = new Set<Id>();
for(Penetration_Summary__c penetration1:[Select ID, SFU__c, Non_Video_Subs__c, Video_Penetration__c from Penetration_Summary__c Where Id IN:PenetrationSummaryId ]) {
    SFUId.add(penetration1);
}


List<SFU__c> sfuforupdate = new List<SFU__c>();

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:SFU.Id ]) {
    
    set.(c.non_video_addresses__c) == penetration1.Non_Video_Subs__c;
    set.(c.current_penetration__c) == penetration1.Video_Penetration__c;
}
update sfuforupdate;    
}

 I am getting Error: Compile Error: Variable does not exist: penetration1.Non_Video_Subs__c at line 22 column 39

 

Suggestions?

 

Thanks,

 

Hampton

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

They aren't being added to the sfuforupdate list - should be as simple as:

 

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:PenetrationSummaryId]) {
    
       c.current_penetration__c=sumsBySFUId.get(c.ID).Video_Penetration__c;
       c.non_video_addresses__c=sumsBySFUId.get(c.ID).Non_Video_Subs__c;
       sfuforupdate.add(c);
}
update sfuforupdate;  

 

All Answers

bob_buzzardbob_buzzard

Penetration1 is the control variable for the first loop, so it won't be available once that loop exits.   I reckon you need to retain those records in a map keyed by the SFU__c field.  Something like:

 

Map<Id, Penetration_Summary__c> sumsBySFUId = new Map<Id, Penetration_Summary__c>();
for(Penetration_Summary__c penetration1:[Select ID, SFU__c, Non_Video_Subs__c, Video_Penetration__c from Penetration_Summary__c Where Id IN:PenetrationSummaryId ]) {
    sumsBySFUId.put(penetration1.SFU__c, penetration1);
}

List<SFU__c> sfuforupdate = new List<SFU__c>();

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:SFU.Id ]) {
    
    c.Non_Video_Address__c=sumsBySFUId.get(c.SFU__c).Non_Video_Subs__c;
    c.current_penetration__c=
               sumsBySFUId.get(c.SFU__c).Video_Penetration__c;
}

 

 

 

HamptonHampton

Thanks Bob. I was able to save the code, however when I attempted to save a record, I got:

 

Apex trigger SFUMetrics caused an unexpected exception, contact your administrator: SFUMetrics: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.SFUMetrics: line 6, column 1

 


 

trigger SFUMetrics on Penetration_Summary__c (after insert, after update) {

Set<Id> PenetrationSummaryId = new Set<Id>(); 

for (Penetration_Summary__c penetration : Trigger.new) {
    if(penetration.Current__c = TRUE) {
        PenetrationSummaryId.add(penetration.Id);
    }
} 
    
Map<Id, Penetration_Summary__c> sumsBySFUId = new Map<Id, Penetration_Summary__c>();
for(Penetration_Summary__c penetration1:[Select ID, SFU__c, Non_Video_Subs__c, Video_Penetration__c from Penetration_Summary__c Where Id IN:PenetrationSummaryId ]) {
    sumsBySFUId.put(penetration1.SFU__c, penetration1);    
    
}


List<SFU__c> sfuforupdate = new List<SFU__c>();

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:PenetrationSummaryId]) {
    
       c.current_penetration__c=sumsBySFUId.get(c.ID).Video_Penetration__c;
       c.non_video_addresses__c=sumsBySFUId.get(c.ID).Non_Video_Subs__c;
}
update sfuforupdate;    
}

 

 

bob_buzzardbob_buzzard

This is because your trigger is after insert/update, and the trigger.new records are not writeable.  You should be able to switch these to before insert/update, as you aren't relying on ids of the Penetration_Summary__c records anywhere.

HamptonHampton

Switching to Before Insert/Before Update is now giving me:

 

Error:Apex trigger SFUMetrics caused an unexpected exception, contact your administrator: SFUMetrics: execution of BeforeUpdate caused by: System.StringException: Invalid id: Tamarack: External entry point

 

Tamarack is the record name of the SFU I am attempting to update and this record has already been created.

 

Thanks,

 

Hampton

bob_buzzardbob_buzzard

Is SFU__c not a lookup to an SFU record then?  From that error, it looks like its a string representing the name.

HamptonHampton

Yeah I am an idiot. I forgot I had a field called SFU__c and SFU_Lookup__c (SFU was a free text) so I have changed the code to use SFU_Lookup__c.

 

The code is saving and the records are saving but the fields on the parent are still not updating. I've been staring at this for 40 minutes and cannot spot where/why.

 

Thoughts?

 

Thanks,

 

Hampton

 

trigger SFUMetrics on Penetration_Summary__c (before insert, before update) {

Set<Id> PenetrationSummaryId = new Set<Id>(); 

for (Penetration_Summary__c penetration : Trigger.new) {
    if(penetration.Current__c = TRUE) {
        PenetrationSummaryId.add(penetration.SFU_Lookup__c);
    }
} 
    
Map<Id, Penetration_Summary__c> sumsBySFUId = new Map<Id, Penetration_Summary__c>();
for(Penetration_Summary__c penetration1:[Select ID, SFU_Lookup__c, Non_Video_Subs__c, Video_Penetration__c from Penetration_Summary__c Where SFU_Lookup__c IN:PenetrationSummaryId ]) {
    sumsBySFUId.put(penetration1.SFU_Lookup__c, penetration1);    
    
}


List<SFU__c> sfuforupdate = new List<SFU__c>();

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:PenetrationSummaryId]) {
    
       c.current_penetration__c=sumsBySFUId.get(c.ID).Video_Penetration__c;
       c.non_video_addresses__c=sumsBySFUId.get(c.ID).Non_Video_Subs__c;
}
update sfuforupdate;    
}

 

bob_buzzardbob_buzzard

They aren't being added to the sfuforupdate list - should be as simple as:

 

for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c
 from SFU__c where Id IN:PenetrationSummaryId]) {
    
       c.current_penetration__c=sumsBySFUId.get(c.ID).Video_Penetration__c;
       c.non_video_addresses__c=sumsBySFUId.get(c.ID).Non_Video_Subs__c;
       sfuforupdate.add(c);
}
update sfuforupdate;  

 

This was selected as the best answer
HamptonHampton

Thank you so much for your help. That did it.

HamptonHampton

OK...last question...where do I make the change if I want to do an after insert/after update trigger rather than before? It does not seem to be doing what i need it to do.

bob_buzzardbob_buzzard

After insert/update won't allow you to change the records that are in the trigger.  Looking at your code, I can't see that before or after would affect what you are trying to do, unless the fields that you are copying to the SFU are populated by another trigger or workflow, or are formula fields.

HamptonHampton

Yeah they are both custom formual fields on the Penetration_Summary__c record. I am going to change the code to pass over the fields that compose the formula to the SFU__c record and have the formulas on that record. That should work. Thank you for catching that.