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 

Need help modifying a trigger

I need help modifying a trigger I wrote.  The trigger is supposed to update the parent opportunity record everytime the child record, Media_Plan_History__c, is updated, which works correctly.  

 

I need to add a condition to the trigger so that the opportunity is only updated if a checkbox called Active__c is set to True on the Media_Plan_History__c record.  Where can I add this logic to the trigger?  I can't quite figure it out.

 

Thanks!

 

Here's my trigger:

 

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, 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);
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

Give this a whirl...

 

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) {
    // PUT YOUR CONDITIONALITY HERE
    if(sh.Active__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);
}
}

 

-Andy

 

All Answers

spenceSetGetspenceSetGet

This is a guess.  I'm new to Apex.  Since this is my 3rd post....

 

Wrapping the code with something like 

 

if(sh.Active__c == true){

YOUR CODE TO EXECUTE IF CONDITION TRUE

}

 


Andy BoettcherAndy Boettcher

Give this a whirl...

 

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) {
    // PUT YOUR CONDITIONALITY HERE
    if(sh.Active__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);
}
}

 

-Andy

 

This was selected as the best answer
BrandiTBrandiT

Thank you so much!  That worked!