You need to sign in to do that
Don't have an account?
Sree07
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';
}
}
}
}
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';
}
}
}
}
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
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