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
pvandepvande 

Need help with IF clause on trigger

Please help me add a line of code to my trigger.  I don't want the trigger to run if Date_Pushed_to_Production__c is populated.  This field resides on the custom object Project__c.  Thank you for looking at my question.  
Here is the trigger:


Trigger MoveToProduction on Opportunity (before insert, before update){

   List<ID> ProjIds = New List<ID>();

  for(Opportunity o : Trigger.new){
    if(o.Move_to_Production__c == true && o.RecordTypeId == '012a0000001FqTn'){
    
      ProjIds.add(o.Project__c);
  }

  List<Project__c> ProjList = [SELECT id, Move_to_ProductionP__c, Date_Pushed_to_Production__c FROM Project__c WHERE id in :ProjIds];
  for(integer i = 0 ; i < ProjList.size(); i++){
    If(ProjList[i].Date_Pushed_to_Production__c == null)
     
     ProjList[i].Move_to_ProductionP__c = true;
       ProjList[i]. Date_Pushed_to_Production__c = system.today();
      
  update ProjList;
Best Answer chosen by pvande
Glyn Anderson 3Glyn Anderson 3
Richie has the right answer.  I'd like to share a couple techniques that will help to avoid duplicating code between insert and update blocks, and use some other best practices.

<pre>
// first, move to after insert, after update, since we are not modifying the Opportunity object
trigger MoveToProduction on Opportunity (after insert, after update)
{
    // query record type so we don't have to hard-code RecordType IDs
    RecordType oppRT =
    [   SELECT  Id
        FROM    RecordType
        WHERE   sObjectType = 'Opportunity' AND DeveloperName = 'Desired_Record_Type'
    ];

    // use a Set because multiple Opportunities could look up to the same Project
    Set<Id> projIds = new Set<Id>();

    for ( Opportunity opp : Trigger.new )
    {
        // get the old version of the record - null if insert
        Opportunity oldOpp = Trigger.isUpdate ? Trigger.oldMap.get( opp.Id ) : null;

        if  (   opp.Move_to_Production__c  // it's boolean - no need to compare to true
            &&  (oldOpp == null || !oldOpp.Move_to_Production__c)
            &&  opp.RecordTypeId == oppRt.Id
            )
        {
            projIds.add( opp.Project__c );
        }
    }

    if ( !prjIds.isEmpty() )
    {
        // exclude Projects with non-null Date_Pushed_to_Production__c
        // since we're not going to update those
        List<Project__c> projList =
            [   SELECT  Id, Move_to_ProductionP__c, Date_Pushed_to_Production__c
                FROM    Project__c
                WHERE   Id in :projIds AND Date_Pushed_to_Production__c = null
            ];

        for ( Project__c proj : projList )
        {
            proj.Move_to_ProductionP__c = true;
            proj.Date_Pushed_to_Production__c = System.today();
        }
        update projList;
    }
}
</pre>

All Answers

Richie LandinghamRichie Landingham
Hello!,

You are already accounting for it to not run at the end on the 
" If(ProjList[i].Date_Pushed_to_Production__c == null)",

If you don't want any of it to run, it would require a little bit more changing of the total code as seen below.
 
Trigger MoveToProduction on Opportunity (before insert, before update){

   List<ID> ProjIds = New List<ID>();

 if (Trigger.isUpdate && Trigger.isBefore) {
  for(Opportunity o : Trigger.new){
    if(o.Move_to_Production__c == true && trigger.oldmap.get(o.id).Move_To_Production__c == false o.RecordTypeId == '012a0000001FqTn'){
      ProjIds.add(o.Project__c);
  }

  if (Trigger.isInsert && Trigger.isBefore){
    if(o.Move_to_Production__c == true && o.RecordTypeId == '012a0000001FqTn'){
      ProjIds.add(o.Project__c);
    }
  }

  if (!PrjIds.isempty()){
    List<Project__c> ProjList = [SELECT id, Move_to_ProductionP__c, Date_Pushed_to_Production__c FROM Project__c WHERE id in :ProjIds];
    for(integer i = 0 ; i < ProjList.size(); i++){
      If(ProjList[i].Date_Pushed_to_Production__c == null)
       
       ProjList[i].Move_to_ProductionP__c = true;
         ProjList[i]. Date_Pushed_to_Production__c = system.today();
        
    update ProjList;
  }

What I am doing here is checking if the opporutnitys "Move_To_Production__c" went from false to true on the update, instead of always causing it to add to the list if it was ever true.

Then I only execute the bottom update onto the Project__c if the list is not empty.
Glyn Anderson 3Glyn Anderson 3
Richie has the right answer.  I'd like to share a couple techniques that will help to avoid duplicating code between insert and update blocks, and use some other best practices.

<pre>
// first, move to after insert, after update, since we are not modifying the Opportunity object
trigger MoveToProduction on Opportunity (after insert, after update)
{
    // query record type so we don't have to hard-code RecordType IDs
    RecordType oppRT =
    [   SELECT  Id
        FROM    RecordType
        WHERE   sObjectType = 'Opportunity' AND DeveloperName = 'Desired_Record_Type'
    ];

    // use a Set because multiple Opportunities could look up to the same Project
    Set<Id> projIds = new Set<Id>();

    for ( Opportunity opp : Trigger.new )
    {
        // get the old version of the record - null if insert
        Opportunity oldOpp = Trigger.isUpdate ? Trigger.oldMap.get( opp.Id ) : null;

        if  (   opp.Move_to_Production__c  // it's boolean - no need to compare to true
            &&  (oldOpp == null || !oldOpp.Move_to_Production__c)
            &&  opp.RecordTypeId == oppRt.Id
            )
        {
            projIds.add( opp.Project__c );
        }
    }

    if ( !prjIds.isEmpty() )
    {
        // exclude Projects with non-null Date_Pushed_to_Production__c
        // since we're not going to update those
        List<Project__c> projList =
            [   SELECT  Id, Move_to_ProductionP__c, Date_Pushed_to_Production__c
                FROM    Project__c
                WHERE   Id in :projIds AND Date_Pushed_to_Production__c = null
            ];

        for ( Project__c proj : projList )
        {
            proj.Move_to_ProductionP__c = true;
            proj.Date_Pushed_to_Production__c = System.today();
        }
        update projList;
    }
}
</pre>
This was selected as the best answer
Glyn Anderson 3Glyn Anderson 3
pvande,  Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved the problam another way, please post your solution for everyone's benefit.  Thanks!
pvandepvande
Richie Landingham and Glyn  Anderson 3 - thank you both for your help with this issue.