I'm starting to learn Apex and work 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:
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)
{
kid.Send_Email__c = True;
kid.Status__c = p.Project_Stage__c;
childrenToUpdate.add(kid);
}
}
else {
for(Referral__c kid2 : p.Referred_By__r)
{
kid2.Status__c = p.Project_Stage__c;
childrenToUpdate.add(kid2);
}
{ }} if( !childrenToUpdate.isEmpty()) { update childrenToUpdate; } }} }
Thanks in advance for your help!