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
Eirik Fladby 8Eirik Fladby 8 

Replace lookup field value with text field value (need id?)

Hi,

We are using an integrated web-to-lead application, and the lookup field cannot be sent. Therefore, the value is sent as text to a text field in Lead object in Salesforce. I then need to convert/copy the value in the text field to the lookup field. How do I get the ID of the value in the text field sent by the web-to-lead form? If I get the ID, I can maybe use this on the lookup field instead?

This is my Apex code:

public class LeadProperty {
    public static void convertLeadProperty(Lead lead) { // triggers before insert
        lead.Property__c = lead.Property_text__c; // Need to get ID of property text to put it in property lookup field. Maybe via SQL?
    }


And trigger:

trigger LeadConvertPropertyTrigger on Lead (before insert) {
    Lead lead = Trigger.new;
    LeadProperty.convertLeadProperty(lead);
}


This code is probably wrong? My first try.

Abhishek BansalAbhishek Bansal
Hi Eirik,

You do not to query anything since lookup field can be assigned with the text values directly. Just need some updates on the trigger as well as helper class. Please find the updated one below:

Trigger:
trigger LeadConvertPropertyTrigger on Lead (before insert) {
    LeadProperty.convertLeadProperty(trigger.new);
}
Helper Class:
public class LeadProperty {
    public static void convertLeadProperty(List<Lead> newLeads) { // triggers before insert
        for(Lead ld : newLeads){
			ld.Property__c = ld.Property_text__c;
		}
    }
}

Please try with the above updated code and let us  know if you face any issues with this.

Thanks,
Abhishek Bansal.
 
Eirik Fladby 8Eirik Fladby 8

Hi Abhishek,

Thanks for your reply! I tried your solution. The implementation looks like this now: 

Trigger:

trigger LeadConvertPropertyTrigger on Lead (before insert) {
   LeadProperty.convertLeadProperty(trigger.new);
}
Class:
public class LeadProperty {
    public static void convertLeadProperty(List<Lead> newLeads) { // triggers before insert
        for(Lead ld : newLeads) {
            ld.Property__c = ld.Property_text__c;
        }
    }
}

But when the trigger is active no leads come in. If I disable the trigger the leads come in again. So there must be something wrong with here?
Abhishek BansalAbhishek Bansal

Hi Eirik,

Did you saw any error when the trigger is active. Can you confirm the Property_text__c field consist a single id of the Property__c object record. Also you need to make sure that the Property record should exist in your org with the id stored in the Property_text__c field.

It would be good if you can contact me on gmail or skype so that we can debug the issue and find out the right solution.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal