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
cambart14cambart14 

Update Opportunity Based on Related Custom Object

I have created a custom object (Product_Brief__c) , that is related to a specific opportunity.  With the Product_Brief__c there are stages (Stage_c) that can be selected from a drop down list. My Opportunity has a custom checkbox field (Submit_to_New_Products__c).

 

I need when the stage = "Needs Corrections/ Clarifications" that my custom checkbox field (Submit_to_New_Products__c) to be unchecked.

 

The code I have written is below, but it is not updating the related Opportunity.

 

trigger uncheckSubmitPB on Product_Brief__c (after insert, after update) {

    Set<ID> oppIds = new Set<ID>();   
    for(Product_Brief__c PB : Trigger.new)
    
    if(PB.Stage__c=='Needs Corrections/ Clarification')
    {
    oppIds.add(PB.Opportunity__r.id);
    }
    List<Opportunity> UpdateOpp = [SELECT ID, Submit_to_New_Products__c  FROM Opportunity WHERE Opportunity.id IN : oppIds];
    for(Opportunity ol: UpdateOpp)
    {ol.Submit_to_New_Products__c=false;}
    update UpdateOpp;

    
}

 

 Thanks for your help!

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

When you said related object... the object product_Brief__c would have a lookup to opportunity...  

 

try this code..

 

trigger uncheckSubmitPB on Product_Brief__c (after insert, after update) {

Set<ID> oppIds = new Set<ID>();
for(Product_Brief__c PB : Trigger.new)

if(PB.Stage__c=='Needs Corrections/ Clarification')
{
oppIds.add(PB.Opportunity__c); //opportunity lookup field API name
}
List<Opportunity> UpdateOpp = [SELECT ID, Submit_to_New_Products__c FROM Opportunity WHERE Opportunity.id IN : oppIds];
for(Opportunity ol: UpdateOpp)
{ol.Submit_to_New_Products__c=false;}
update UpdateOpp;


}