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
ashu 6112ashu 6112 

Trigger Related Task

I have one custom object, there are two fields, one multiselect picklist and second Text Field.
 
In multiselect picklist, there are 5 values : Mango, Apple, Orange, Guava and Grapes.
 
The requirement is that if I select Mango in picklist, then It needs to show Yellow Color fruit in the second text field.
If I select Apple in picklist , then it must show Red Color fruit in the second text field..
If I select Mango and Orange, then it should show Yellow Color Fruit, red Color Fruit in the second text field.
 
I need to write a trigger for this. Please help how to acheive the same.

Do I need to store these values, "Yellow Color Fruit", "red Color Fruit" somewhere in the same object....
Best Answer chosen by ashu 6112
Chris Gary CloudPerformerChris Gary CloudPerformer
trigger CustomTrigger on <SObjectName> (before insert, before update){
    Map<String,String> fruitToColorMap = new Map<String,String>(){
                                                                      'Apple'=>'Red Color Fruit',
                                                                      'Mango'=>'Red Color Fruit',
                                                                      'Orango'=>'Yello Color Fruit'
                                                                  };
    for(<sObjectName> sobj:Trigger.new){
        if(String.isNotBlank(sObj.<picklistName>)){
            String secondTextFieldValue  = '';
            List<String> picklistValues = sObj.<picklistName>.split(';');
            for(Integer i=0;i<picklistValues;i++){
                if(fruitToColotMap.containsKey(picklistValues.get(i))){
                    secondTextFieldValue += fruitToColorMap.get(picklistValues.get(i));
                    if(i>picklistValues.size()-1) secondTextFieldValue += ', ';
                }
            }
            sobj.<secondTextFieldName> = secondTextFieldValue;
        }
    }
}

Yes, basically you put the Fruits and colors in a map. As you loop through the records passed into the trigger, you build the text string based on the fruits selected in the picklist.  These values are stored separated by a semicolon(;) so you have to split the string by the semicolon, and as you examine each part of the picklist string, you simply build the second Text string based on what you find.. Once that's all done, you can insert the text String you built into the value of the second text field. If this a 'before' trigger, no insert statement needed.

All Answers

Chris Gary CloudPerformerChris Gary CloudPerformer
trigger CustomTrigger on <SObjectName> (before insert, before update){
    Map<String,String> fruitToColorMap = new Map<String,String>(){
                                                                      'Apple'=>'Red Color Fruit',
                                                                      'Mango'=>'Red Color Fruit',
                                                                      'Orango'=>'Yello Color Fruit'
                                                                  };
    for(<sObjectName> sobj:Trigger.new){
        if(String.isNotBlank(sObj.<picklistName>)){
            String secondTextFieldValue  = '';
            List<String> picklistValues = sObj.<picklistName>.split(';');
            for(Integer i=0;i<picklistValues;i++){
                if(fruitToColotMap.containsKey(picklistValues.get(i))){
                    secondTextFieldValue += fruitToColorMap.get(picklistValues.get(i));
                    if(i>picklistValues.size()-1) secondTextFieldValue += ', ';
                }
            }
            sobj.<secondTextFieldName> = secondTextFieldValue;
        }
    }
}

Yes, basically you put the Fruits and colors in a map. As you loop through the records passed into the trigger, you build the text string based on the fruits selected in the picklist.  These values are stored separated by a semicolon(;) so you have to split the string by the semicolon, and as you examine each part of the picklist string, you simply build the second Text string based on what you find.. Once that's all done, you can insert the text String you built into the value of the second text field. If this a 'before' trigger, no insert statement needed.
This was selected as the best answer
ashu 6112ashu 6112
Hi Chris,

Thanks for this but when I am trying to save this trigger, there is an error message that stops it to save. Error message is "Invalid identifier: secondTextFieldValue at line 09 column 20". I put the line number according to your code.

Please suggest.
Chris Gary CloudPerformerChris Gary CloudPerformer
The code I provided was an example for guidance on how to solve your problem.  I did not put the actual API names of the fields in the code because they were not provided in your original question.  It was not meant to be a copy and paste solution, but rather 'here's an example of how you do it.' You would need to replace the items between the '<>' with the necessary field names for this to actually work. Apologies for any confusion.