4

I have trigger on lead where i need to update the owner name into custom field (User__c), this field is on lead object which is lookup to user object, here is my trigger. It only updates when you edit the lead record, modify data and save it not when you refresh the page, any help is appreciated.

trigger UserRegionL on Lead (before insert, before update) 
{
 List<User> us = new List<User>();
 Id userId;
 for (Lead l : Trigger.New)
  {
    userId = l.OwnerId;
  }
     us = [select Id,Name from User where id =:userId];
     for(Lead ld : trigger.New)
       {
         for (User u :us)
            {
              ld.User__c = u.Id;
            }
       } 

}
6

I see a couple of issues. Firstly, you're looping over all of the leads, but only setting the User Id once:

 for (Lead l : Trigger.New)
  {
    userId = l.OwnerId;
  }

So, if say 10 leads are updated at once, the userId will be set to the Owner of the last lead that is updated.

Then, you go back around the loop, and have a sub-loop of all (1) users selected, updating all of your leads to have the same user Id:

 for(Lead ld : trigger.New)
   {
     for (User u :us)
        {
          ld.User__c = u.Id;
        }
   } 

Unless I am misunderstanding what you're trying to achieve, I think what you are trying to do is simply this:

trigger UserRegionL on Lead (before insert, before update) 
{
    for(Lead l : Trigger.new)
    {
        l.User__c = l.OwnerId;
    }
}

EDIT: Adding check to see if User or Group:

It is possible that a record can be owned by a Group rather than a User. In which case, you will not be able to assign the OwnerId to the User__c field. In this situation, you would need to determine whether the Owner is a User or a Group, for example:

if(l.OwnerId.getSObjectType() == User.getSObjectType())
{
    l.User__c = l.OwnerId;
}
else
{
    l.Group__c = l.OwnerId;
}
  • Thanks Phil, your trigger is working fine for new leads but for the old ones when i refresh the page it's not updating the user field, do i have to edit the record and modify some field and save it to update the field? i thought if we refresh then it should update, any idea on how to achieve this? – Admin_BA Jul 16 '14 at 18:46
  • Yes, the update trigger won't fire until you actually make an update to the record. Refreshing the page doesn't make an update, it simply reads the record from the backend. – Phil Hawthorn Jul 16 '14 at 19:01
  • Got it. Iwas trying to perform mass update using dataloader but iam getting this error "UserRegionL: data changed by trigger for field User: id value of incorrect type: 00G400000015XYFEA2", what does this mean? – Admin_BA Jul 16 '14 at 19:39
  • It means that the owner must be a Group rather than a User. You'd need to check in the trigger code to see if its a User or Group that owns the record and then populate accordingly. – Phil Hawthorn Jul 17 '14 at 8:15

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.