3

I am able to set Lead owner (Logged In) from Queue On INSERT but not able to set for UPDATE.

what mistake i did ?

Here is my trigger :

trigger leadConvertToLoggedInUser on Lead (before insert, before update, after update) { Lead Ld = Trigger.new[0]; if(Trigger.isInsert){ if(Ld.Owner.Type == 'Queue'){ User u = [select Id, Name from User where Id =: UserInfo.getUserId()]; Ld.OwnerId = u.Id; Insert Ld; } } if(Trigger.isUpdate){ Lead Ld1 = Trigger.Old[0]; if(Ld1.owner.Type == 'Queue'){ User u = [select Id, Name from User where Id =: UserInfo.getUserId()]; Ld1.OwnerId = u.Id; update Ld1; } } } 

Thanks

2 Answers 2

2

I can see a lot of changes to be made to the trigger

1.your trigger runs on both before & after update.. you cannot update the record coming into the trigger during after update.. so if you just keep it to before insert & before update it should work.

2.you should run for all the records coming into the trigger not just the first record alone..

3.you are doing the same logic for both insert and update.. so no need to replicate

4.related fields cannot be accessed without querying.. so Owner.Type will not be available unless u do a SOQL.. instead you can check the OwnerId prefix

with all these changes u might end up something like

trigger leadConvertToLoggedInUser on Lead (before insert, before update) { User u = [select Id, Name from User where Id =: UserInfo.getUserId()]; for(Lead Ld : Trigger.New){ String leadOwner = Ld.OwnerId; if(leadOwner.startsWith('00G')){ Ld.OwnerId = u.Id; } } } 

finally, this doesn't need a trigger. you can try using workflow rule or process builder to implement the same without any code.

0
1

I am not sure about your current code. But try this piece of code hope it will work for you. Also this is optimize code so you don't need to perform dml or soql.

if(Trigger.isUpdate){ if(Trigger.new[0].owner.Type == 'Queue') Trigger.new[0].OwnerId = UserInfo.getUserId(); } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.