+ Start a Discussion
HNT_NeoHNT_Neo 

Apex skip dropdown when certain value has been selected

I have an Apex trigger setup to check the Forecast checkbox to true, and updater or insert the Company Name to BPHS. 

What should my syntax look like if I don't want this trigger to update a picklist named, License__c with the value of Distribution

I want to keep the syntax I have, but if Distribution is selected, I need the syntax to skip that record. 

Here is my current syntax: 
trigger ForceForecasting on User (before update, before insert) {
    for (User userInLoop : Trigger.new) {
        userInLoop.ForecastEnabled = true;
        userInLoop.CompanyName = 'BPHS';
    }
}

Thank you in advance!
Best Answer chosen by HNT_Neo
VineetKumarVineetKumar
Something like this :
trigger ForceForecasting on User (before update, before insert) {
    for (User userInLoop : Trigger.new) {
		if(userInLoop.License__c != null && !userInLoop.License__c.equalsIgnoreCase('Distribution')){
			userInLoop.ForecastEnabled = true;
			userInLoop.CompanyName = 'BPHS';
		}
    }
}

All Answers

VineetKumarVineetKumar
Something like this :
trigger ForceForecasting on User (before update, before insert) {
    for (User userInLoop : Trigger.new) {
		if(userInLoop.License__c != null && !userInLoop.License__c.equalsIgnoreCase('Distribution')){
			userInLoop.ForecastEnabled = true;
			userInLoop.CompanyName = 'BPHS';
		}
    }
}
This was selected as the best answer
Tavva Sai KrishnaTavva Sai Krishna
Hi JH_Neo,
As per my understanding, LIcense__c picklist field on the User Object needs to be updated to the value as "Distribution" only if the field value is not "Distribution". 
 
trigger ForceForecasting on User (before update, before insert) {
    for (User userInLoop : Trigger.new) {
        userInLoop.ForecastEnabled = true;
        userInLoop.CompanyName = 'BPHS';
         if(userInLoop.License__c  != 'Distribution')
               userInLoop.License__c = 'Distribution';
    }
}

Let me know if you have any queries. Also please mark this answer as best answer if it answers to your question as it may helpful to others.
Regards,
Sai Krishna Tavva
HNT_NeoHNT_Neo
Thank you VineetK! This did the trick. 

Tavva, I will also take your solution into consideration if a value when the field is Null.