1

I get a read only error when I am trying to change the lead owner. Scenario is like: I have to change the lead owner based a condition lets say (status__c = 'a' or status__c = 'b'). The new lead owner is coming from a cusotm object lookup(user) field.

I am trying to do something like this:

for(Lead lead : Trigger.new) {
        if(lead.Status__c == 'A' || lead.Status__c == 'B') {
            val = lead.value__c;
            System.debug('lead value: ' + lead.value__c );
            Value__c value = (Value__c)valueMap.get(val);
            String userId = value.userlookupfield__c;
            User u = userMap.get(userId);
            lead.Owner.Name = u.Id;
        }
        leadsWithUpdatedOwner.add(lead);
    }
    if(leadsWithUpdatedOwner.size() > 0 && leadsWithUpdatedOwner != null)
        update leadsWithUpdatedOwner;

Sometimes I get a read only error and when I am trying to do like this:

lead.Owner.ID = u.Id;

it gives me a null reference error.

Also I tried like this:

lead.Owner.Name = u.name;

Field is not writeable: Name.Name (compile error)

How do I change the lead owner here...

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Jan 22, 2018 at 18:01

1 Answer 1

4

Try something like this.

...    
Set<String> StatusToCompare = new Set<String> {'A','B'}; 
    for(Lead lead : Trigger.new) {
            if(StatusToCompare.contains(lead.Status__c)) {
                System.debug('lead value: ' + lead.value__c );
                val = lead.value__c;
                Value__c value = (Value__c)valueMap.get(val);
                String userId = value.userlookupfield__c;
                if(userId == Lead.Ownerid) continue;                    
                leadsWithUpdatedOwner.add(new lead(id = lead.id, Ownerid = userId));
            }
        }
            update leadsWithUpdatedOwner;

The if() continue; is there to protect the code from infinite recursion problem, since the trigger is firing after update, we don't want to keep updting the lead indefinetly. And as madvhati said you should use the Object.Ownerid field in order to change the ownership of an object. (would be still useful to know how ValueMap is getting populated.

4
  • if(userId == Lead.Ownerid) continue; does really adds value. Another workaround is you could have entry filter criteria to check if the value__c is modified. Jan 22, 2018 at 11:19
  • Thank you.. I have added this to my code. Really appreciate this. :)
    – user905
    Jan 22, 2018 at 11:22
  • 1
    If It still doesn't solve your errors on update, tell me which error are you getting so we can try find a proper way to solve it. If it solved your problem mark as an accepted answer so it can help other people aswell :) Jan 22, 2018 at 11:25
  • This does solve my error... omg thank you so so much @AlexanderAeonsTorn. Thanks a ton :) And thanks for all the additional comments as well..I know its really gonna help me in the long run. Appreciate your help very much.
    – user905
    Jan 22, 2018 at 11:52

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.