I have requirement to update user record based on insert/update Junction object between custom object and user. I need to update user custom field accountdef with junction object values(more than one for given user).I am newbie to Apex coding and having trouble with bulkifying the code. I have started something like this but could not navigate list to update user record.Any help will be appreciated.

trigger UpdateUserGUL on GUL_Account_to_User_Junction__c (after insert) {

    set<id> UserList    = new Set<id>();

    for (GUL_Account_to_User_Junction__c junc : Trigger.New){

       //add set of Ids

   //query junction records based on userid set

GUL_Account_to_User_Junction__c[] juncrecords = [select id,UserId__c,GUL_Account_Name__c,Chatter_Group_Name__c 
                                                     from GUL_Account_to_User_Junction__c where UserId__c IN :UserList];
     Map<id, String> AccountsMap = new Map<id, String>();
     Map<id, String> ChatterMap = new Map<id, String>();

    //Create Map of records 
    for (GUL_Account_to_User_Junction__c j : juncrecords) {
    AccountsMap.put(j.UserId__c, j.GUL_Account_Name__c);
        ChatterMap.put(j.UserId__c, j.Chatter_Group_Name__c);

  • @DougB Junction object records are more than one for given user with different account names. I need to male long string of accountnames like accountA,accountB,accountC. – crmsj May 20 '16 at 18:07

Edit: per Adrian's comment you technically don't need to query the User record. You can use the User Id from the junction object alone to update the records.

List<User> userList = new List<User>();

for(GUL_Account_to_User_Junction__c junction : Trigger.new) {
    User userRecord = new User();
    userRecord.Id = junction.UserId__c;
    userRecord.Field_To_Update__c = ???;
update userList;

Original Answer

If you need to update the User object then you need to query the User records.

Map<Id, User> userMap = new Map<Id, User>([SELECT Id, Field_To_Update__c FROM User WHERE Id IN :userList];

Then loop through your junction object and update the User records per your requirements.

for(GUL_Account_to_User_Junction__c junction : Trigger.new) {
    User userRecord = userMap.get(junction.UserId__c);
    userRecord.Field_To_Update__c = ???;
update userMap.values();
  • 2
    Actually, if you have the Id already, you don't need to query. update new User(Id=userId, Field_To_Update__c='Some Value'. – Adrian Larson May 20 '16 at 17:45
  • 2
    Also note that it is demonstrably faster to put all field/value pairs in the SObject constructor if you know them at time of instantiation. :P – Adrian Larson May 20 '16 at 17:53
  • 2
    @AdrianLarson I did not know that it was faster via the constructor. Learn something new everyday :D – dBeltowski May 20 '16 at 17:56
  • @sfdcfox showed it somewhere on here. Can't seem to find it now, but I'll link to it if I do. – Adrian Larson May 20 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.