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

Cannot update ownerId from lead trigger from Web-to-Lead

I have tried before insert and after insert triggers but i cannot seem to update the ownerId. During the trigger the ownerId gets set properly, as seen via the debug log, but when I look at the lead in SF it is still the default owner. Specificly these leads are being created via a web-to-lead form.


In either version (Before or After insert) I get the list of leads, find the proper ownerId and assign it to the lead. In my After Insert trigger i even printed to the log the owner Id before updating the leads and then again after updating the leads. The ownerId comes our correct in the log but still not in SF. Here is a stripped down version of my After Insert trigger



trigger Lead on Lead (after insert) { 
    ProtectionRules pr = new ProtectionRules();
    String prResult = 'not found';
 	String countryCode;
	Set<ID> ids = trigger.newmap.keyset();
	list<Lead> leads = [SELECT Id,Status,Downgrade_Date__c,Phone,indexedPhone__c,Email,indexedEmail__c,OwnerId,Owner_Manager_Email__c,Owner_Manager_Name__c,LeadSource,Lead_Score__c FROM Lead WHERE Id in :ids];
	for(Integer i=0; i<leads.size();i++){
		*Code to find OwnerId*
                leads[i].OwnerId = [SELECT id FROM User WHERE Team__c=:assignmentRule.TeamName__c AND Team_Member_Number__c=:teamMemberNumber].Id;

		system.debug('THIS IS LEAD.OWNERID AT THE END: ' + leads[i].OwnerId);

    system.debug('THIS IS THE LEAD OWNER BEFORE UPDATE: ' + [SELECT Id,OwnerId FROM Lead WHERE Id=:leads[0].Id].OwnerId);    	
	update leads;
	system.debug('THIS IS THE LEAD OWNER AFTER UPDATE: ' + [SELECT Id,OwnerId FROM Lead WHERE Id=:leads[0].Id].OwnerId);



 I realize those debug lines at the end will only show the first lead in the list. I am just testing one at a time.


What am I missing? I've been stuck on this for the last day or two and any help would be appreciated.




Can you just explain why you're doing this in Apex as opposed to configuring the Lead Assignment Rules to do what you want ?



You can't change any field values for records in trigger after context so you should implement trigger for before insert event. Also why you are querying the leads from the database when you could simply iterate


You could try something like this:


trigger tgrLead on Lead (before insert) {


   for(Lead x :


      x.OwnerId = <desired owner>;





Furthermore, you should avoid having queries inside loops. If the trigger is processing a batch of records you will get an exception when you hit the limit in queries.


-Well the assignment rules we are trying to follow are a bit to complicated to just use the Lead Assignment Rules. I was using them before though. 


-I do have a before insert version also, using It is actually what i have on production right now. But it still doesn't work. There is something about how or the order that web-to-leads are inserted.


-I agree about the queries inside loops. I've only just started to understand bulk apex. But I need to at least see this work with 1 record.


So I am pretty convinced that this is just because these are web-to-leads. What is it about the timing of insertion that is stopping this from working? 


Update: I created a Before Update trigger where i print[i].OwnerId and trigger.old[i].OwnerId. The old value holds that Id that I want while the new value holds the default owner Id. So obviously my Before Insert trigger is causing the before update trigger to be called. Its almost like the Before trigger catches the lead at the same time the lead is being inserted, then when the Update is being called the insert has finish but with the default value. Which makes me think that I should be using an After Insert trigger, but that didn't help when i did it yesterday.


I found a solution, two of them actually on this post:


I'm using the second solution of adding an active assignment rule which has no criteria and does nothing but the Reassign Owner box is checked. So far seems to be working fine since I don't have any other assignment rules.