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
imishraimishra 

Update picklist value from Parent-child

Hi,
I have a master detail relation between Opportunity and object__c(custom object)
I have same stage fields as in opportunity in object__c. 
I want to update the stage value from opportunity to object__c only when the object__c record is created. After that the object__c record can have its own stage values different from Opportunity.

I have written the below trigger, but it updates the object__c record everytime there is some update on opportunity, where as i want it to fire only once when the object record is created.

trigger updateLineItemValues on Opportunity (before update){
    
    Set<ID> oppids = new Set<ID>();
   
    for(Opportunity opp : Trigger.new){
       
        if(opp.Line_Item__c > 0){
            oppids.add(opp.Id);
        }
    }
    List<Opportunity> updatedOpps = [SELECT Id, StageName, Status__c,Partner_Intermediary_PE_Firm_Involved__c, (Select Id, Stage__c,Partner_Intermediary_Involvement__c,Status__c from Opportunity_Service_Line_Items__r)  FROM Opportunity WHERE Id in :oppids];

    List<Opportunity_Service_Line_Item__c> relatedDealsToUpdate = new List<Opportunity_Service_Line_Item__c>();
   
    for (Opportunity opp : updatedOpps){
       
        for(Opportunity_Service_Line_Item__c rd : opp.Opportunity_Service_Line_Items__r){
            rd.Stage__c = opp.StageName;
            rd.Status__c = opp.Status__c;
            rd.Partner_Intermediary_Involvement__c = opp.Partner_Intermediary_PE_Firm_Involved__c;
            relatedDealsToUpdate.add(rd);
        }
    }
    update relatedDealsToUpdate;
}


Please let me know how can i get this trigger fire only once when the object__c record is inserted.

Thanks
Best Answer chosen by imishra
Sure@DreamSure@Dream
Hi,


Why are you writing the trigger on Opportunity. You need to write it on Object__c.

You can do like below:
trigger sample on Object__c(before Insert)
{

  Set<Id> setOfIds=new Set<ID>();
  for(Object__c obj:Trigger.new)
{
  setOfIds.add(obj.Opportunity__c);
}

  Map<Id,Opportunity> mapOfIdStage=new map<Id,String>([SELECT id,Stage FROM Opportunity WHERE Id IN :setOfIds]);

  for(Object__c obj:Trigger.new)
{
  obj.Stage__c=mapOfIdStage.get(obj.Opportunity__c).Stage;
}
}

All Answers

SFDC_DevloperSFDC_Devloper
Hi Imishra,

Add a static boolean variable to a class, and check its value within the affected triggers.

Example Code Here:

public class HelperClass {
   public static boolean firstRun = true;
}

trigger affectedTrigger on Account (before delete, after delete, after undelete) {
    if(Trigger.isBefore){
        if(Trigger.isDelete){
            if(HelperClass.firstRun){
                Trigger.old[0].addError('Before Account Delete Error');
                HelperClass.firstRun=false;
            }
        }
    }
}



Thanks,
Rockzz
SFDC_DevloperSFDC_Devloper
Hi Imishra,

try below Code...

Controller:
public class HelperClass {
   public static boolean firstRun = true;
}

Trigger:

trigger updateLineItemValues on Opportunity (before update){
   
    Set<ID> oppids = new Set<ID>();
  
    for(Opportunity opp : Trigger.new){
      
        if(opp.Line_Item__c > 0){
            oppids.add(opp.Id);
        }
    }
    List<Opportunity> updatedOpps = [SELECT Id, StageName, Status__c,Partner_Intermediary_PE_Firm_Involved__c, (Select Id, Stage__c,Partner_Intermediary_Involvement__c,Status__c from Opportunity_Service_Line_Items__r)  FROM Opportunity WHERE Id in :oppids];

    List<Opportunity_Service_Line_Item__c> relatedDealsToUpdate = new List<Opportunity_Service_Line_Item__c>();
  
     if(HelperClass.firstRun){
    for (Opportunity opp : updatedOpps){
        
        for(Opportunity_Service_Line_Item__c rd : opp.Opportunity_Service_Line_Items__r){
    
            rd.Stage__c = opp.StageName;
            rd.Status__c = opp.Status__c;
            rd.Partner_Intermediary_Involvement__c = opp.Partner_Intermediary_PE_Firm_Involved__c;
            relatedDealsToUpdate.add(rd);
        }
    }
    update relatedDealsToUpdate;
  HelperClass.firstRun=false;
  }
}
}

Thanks,
Rockzz


Sure@DreamSure@Dream
Hi,


Why are you writing the trigger on Opportunity. You need to write it on Object__c.

You can do like below:
trigger sample on Object__c(before Insert)
{

  Set<Id> setOfIds=new Set<ID>();
  for(Object__c obj:Trigger.new)
{
  setOfIds.add(obj.Opportunity__c);
}

  Map<Id,Opportunity> mapOfIdStage=new map<Id,String>([SELECT id,Stage FROM Opportunity WHERE Id IN :setOfIds]);

  for(Object__c obj:Trigger.new)
{
  obj.Stage__c=mapOfIdStage.get(obj.Opportunity__c).Stage;
}
}
This was selected as the best answer
imishraimishra
Thanks..
It worked.