0

Have the following code which populates field Code__c where null

List<Account> accounts = [
                    SELECT
                        Id, 
                        Code__c 
                    FROM 
                        Account 
                    WHERE 
                        Code__c = : null
                    LIMIT 4000
            ];
System.debug('accounts: ' + accounts);
for(Account acc : accounts){
    acc.Code__c = 'Some_Code__c';
}
update accounts;

After running I look at the account records that have been updated and they are showing the API Value 'Some_Code__c' instead of the Label Value 'Some Code'

How would i get the record to show the Label Value instead of the API Value?

  • 3
    in your example, what is Some_Code__c supposed to represent? As written, it's just a string of text and so that's what's getting stored in the Code__c field. – Mark Pond Jan 16 at 22:53
  • Instead of acc.Code__c = 'Some_Code__c' you can do acc.Code__c = 'Some Code' and that change in literal would store the label but you should avoid hardcoding like this so use following instead \n SELECT toLabel(CUSTOM_PICK_LIST_COL__C) FROM YOUROBJ_C – Mitesh Jan 16 at 23:12
  • @Mitesh The issue is i can only store a string of text that matches the API Value, in this case 'Some_Code__c'. I want to be able to store the Label Value 'Some Code' but when I try acc.Code__c = 'Some Code' i get an error saying not a picklist value – John Hernandez Jan 17 at 14:59
0

Your code is literally putting the string of text 'Some_Code__c' into the field value because it is surrounded by single quotes. The other comments are correct; you'll need to either change the string to the desired 'Some Code', or query for a desired picklist value from the database.

| improve this answer | |
  • Are you able to provide an example of the code? – John Hernandez Jan 17 at 17:20
  • You would just use the literal string 'Some Code' with no underscores. It's technically possible for a picklist to have a different label versus the api value, but it's very rare. (IE: I haven't seen it and I've logged on to roughly 150 orgs.) – JimG Jan 29 at 19:54

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.