1

I have a trigger, that executes right after ownership of the lead is changed. Everything works as expected, till someone uses the feature in the menu, where you can easily change multiple leads ownership. How to catch that?

trigger dTrigger on Lead (before insert, before update) {
if(trigger.new.size() > 1) 
        return;

if(string.isBlank(Trigger.New[0].SomeField))
{
    for(Lead obj: trigger.new)
        obj.SomeField = obj.OwnerId;
}

else if (Trigger.New[0].OwnerId != Trigger.Old[0].OwnerId) 
{
    for(Lead obj: trigger.new)
    {
        if([SELECT Name FROM UserRole Where Id = :[SELECT Id, UserRoleId FROM User WHERE Id = :obj.OwnerId][0].UserRoleId].Name == 'something')
            obj.SomeField = obj.OwnerId;
    }
}

}

enter image description here

2 Answers 2

1

If your trigger is bulkified, you will catch it the same way as you would if just one record was updated. For example, this code prevents ownership change:

trigger PreventOwnerChange on Lead (before update) {
    for(Lead record: Trigger.new) {
        if(record.OwnerId <> Trigger.oldMap.get(record.id).OwnerId) {
            record.OwnerId.addError('You may not change ownership of leads.');
        }
    }
}

This same code works from the Change Owner link, the data loader, or trying to change an owner directly from a lead record. No special logic is needed other than writing a normal, bulkified trigger.

1
  • Actually, I have pretty similar trigger. It works every time except the one on the screen
    – Say MyName
    Oct 22, 2015 at 6:43
0
if(trigger.new.size() > 1) 
    return;

This was the cause! Now it works.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .