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
ChrisbrisbaneChrisbrisbane 

Apex Trigger to copy picklist value to a multi select picklist

Hello,

 

I am  new to Apex and I am trying to write a trigger.

 

In the User object I have a field called Business_Stream__c which is a picklist.

 

In the Account object I havea field called Business_Stream__c which is a multi select picklist and is a required field.

 

On creation of the account record I would like the Business_Stream__c field in Account to be the same as the Owners Business_Stream__c field in the User object.

 

The trigger that I have at the moment is below 

 

trigger ChrisAccountTest on Account (before insert) {
    Set<string> ownerId = new Set<string>();
    for(Account act : Trigger.New) {
        ownerId.add(act.ownerId);
    } 
    Map<id,User> idToUser = new Map<id,user([select id,Business_Stream__c from user where id in : ownerId]);
    for(Account act: Trigger.New){
        act.Business_Stream__c = (idToUser.get(act.ownerId)).Business_Stream__c ;
    }
}

 

The problem is on creation of the Account record the Business_Stream__c field is not being pre populated to that of the Business_Sream__c field in the User object. Business_Stream_c in Accounts is a required field so I have to set the Business_Stream__c field to a value and then save the record. After saving the record the you can see that the trigger has fired and the Business_Stream__c field on Accounts matches that of the Owners in the User object.

 

Is there anyway to have the field Business_Stream__c in Accounts pre populated before the record is saved?

 

Any help is greatly appreciated. 

hitesh90hitesh90

Hi Chris,

 

There is one error in your trigger. i have solved it and make sure your account owner has value in Business_Stream__c field.

 

trigger ChrisAccountTest on Account (before insert) {
    Set<string> ownerId = new Set<string>();
    for(Account act : Trigger.New) {
        ownerId.add(act.ownerId);
    } 
    Map<id,User> idToUser = new Map<id,user>([select id,Business_Stream__c from user where id in : ownerId]);
    for(Account act: Trigger.New){
        act.Business_Stream__c = (idToUser.get(act.ownerId)).Business_Stream__c ;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel

ChrisbrisbaneChrisbrisbane

Hi Hitesh,

 

Thanks for responding but your code provided is doing the same as my attempt.

 

The Business_Stream__c field in Account is still not being pre popualted from the Business_Stream__c field in the User record.

 

Thanks

hitesh90hitesh90

have you checked your account owner(User) has value in Business_Stream__c field means it not blank or None.

ChrisbrisbaneChrisbrisbane
Yes the Business_Stream__c in Owner(User) is a mandatory field there are no records in the system with this field blank or null.
Jerun JoseJerun Jose
Your trigger code seems correct. You can verify that by adding
system.debug('@@@ '+act.Business_Stream__c);
after the line,
act.Business_Stream__c = (idToUser.get(act.ownerId)).Business_Stream__c ;

And inspecting the debug logs.

If you still cant figure out why its not working, please post the debug logs for a transaction.