So I am working on two objects:


-Partner object(Custom)

So I have a before insert Trigger on Leads, so when a new Lead is created and Partner Number Field is not blank it has to lookup for Partner_Number_Copy__c field on the Partner Object and return its Partner ID

Following Code works fine if I manually create a lead and provide a Partner Number it is populating a Partner__c field which is a lookup field to Partner object. But when the Lead is created from a Web Form, Partner Number field is populating a number which has a match on the Partner Object but the Partner Id is not being pulled to the lookup.

public class Helper{      

         public static void populateparnerId(list<Lead> lstlead){

             set<decimal> setpartnerIds=new set<decimal>();

              for(Lead l:lstlead){

       //Query partner records

    map<decimal,Partner__c>  mapIdByPartner=new map<decimal,Partner__c>();

    for(Partner__c p:[Select Id,Partner_Number_Copy__c from Partner__c where Partner_Number_Copy__c  in:setpartnerIds]){

     for(lead l:lstlead){


Trigger used ,

if(trigger.isInsert && trigger.isBefore){ Helper.populateparnerId(trigger.new); }

Following is a image from the Lead object created from the web form, it has a Partner Number but the Partner(lookup) field did not populate the ID. The same number when I tested creating a Lead manually without using forms it works.

enter image description here

  • 1
    Debug out the Partner Number and see what it looks like in the code when coming from the web and then debug the map keys. You will find the problem if you do that. The issue is the map does not contain the Partner Number or the query did not find a match the question is why so It has to do with the value of Partner_Number__c when coming from the web
    – Eric
    Jul 27 '15 at 22:12
  • The debug statement is a great idea. You may also want to try setting up your class without sharing to see if that works. If it works without sharing, then you might have a FLS or object level security issue when the processing takes place. Jul 27 '15 at 22:17
  • Will try your suggestions and update back. Thanks Eric and Coldeagle
    – Sanjay
    Jul 27 '15 at 22:39
  • @Eric: thanks for the suggestion that helped ! Value returned from the form is like ' 1024.0 ' value from the map is ' 1024 ' so used l.Partner__c = mapIdByPartner.get((string.valueof(l.Partner_Number__c)).substringBefore('.')).id;
    – Sanjay
    Jul 28 '15 at 4:03

So on debug the value returned from the form is 1024.0 and the value in the map is 1024 so following change in the code is the fix:

l.Partner__c = mapIdByPartner.get((string.valueof(l.Partner_Number__c)).substringBefore('.')).id;

substringBefore('.') - did the Job

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.