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
Alan Monk 10Alan Monk 10 

Case Grade formula field

I am trying to create a formula field from values from  a picklist field. I am new to formulas so need some help. Basically I want to display a Letter in the field that match a certain value alone these lines.

IF(ISPICKVAL( Quality_Grade__c , "A = no errors"), 'A', 
         IF(ISPICKVAL(Quality_Grade__c,"B = 1 minor"), 'B',
         IF(ISPICKVAL(Quality_Grade__c,"C = 2 minors"), 'C',
         IF(ISPICKVAL(Quality_Grade__c,"D = 3 minors"), 'D'
         IF(ISPICKVAL(Quality_Grade__c, "E = 4 minors OR 1 or more severes"), 'E'))
 
Best Answer chosen by Alan Monk 10
Alain CabonAlain Cabon
Is there always a default value nevertheless (A = no errors) ? ( test for the null value otherwise will be mandatory )

IF ( ISBLANK( TEXT(Quality_Grade__c)), 'A', LEFT( TEXT( Quality_Grade__c) , 1))

Regards
 

All Answers

Alain CabonAlain Cabon
Hi,

Could you post all the exact values of your picklist?   (labels: "no errors", "1 minor" ... "4 minors" and "1 or more severes"  only?) 

Regards
Alan Monk 10Alan Monk 10
Hi Alain, below are the exact picklist values

A = no errors
B = 1 minor
C = 2 minors
D = 3 minors
E = 4 minors OR 1 or more severes
Alain CabonAlain Cabon
Ok, so we can extract the first letter direcly (there is no "-- none --" "-- default--" and so on);

LEFT( TEXT( Quality_Grade__c) , 1)

Regards
Alain CabonAlain Cabon
Is there always a default value nevertheless (A = no errors) ? ( test for the null value otherwise will be mandatory )

IF ( ISBLANK( TEXT(Quality_Grade__c)), 'A', LEFT( TEXT( Quality_Grade__c) , 1))

Regards
 
This was selected as the best answer
Alan Monk 10Alan Monk 10
Thanks for your help Alain, I have managed create a formula thatseems to work

CASE ( Text (Quality_Grade__c ),
                                "A = no errors", "A",
                                "B = 1 minor", "B",
                                "C = 2 minors", "C",
                                "D = 3 minors", "D",
                                "E = 4 minors OR 1 or more severes","E",
                                null )
.
Alain CabonAlain Cabon
Excellent. We often prefer a simple case instead of nested "if" particularly. In fact, I had asked for the exact values of your picklist because your question seemed to be already a solution (that's why perhaps other people didn't react). 

ISPICKVAL and TEXT are often interchangeable for a picklist but we prefer TEXT according the last documentation.

Tips: Picklist fields are supported in TEXT functions used in these kinds of formulas: validation rules, approval rules, approval step rules, workflow rules, escalation rules, assignment rules, auto-response rules, formula fields, field updates, and custom buttons and links.
In other formulas, use ISPICKVAL or CASE when referencing a picklist field.

https://help.salesforce.com/articleView?id=customize_functions_i_z.htm&language=en&type=0

TEXT and CASE are the good practices.

Thank you for the points and with the next one.