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
Sree07Sree07 

Trigger to update picklist value based on account name

I have a requirement which is
If account name field in account object contains any value from industry field(picklist),the word contained in account name field should be updated in industry picklist using apex. how can i do that?Can u please help me with this requirement.I have written the following code:


trigger updateIndustryFieldTrigger on Account (before update) {
    List<Account> accList = [SELECT Id,Name FROM Account];
    for(Account acc : Trigger.New){
        for(Account a : acc.Industry){
            if(acc.Name.contains(acc.Industry)){
                acc.Industry += acc.Industry;
            }else{
                acc.Industry = 'Other';
            }
        }
    }
}
Best Answer chosen by Sree07
Suraj Tripathi 47Suraj Tripathi 47
Hello Sree,

trigger updateIndustryFieldTrigger on Account (before update) {
if(Trigger.isupdate && trigger.isbefore)
{
for(Account acc : Trigger.New){
Schema.DescribeFieldResult fieldResult = Account.industry.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
if(acc.Name.contains(pickListVal.getLabel()))
{
acc.Industry=pickListVal.getLabel();
}
}
}
}
}



I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hello Sree,

trigger updateIndustryFieldTrigger on Account (before update) {
if(Trigger.isupdate && trigger.isbefore)
{
for(Account acc : Trigger.New){
Schema.DescribeFieldResult fieldResult = Account.industry.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
if(acc.Name.contains(pickListVal.getLabel()))
{
acc.Industry=pickListVal.getLabel();
}
}
}
}
}



I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi
This was selected as the best answer
Sree07Sree07
@Suraj Tripathi Can I get more simpler one like alternative solution for this?