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
GV1GV1 

Need help: field updating three level down in a trigger

I have four objects

Opportunity--->>>Lookup>>Playbook ---->>>>Master detail>>>Stage->>>>master detail>> Substage
 
playbook already have a few entries of stages. And each stage already has a few entries of substage value.
 
When we select a playbook value for Opportunity (Opportunity.Playbook__c), I need to update two other fields of Opportunity object.
 
 
First Field: out of many child (in Stage object) of the selected playbook I need to chose the Stage name of the record whose 'Sort Order' is 1; and assign that value to Opportunity.CurrentStage__c
 
Second field : out of many child (in SubStage object) of the above Stage I need to chose the SubStage name of the record whose 'Sort Order' is 1; and assign that value to Opportunity.CurrentSUBStage__c
 
I have to write a trigger to do this. Kindly help me with the code.
 
I have started with it as below. but not sure how to proceed further. Can someone kindly help.
 
if(Trigger.isUpdate && Trigger.isAfter) {
        for(Opportunity opp : Trigger.new) {
            if((opp.Playbook__c != null && opp.Playbook__c != Trigger.oldMap.get(opp.Id).Playbook__c)
Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
You need to write query something like
Select id from Stage__c where Paybook__c =: Opportunity.PalayBook__c and Sort_Order__c = 1

All Answers

sushant sussushant sus
you have to write three trigger for this for all three object

then u can achieve ur requirement
GV1GV1

Three triggers are not required.

 

playbook already have a few entries of stages. And each stage already has a few records of substage value.

Bhawani SharmaBhawani Sharma
You need to write query something like
Select id from Stage__c where Paybook__c =: Opportunity.PalayBook__c and Sort_Order__c = 1
This was selected as the best answer
GV1GV1

 


Bhawani Sharma wrote:
You need to write query something like
Select id from Stage__c where Paybook__c =: Opportunity.PalayBook__c and Sort_Order__c = 1

Thanks bhawani for the answer. That exactly what i guessed.


How will i select the record from Substage is more tricky for me.!!

Bhawani SharmaBhawani Sharma
Select id from SubStage where Stage__r.Paybook__c =: Opportunity.PalayBook__c and Sort_Order__c = 1
GV1GV1
Thanks Soo much
GV1GV1

Hi Bhawani,

I have written the following code:

 

if(Trigger.isUpdate && Trigger.isAfter) {
        for(Opportunity opp : Trigger.new) {
            if(opp.Playbook__c != null && opp.Playbook__c != Trigger.oldMap.get(opp.Id).Playbook__c) {
Error>  opp.StageName = [Select id from SubStage__c where Stage__r.Playbook__c =: Opp.Playbook__c and Substage_Sort_Order__c = 1];
            }
        }
     }  

 

I am getting this Error: Illegal assignment from LIST<Substage__c> to String

 

 

Bhawani SharmaBhawani Sharma
where Stage__r.Playbook__c = Opp.Playbook__c, you are missing colon(:) here. Change it to
where Stage__r.Playbook__c =: Opp.Playbook__c

Apart from that,
opp.StageName = [Select id from SubStage__c where Stage__r.Playbook__c = Opp.Playbook__c and Substage_Sort_Order__c = 1];

This SOQL will return a SubStage__c object, so you will have to hold that in in same type like
SubStage__c ss = YOUR query