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
Sriurs444Sriurs444 

Reg: Updating the Field On the User Object through Trigger from another custom object

Hi,

 

Can you please anyone help me on the below trigger.

 

We are trying to update the Federation Identifier Field (On User Object) from one Custom Object  SAPCode__c. There is a field called PeopleKey __c.

 

When we creating a new user or updating a user record, we have to update the Federation Identifier on the User Object from Custom Object A ( Field in SAPcode__c as Peoplekey__c).

 

Here is the trigger code:

 

trigger PopulateFederationId on User (before insert, before update)
{
 
    Set<id> setsapcodeids = new Set<id>();
    for(User u : trigger.new)
   {
         if(u.email!= null)
         {
          setsapcodeids.add(u.id);
         }
   }

 MAP<ID,SAPCode__c> mapSAPCode = new MAP<ID , SAPCode__c>([Select Enterprise_Id__c, PeopleKey__c, Email__c from SAPCode__c where id in: setsapcodeids Limit 1]);
system.debug('####' + mapSAPCode.size() + '- user counts:' + setsapcodeids.size());
   for(User u : Trigger.new)
   {
       if(mapSAPCode.containsKey(u.email))
       {
          SAPCode__c  sc = mapSAPCode.get(u.id);
         
          if(sc.email__c == u.email)
          {
            u.FederationIdentifier = sc.PeopleKey__c;
          }
       }  
            
   }

}

 

I am trying to update it, but, I am not getting any error, but the field is not updating in the User Object.

 

Can anypone please help me onthis.

 

Thanks,

Sridhar.

HariDineshHariDinesh

Hi,

 

Here problem with this line

 

                if(mapSAPCode.containsKey(u.email))

 

As per your map declaration your map contains Id as Key and particular instance as value.

 

But here you are checking like mapSAPCode.caontainskey(email)

Which always false and your controller will not enter if loop.

 

Try to change the logic or change the type of map and write the code accordingly.

jungleeejungleee

Hi Sridhar ,

 

There will never be a value in the map mapSAPCode because you're are picking up the user id and storing in the set called setSAPCodeIds and then you're using these id's to fetch the records from SAPCode__c object in the SOQL (where clause). I hope you're getting what I am trying to say here.

 

I think the solution is to create a look up to User object on the SAPCode__c, by doing that you're establishing a relationship between the 2 objects.

 

Hope this helps!

 

Regards

Sam

maja madi