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
Stephanie Zimmerman 7Stephanie Zimmerman 7 

Failing to get Trigger to Stamp Text Field from Parent to Child. Please Help!

I'm starting to learn Apex and working on basic triggers so apologies in advance if the code is bad.

Architecture: Parent: Opportunity; relevant fields = Draw_4__c (checkbox), Project_Stage__c (text) Child: Referral__c; relevant fields = Send_Email__c (checkbox), Status__c (text)

Referral__c is a junction object between Opportunity records. The purpose is to link one opportunity to another and say that this opportunity referred that opportunity.

On update of Opportunity, the code needs to always set Status__c = Project_Stage__c and only set Send_Email__c = TRUE if Draw_4__c = TRUE.

Right now, Send_Email__c is getting appropriately set but Status__c is not. I can tell that the problem is how I am trying to refer to the parent field (opportunity.project_stage__c). If I set Status__c = some text, such as 'Active', the trigger works as needed but if I set status__c = opportunity.project_stage__c, the field remains blank. I'm wondering if this has something to do with the fact that Referral is junction between two opportunity records.

Trigger:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
trigger UpdateReferral on Opportunity(after update) {

If(Trigger.isUpdate){

     Set<ID> ids = Trigger.newMap.keySet();
     list<Opportunity> updatedParents = [SELECT Id, 
                 (SELECT Id, Referred_Opportunity__c, Send_Email__c, Status__c 
                  from Referred_By__r ) FROM Opportunity
                 WHERE Id in :ids];
         List<Referral__c> childrenToUpdate = new List<Referral__c>();

         //Then loop through each parent object in updated parent
         for (Opportunity p : updatedParents) 
         { 
        if(p.Draw_4__c = TRUE){

                //and loop thru each kid in the child set}
               for(Referral__c kid : p.Referred_By__r) 
               { 
                         ///update logic from above
                             kid.Send_Email__c = True;
                             kid.Status__c = 'Active';
               childrenToUpdate.add(kid);
                }
                }
                else  {
           for(Referral__c kid2 : p.Referred_By__r) 
           {
               kid2.Status__c = 'Active';
               childrenToUpdate.add(kid2);
           }
{
        }}
       if( !childrenToUpdate.isEmpty())
       {
            update childrenToUpdate;
       }
}}
}

Thanks in advance for your help!
Best Answer chosen by Stephanie Zimmerman 7
Jim JamJim Jam
in your query for updatedParents you need to include the Draw_4__c field, also in your if statements = should be == ie. if (p.Draw_4__c == true) ... if (kid2.status__c == 'Active)

All Answers

Jim JamJim Jam
in your query for updatedParents you need to include the Draw_4__c field, also in your if statements = should be == ie. if (p.Draw_4__c == true) ... if (kid2.status__c == 'Active)
This was selected as the best answer
Stephanie Zimmerman 7Stephanie Zimmerman 7
Thanks, Jim. I realized the problem was not including Draw_4__c and Project_Status__c in the query.