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

Updating Custom Object Look Up Field on Lead Record

So let me preface this by stating that I have searched the boards and Google in general and have not been able to find a simplified explanation for what I am trying to do. I have very limited coding skills and no experience when it comes to Apex triggers so I appreciate in advance any and all help you can provide. I will do my best to explain below what I am trying to do.


On my lead records, I have a look up field to a custom object called 'Distributors.' If a lead comes in from an outside distributor, then the internal sales rep can look up and add the appropriate distributor. Sometimes these outside distributors have co-branded web landing pages so that visitors can fill out a web form, which becomes a new lead record. What I need is a way to have the distributor automatically filled in, something that doesn't necessarily need to occur upon the initial record creation, but could happen following this initial creation or during the subsequent record editing.


What I have in mind is a hidden pick list with all distributors listed. The web form would be set to default to the specified distributor value and the apex trigger would then take the text of this value and insert it into the look up field. If this doesn't work because of the source being a pick list, then I could create a workflow that takes the pick list value and inserts it into a text box. 


What I need help with is the basic code to make this happen as it's something that I will need to perhaps extend to campaigns and other custom objects that rely on the use of a look up fields on the lead record.


Once again, thank you in advance for taking the time to read this and for any help that can be provided.




What I understood from your scenario is you are not going to display Lookup field on web form, but the lead record should get a default value for the Distributor__c field (which is Lookup field).


In this case, use is not going to select any value at the time of initial creation, so we can achive this using a trigger code.


Assuming that Distributor object has few records (at least one record so that link can be made with Lead object),


trigger setDefaultDistributor on Lead(before insert, before update){
	Id defaultDis;
	// Query the default Distributor 
	List<Distributor__c> lstDitributor = [Select Id,Name from Distributor__c where Name =: 'Default' limit 1];
		defaultDis = lstDitributor[0].Id;
		defaultDis = '';
	for(Lead objLead : Trigger.New){
		if(objLead.Distributor__c == null || objLead.Distributor__c == '') // Lead created from Web form or internal user has not selected the Distributor
			Distributor__c = defaultDis; // Assign default distributor Id






Correct in that this would be need to be a trigger running after the initial lead creation. Unfortunately I cannot set the distributor beforehand with the webforms we are using.


Also, I have many distributors and no one default distributor (there are also non-distributor leads where no distributor needs to be entered.)


Is there no way to simply copy/paste text from a picklist or text box into the distributor lookup box upon saving a record?