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
Alexander G GoodmanAlexander G Goodman 

Creating an If statement where the critia comes from a Picklist

I have Custom code for a trigger which I am trying to get it to only activate if a pick list field is selected. The code needsto be Triggered from a If Statement. 

This is the original Code:
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
   // get the ids of all the accounts
     Set<Id> accIds=new Set<Id>();
   for (Opportunity opp : trigger.new)
 {
     accIds.add(opp.AccountId);
 }

   // retrieve the accounts and owners
  List<Account> accs=[select id, OwnerId from Account where id in :accIds];
  Map<Id, Account> accById=new Map<Id, Account>();
  accById.putall(accs);

   // iterate the opportunities, updating the opportunity owner to the account owner
   for (Opportunity opp : trigger.new)
  {
       // any logic about when to do this - i.e. only if the current owner matches a
       // particular id goes here
      if (null!=opp.AccountId)
       { 
         opp.OwnerId=accById.get(opp.AccountId).OwnerId;
              
       }  }
}
Any help would be apprecated.
Note: I can not give the field name of the picklist.
sandeep sankhlasandeep sankhla
Hey Alex,

What exactly you want to do in this trigger ?

if opportunity.AccountId != null then you want to updaet the opportunity ownerId with related account ownerId is it ? 
so what is the use of picklist ..

Thanks
ManojjenaManojjena
Hi  Alexander,
Assume that you want to fire that cod eif the opportunity stageName have some value .
You can try like if(opp.StageName != null) 
or if(opp.StageName= ='Closed Own') 

I think it will help any issue let me know .
 
Alexander G GoodmanAlexander G Goodman
In short, I would like  this trigger to only work when a certain picklist selection is selected and then when the account owner is changed, the opportunity owners are not chaged with it.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Alex ,

Please try below code with your Opportunity field.

trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{

    
   // get the ids of all the accounts
     Set<Id> accIds=new Set<Id>();
   for (Opportunity opp : trigger.new)
 {
    if(opp.stage =='Closed Won'){
        accIds.add(opp.AccountId);
    }
     
 }

   // retrieve the accounts and owners
  List<Account> accs=[select id, OwnerId from Account where id in :accIds];
  Map<Id, Account> accById=new Map<Id, Account>();
  accById.putall(accs);

   // iterate the opportunities, updating the opportunity owner to the account owner
   for (Opportunity opp : trigger.new)
   {
    //I would like  this trigger to only work when a certain picklist selection is selected and then when the account owner is changed, the opportunity owners are not chaged with it.
       if(opp.stage =='Closed Won' && opp.OwnerId != accById.get(opp.AccountId).OwnerId){
         opp.OwnerId=accById.get(opp.AccountId).OwnerId;
    }
   }
      
}



 
Alexander G GoodmanAlexander G Goodman
I tried that and it does not work it errors on the custom field change and it takes the oppertunitys with it.
Alexander G GoodmanAlexander G Goodman
This is the corrent copy minus sensitive system info
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
   // get the ids of all the accounts
     Set<Id> accIds=new Set<Id>();
   for (Opportunity opp : trigger.new)
 {
     accIds.add(opp.AccountId);
 }

   // retrieve the accounts and owners
  List<Account> accs=[select id, OwnerId from Account where id in :accIds];
  Map<Id, Account> accById=new Map<Id, Account>();
  accById.putall(accs);

   // iterate the opportunities, updating the opportunity owner to the account owner
   for (Opportunity opp : trigger.new)
  {
       // any logic about when to do this - i.e. only if the current owner matches a
       // particular id goes here
      if (opp.****_*****__c == '****')
       { 
         opp.OwnerId=accById.get(opp.AccountId).OwnerId;
              
       }  }
}
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Alex,
What is exacxt error that you are getting ? Can  you paste screenshot ?
Alexander G GoodmanAlexander G Goodman
I can't paste a screenshot but I can paste the error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger OpportunityAutoUpdate caused an unexpected exception, contact your administrator: OpportunityAutoUpdate: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityAutoUpdate: line 24, column 1
Alexander G GoodmanAlexander G Goodman
Also would it be possable to rewrite this as a class which is triggered by the process builder?
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Alex,
// Filter opportunity here which meets your criteria on the very second block of your code.
So you will have all accounts in map where related opportunity field value has changed.

 for (Opportunity opp : trigger.new)
 {
 if( opp.stage =='Closed Won'){
   accIds.add(opp.AccountId);
  }
     
 }

And yes ,you can transfer trigger logic to a utility class which can be triggered from process builder.