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
BrandiTBrandiT 

Help needed with trigger to update parent record

I need help with a simple trigger.  I have an object called Media_Plan_History, which is a child to the Opportunity object.  There can only be one active Media Plan History record per Opportunity.

 

Both objects have a lookup field called Media Planner.  I need a trigger that will fire when the Media Plan History record is updated that would update the Media Planner field on the parent opportunity record to match the Media Planner field on the Media History Plan.

 

so opp.Media_Planner__c = mph.Media_Planner__c

 

Here is what I have so far, but I'm missing something important because I keep getting error messages:

 

trigger OppOwnerSH on Media_Plan_History__c (after update) {

Media_Plan_History__c[] shs;
shs = Trigger.new;

set<Id> oppIds = new Set <ID>();

for (Media_Plan_History__c sh : shs) {
    oppIds.add(sh.Opportunity__c);

Map <ID, Media_Plan_History__c> shforOpps = new Map <ID, Media_Plan_History__c> (
    [select Id, opportunity__c, Media_Planner_2__c, Artist_2__c from Media_Plan_History__c
    where opportunity__c in :oppIds]);

Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity> (
    [select Id, Media_Planner_2__c, Artist_2__c from Opportunity
    where Id in :oppIds]);

for (Opportunity opp : oppsToUpdate) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;
}}}

 

 

Here's the message I'm getting:

Error: Compile Error: Loop must iterate over a collection type: MAP<Id,Opportunity> at line 19 column 24

 

 

 

 

 

I would really appreciate some help with this one.  I'm stil pretty new to coding.  Thank you!!

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You have issues with the trigger, especially doing DML inside a for loop....

 

your error come from trying to itenerate over a map...

 

Change this:

 

for (Opportunity opp : oppsToUpdate) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;

 

to

 

for (Opportunity opp : oppsToUpdate.values()) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;

 

to solve the immediate error

All Answers

Starz26Starz26

You have issues with the trigger, especially doing DML inside a for loop....

 

your error come from trying to itenerate over a map...

 

Change this:

 

for (Opportunity opp : oppsToUpdate) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;

 

to

 

for (Opportunity opp : oppsToUpdate.values()) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;

 

to solve the immediate error

This was selected as the best answer
BrandiTBrandiT

Thank you!

 

I've rewritten the trigger after reading your feedback and it works great now.  Thanks again!

 

trigger OppOwnerSH on Media_Plan_History__c (after update) {
 
Media_Plan_History__c[] shs;
shs = Trigger.new;
 
set<Id> oppIds = new Set <ID>();
 
for (Media_Plan_History__c sh : shs) {
      if(sh.Active_Media_Plan__c == true) {
         oppIds.add(sh.Opportunity__c);
    }
 
Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity> (
    [select Id, Media_Planner_2__c from Opportunity
    where Id in :oppIds]);

  
List<Opportunity> oppUpd = new List<Opportunity>{};
 
for (Opportunity opp : oppsToUpdate.values()) {
    opp.media_planner_2__c = sh.media_planner_2__c;
    update opp;
    oppUpd.add(opp);
}


if (oppUpd != null && !oppUpd.isEmpty())
Database.update(oppUpd);

}
}
}