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
Kumar Rao 1Kumar Rao 1 

Picklist value check - If statement

Dear Folks,

I'm new bee to salesforce and please help to get me a sample code if possible, as I didn't find it anywhere.

Requirement: I have picklist filed with 2 values (val 1, Val2)
Scenario (trigger):

If (picklist value = 'Val1') {
 code here...
}

But I encounter bellow error, can you please suggest.
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 5 column 12
 
Best Answer chosen by Kumar Rao 1
Vishal Negandhi 16Vishal Negandhi 16
So let's assume on Account you have a picklist field called Account_Type__c and it has values "Big" and "Small".
Your code should be something like
trigger AccountTrigger on Account(before insert){
     for(Account acnt : trigger.new){
           if(acnt.Account_Type__c == 'Big'){
                acnt.Big_Account__c = true;
           }
           else{
                acnt.Big_Account__c = false;
           } 
     }
}
So here, when a new Account is created I am checking if the Account type is "big", if yes then I am setting a checkbox field "Big_Account__c" to true. 

Note: the field names should be the API Names and not labels. 
So for me Account Type is a label, whereas the API Name is Account_Type__c.

Hope this helps :)
 

All Answers

AnjithKumarAnjithKumar
Hello Kumar,

You have to compare with '==' or using string methods

IF(obj.picklistfield=='val1'){

}

OR 

IF(obj.picklistfield.equals('val1)){

}

Please share the actual IF statement so that I can help you.

Thanks,
Anjith
Vishal Negandhi 16Vishal Negandhi 16
So let's assume on Account you have a picklist field called Account_Type__c and it has values "Big" and "Small".
Your code should be something like
trigger AccountTrigger on Account(before insert){
     for(Account acnt : trigger.new){
           if(acnt.Account_Type__c == 'Big'){
                acnt.Big_Account__c = true;
           }
           else{
                acnt.Big_Account__c = false;
           } 
     }
}
So here, when a new Account is created I am checking if the Account type is "big", if yes then I am setting a checkbox field "Big_Account__c" to true. 

Note: the field names should be the API Names and not labels. 
So for me Account Type is a label, whereas the API Name is Account_Type__c.

Hope this helps :)
 
This was selected as the best answer
Kumar Rao 1Kumar Rao 1
Here is my code on a custom object.

trigger Fund on Parthiv__Fund__c(Before insert, after Insert, before update, After update){
if(Trigger.isbefore){
    if(Trigger.isInsert){    
    for(Parthiv__Fund__c fund : trigger.new){
//        if(Parthiv__Fund__c.Parthiv__Fund_Split__c == 'value 1'){
        
//        }
    }    
    }
 }
}
Kumar Rao 1Kumar Rao 1
Thanks a lot Vishal & Anjith
Vishal Negandhi 16Vishal Negandhi 16
correction on the if condition line:
if(fund.Parthiv__Fund_Split__c == 'value 1'){


 
Krishna SambarajuKrishna Sambaraju
Here is a sample trigger.
trigger myTrigger on myObject (before insert, before update)
{
      for (myObject obj : trigger.new)
      {
              if (obj.picklistField == 'val1')
              {
                   //do something here
              }
               if (obj.picklistField == 'val2')
              {
                   //do something here
              }
      }
}
Change the object name (myObject) accordingly. In the above code before insert and before update are the events when you want the trigger to fire. If you can share the trigger code, I can help you with it. 
SANTOSHKUMAR TEMBHARESANTOSHKUMAR TEMBHARE
#Here is one more example for Checking 'Picklist' value in If statement and setting it.
Here I am setting Ownership as 'Public' and Rating as 'Hot' based on Account Industry selection.
All 3 fields (Industry/Ownership/Rating) are Picklist values. 

trigger BankingTriggerOnAccount on Account (before insert){    
    for (Account ac : Trigger.new){
        if (ac.Industry.equals('Banking')){
            ac.Ownership = 'Public';
            ac.Rating = 'Hot';            
        }           
    }
}