0

I have a picklist with values as : 'abc1', 'abc2', 'abc3' and 'others'. Now when Lead is created using data loader with picklist value abc4 then i want to set that picklist value to 'others'.

What is best way to achieve this? Can i do it using workflow?

I wrote a trigger to do this but i am still getting error that "bad value for restricted picklist field: abc4"

My Trigger:

List<Schema.PicklistEntry> picklistValues = Lead.fields.Lead_City__c.getDescribe().getpicklistvalues();
set<string> LeadCityValues = new set<string>(); 
for(Schema.PicklistEntry value : picklistValues){
   LeadCityValues.add(value.getLabel());
}

for( Lead ld : Trigger.new ) {  if( ld.Lead_City__c != null && !(LeadCityValues.contains(ld.Lead_City__c)) ){
       ld.Other_Lead_City__c = ld.Lead_City__c;
       ld.Lead_City__c = 'Others';  } }
  • Are you using a before trigger? – Girbot Feb 19 '17 at 13:05
  • Yes..I am using before insert/ before update – Dnyaneshwar Feb 19 '17 at 13:54
1

You have "Restrict picklist to the values defined in the value set." for this field. If you wish to allow incorrect values to be passed to this field you will need to uncheck this option.

Be careful, this may lead to corrupt data further down the line hence why SF impose this option by default.

Once unchecked, your trigger will manipulate the field data accordingly.

  • I can not uncheck it...but i don't want to save values other than specified but when user insert/ update a record with wrong value i just want trigger to correct it and set value 'Others' (which is one of the values of picklist.) ! Is there no way i can do this? What is wrong with my code? – Dnyaneshwar Feb 19 '17 at 14:57
  • There is nothing wrong with your code but unfortunately, your trigger does not get a chance to change the value before insert/update due to the DML exception. – TSmith Feb 19 '17 at 15:14
  • Any workaround other than unchecking the checkbox? Thanks a lot for reply :) – Dnyaneshwar Feb 19 '17 at 15:28
  • @Dnyaneshwar Two options besides allowing bad data in the picklist - 1. Modify your Data Loader script to do this checking rather than doing it in the trigger, 2. Create a second field (probably just a plain text field) for data loader to dump the value, and in the trigger take the value from that field, change it if necessary, and assign it to the picklist. – IllusiveBrian Feb 19 '17 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.