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
JohnDuraiJohnDurai 

Hi can somebody help me with solving this trigger? Here is my requirement.

I have a custom object customer project relationship with Opportunity. in my custom object i have status field and if the satus field is active then it shoud automatically check the checkbox in opportunity object ( I have created custom checkbox field in opportunity object). Below is my attempt to try but could not able to proceed further can someone guide me?


trigger Activeprojectopp4 on Customer_Project__c (after insert, after update) {
//storing the list of customer project recodrs added  or updated
    List<Customer_Project__c> cprecords=new list<Customer_Project__c>();
    if(trigger.isinsert){
        for(Customer_Project__c cp: trigger.new){
            cprecords.add(cp);    
        }
    }
//fetching the activerecords from customer project and puting into map
    Map<id,Customer_Project__c> activerecords = new Map <id,Customer_Project__c>([select id,name,Opportunity__c from Customer_Project__c where status__c='Active']);
 
}


Thanks
Best Answer chosen by JohnDurai
Bhimaraj BBhimaraj B
Hi Ian,
 
I hope the below code works for you!
 
trigger ActiveProjectOpp on Customer_Project__c (after insert, after update) {
    Set<Id> opportunityIDs = new Set<Id>();
    for (Customer_Project__c cp : trigger.new) {
        if (cp.Status__c == 'Active') {
            opportunityIDs.add(cp.Opportunity__c);
        }
    }
    if (!opportunityIDs.isEmpty()) {
        List<Opportunity> opportunities = [SELECT Your_CheckBox_API__c
                                           FROM Opportunity
                                           WHERE Id IN : opportunityIDs];
        for (Opportunity opp : opportunities) {
            opp.Your_CheckBox_API__c = true;
        }
        update opportunities;  
    }
}

All Answers

Bhimaraj BBhimaraj B
Hi Ian,
 
I hope the below code works for you!
 
trigger ActiveProjectOpp on Customer_Project__c (after insert, after update) {
    Set<Id> opportunityIDs = new Set<Id>();
    for (Customer_Project__c cp : trigger.new) {
        if (cp.Status__c == 'Active') {
            opportunityIDs.add(cp.Opportunity__c);
        }
    }
    if (!opportunityIDs.isEmpty()) {
        List<Opportunity> opportunities = [SELECT Your_CheckBox_API__c
                                           FROM Opportunity
                                           WHERE Id IN : opportunityIDs];
        for (Opportunity opp : opportunities) {
            opp.Your_CheckBox_API__c = true;
        }
        update opportunities;  
    }
}
This was selected as the best answer
JohnDuraiJohnDurai
Thanks @Bhimaraj B it worked for me...but if i try to change the status from Active to Inactive in Customer_Project__c then the check box in opportunity still remains checked...