+ Start a Discussion
Kim MoutesKim Moutes 

Adding a Lookup Relationship field to a custom button

Hello-
I created a Custom Button to convert a Contact to a Lead. I know this is not the normal way but I have a client requesting it for a specific initiative. I created a Lookup Relationship field on the Lead object called Account_Lookup__c so that when I am on the Account page I can look at the Related List to see what Leads have been generated from that Account. I need to add this to the custom button I created so that it auto populates at the time of conversion and am not sure how to write that in. Any help would be appreciated. 

Thanks,
Kim
Best Answer chosen by Kim Moutes
Pankaj_GanwaniPankaj_Ganwani
Okay, Just do this:

if('{!Account.Number_of_DCs__c}' == '')
   Lead.Number_of_DCs__c = null;
else
    Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Kim,

Can you please explain more on your problem statement so that we can assist you for finding the solution? By going through the aforementioned statement, you have created a custom button on Contact(assuming the contact which has been created as a result of lead conversion). On click of this button, you want to associate the related Contact's Account with converted lead. 

Please let me know if my understanding is correct.
Kim MoutesKim Moutes
Hi Pankaj-
Thank you for responding! Yes, when I look at the Account page I want to see what Leads have been generated from that Account. This is why I created the Lookup Relationship field. However, when I write it into the code in the button like this Lead.Account_LookupId__c = '{!Account.Id}'; I get this error faultcode:'sf:INVALID_FIELD', faultstring:'INVALID_FIELD:No such column 'Account_LookupID__c' on entity 'Lead'. 
Pankaj_GanwaniPankaj_Ganwani
Hi Kim,

Can you please let me know where exactly your detail button is placed on contact detail page or on lead detail page? And if possible Can you please share your custom button code with us?
Kim MoutesKim Moutes
Pankaj-
It is on the contact page. Here is the code. The last line Lead.Account_LookupId__c = '{!Account.Id}'; is the one causing the error, I believe. 

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')} 

var Lead = new sforce.SObject('Lead');

Lead.Company = '{!Account.Name}';
Lead.FirstName = '{!Contact.FirstName}';
Lead.LastName = '{!Contact.LastName}';
Lead.Phone = '{!Contact.Phone}';
Lead.MobilePhone = '{!Contact.MobilePhone}';
Lead.LeadSource = 'Concept Services';
Lead.Email = '{!Contact.Email}';
Lead.Street = '{!Account.BillingStreet}';
Lead.City = '{!Account.BillingCity}';
Lead.PostalCode = '{!Account.BillingPostalCode}';
Lead.State = '{!Account.BillingState}';
Lead.Country = '{!Account.BillingCountry}';
Lead.Title = '{!Contact.Title}';
Lead.Description = '{!Contact.Description}';
Lead.OwnerId = '{!Account.OwnerId}';
Lead.Familiar_with_Company__c = '{!Account.Familiar_with_Company__c}';
Lead.Competition__c = '{!Account.Competition__c}';
Lead.Annual_Transportation_Spend__c = '{!Account.Annual_Transportation_Spend__c}';
Lead.Shipping_Methods__c = '{!Account.Shipping_Methods__c}';
Lead.Warehouse_Management_System_WMS__c = '{!Account.Warehouse_Management_System_WMS__c}';
Lead.Transportation_Management_System_TMS__c = '{!Account.Transportation_Management_System_TMS__c}';
Lead.Happy_with_Current_Provider__c = '{!Account.Happy_with_Current_Provider__c}';
Lead.Why_Why_Not_Happy_with_Current_Provider__c = '{!Account.Why_Why_Not_Happy_with_Current_Provider__c}';
Lead.DC_Locations__c = '{!Account.DC_Locations__c}';
Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
Lead.CS_Lead_Write_up__c = '{!Contact.CS_Lead_Write_up__c}';
Lead.Account_LookupId__c = '{!Account.Id}';

result = sforce.connection.create([Lead]); 

if(result[0].success == 'true'){ 
alert('A New Lead was Created Successfully by {!$User.FirstName} {!$User.LastName}.'); 
}
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please check the field level security for Account_LookupId__c? It should be set to visible.
Kim MoutesKim Moutes
It is visible to System Administrators and one other Profile. Does it need to be visible to all Profiles?
Pankaj_GanwaniPankaj_Ganwani
Hi,

Ideally, it should be visible to the profile which you are currently logged in with. Just for testing purpose, you can enable this for rest of the profiles as well and then try. If possible, check for the field api name once more.
Kim MoutesKim Moutes
Pankaj-
I made it visible to all but still got the same error so I changed it back. I double checked the API name and I had been using the Account_LookupID__c field. This time, I just used the Account_Lookup__c field rather than the ID field and I am no longer getting that error but when I click the Create a Lead button nothing happens. Thoughts?
Pankaj_GanwaniPankaj_Ganwani
Is there any javascript errors you are recieving on click of the button? For checking them, just click on the custom button in chrome and use inspect element to see the script errors.
Kim MoutesKim Moutes
I did what you said but I'm not sure what I am looking for. I am not a developer. 
Kim MoutesKim Moutes
This was in the Console section. Not sure if its helpful. 
Failed to load resource: the server responded with a status of 404 (Not Found)
servlet.Integration:1 Font from origin 'https://na9.salesforce.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://postwire.na9.visual.force.com' is therefore not allowed access. The response had HTTP status code 404.
https://na9.salesforce.com/static/fonts/TradeGothic/tradegothicltstd-bd2-webfont.woff Failed to load resource: the server responded with a status of 404 (Not Found)
servlet.Integration:1 Font from origin 'https://na9.salesforce.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://postwire.na9.visual.force.com' is therefore not allowed access. The response had HTTP status code 404.
https://na9.salesforce.com/static/fonts/TradeGothic/tradegothicltstd-bd2-webfont.ttf Failed to load resource: the server responded with a status of 404 (Not Found)
Pankaj_GanwaniPankaj_Ganwani
Okay, Just replace script with this line at the top to import the updated version:
{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')} 

put alert(result) to see what comes after sforce.connection.create statement.
Kim MoutesKim Moutes
I changed it as below and received a Result is not found error. 

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')} 

var Lead = new sforce.SObject('Lead'); 

Lead.Company = '{!Account.Name}'; 
Lead.FirstName = '{!Contact.FirstName}'; 
Lead.LastName = '{!Contact.LastName}'; 
Lead.Phone = '{!Contact.Phone}'; 
Lead.MobilePhone = '{!Contact.MobilePhone}'; 
Lead.LeadSource = 'Concept Services'; 
Lead.Email = '{!Contact.Email}'; 
Lead.Street = '{!Account.BillingStreet}'; 
Lead.City = '{!Account.BillingCity}'; 
Lead.PostalCode = '{!Account.BillingPostalCode}'; 
Lead.State = '{!Account.BillingState}'; 
Lead.Country = '{!Account.BillingCountry}'; 
Lead.Title = '{!Contact.Title}'; 
Lead.Description = '{!Contact.Description}'; 
Lead.OwnerId = '{!Account.OwnerId}'; 
Lead.Familiar_with_Company__c = '{!Account.Familiar_with_Company__c}'; 
Lead.Competition__c = '{!Account.Competition__c}'; 
Lead.Annual_Transportation_Spend__c = '{!Account.Annual_Transportation_Spend__c}'; 
Lead.Shipping_Methods__c = '{!Account.Shipping_Methods__c}'; 
Lead.Warehouse_Management_System_WMS__c = '{!Account.Warehouse_Management_System_WMS__c}'; 
Lead.Transportation_Management_System_TMS__c = '{!Account.Transportation_Management_System_TMS__c}'; 
Lead.Happy_with_Current_Provider__c = '{!Account.Happy_with_Current_Provider__c}'; 
Lead.Why_Why_Not_Happy_with_Current_Provider__c = '{!Account.Why_Why_Not_Happy_with_Current_Provider__c}'; 
Lead.DC_Locations__c = '{!Account.DC_Locations__c}'; 
Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}'; 
Lead.CS_Lead_Write_up__c = '{!Contact.CS_Lead_Write_up__c}'; 
Lead.Account_Lookup__c = '{!Account.Id}'; 

alert(result) = sforce.connection.create([Lead]); 

if(result[0].success == 'true'){ 
alert('A New Lead was Created Successfully by {!$User.FirstName} {!$User.LastName}.'); 
}
Pankaj_GanwaniPankaj_Ganwani
No, you did a mistake here. Just do this:

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')} 

var Lead = new sforce.SObject('Lead');

Lead.Company = '{!Account.Name}';
Lead.FirstName = '{!Contact.FirstName}';
Lead.LastName = '{!Contact.LastName}';
Lead.Phone = '{!Contact.Phone}';
Lead.MobilePhone = '{!Contact.MobilePhone}';
Lead.LeadSource = 'Concept Services';
Lead.Email = '{!Contact.Email}';
Lead.Street = '{!Account.BillingStreet}';
Lead.City = '{!Account.BillingCity}';
Lead.PostalCode = '{!Account.BillingPostalCode}';
Lead.State = '{!Account.BillingState}';
Lead.Country = '{!Account.BillingCountry}';
Lead.Title = '{!Contact.Title}';
Lead.Description = '{!Contact.Description}';
Lead.OwnerId = '{!Account.OwnerId}';
Lead.Familiar_with_Company__c = '{!Account.Familiar_with_Company__c}';
Lead.Competition__c = '{!Account.Competition__c}';
Lead.Annual_Transportation_Spend__c = '{!Account.Annual_Transportation_Spend__c}';
Lead.Shipping_Methods__c = '{!Account.Shipping_Methods__c}';
Lead.Warehouse_Management_System_WMS__c = '{!Account.Warehouse_Management_System_WMS__c}';
Lead.Transportation_Management_System_TMS__c = '{!Account.Transportation_Management_System_TMS__c}';
Lead.Happy_with_Current_Provider__c = '{!Account.Happy_with_Current_Provider__c}';
Lead.Why_Why_Not_Happy_with_Current_Provider__c = '{!Account.Why_Why_Not_Happy_with_Current_Provider__c}';
Lead.DC_Locations__c = '{!Account.DC_Locations__c}';
Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
Lead.CS_Lead_Write_up__c = '{!Contact.CS_Lead_Write_up__c}';
Lead.Account_LookupId__c = '{!Account.Id}';

result = sforce.connection.create([Lead]); 

alert('=============='+result);

if(result[0].success == 'true'){ 
alert('A New Lead was Created Successfully by {!$User.FirstName} {!$User.LastName}.'); 
}
Kim MoutesKim Moutes
Ok, I added it and received this error box =============={id:''00QE0000011AfkfMAC', success:'true',} and when I clicked ok then it created the Lead. Do you know why I received the error first?
Pankaj_GanwaniPankaj_Ganwani
Now, you can remove this alert box from here as your functionality is working as expected now. I suggested you to place this statement to check whether the lead is created or not. We usually use this for debugging purpose in programming. Once the code works properly, we remove these statements from the code.

Anyways, this is not an error box, this is simply a javascript alert box to indicate the status to user so user can easily locate where the error is coming.
Kim MoutesKim Moutes
Thank you so much for the help and explanation. I have one more question for you. I would like to add a date field to my code but every time I do I get an error. {faultcode:'soapenv:Client', faultstring:"2/22/2016' is not a valid value for the type xsd:date', } I don't think it likes the format of the date field. Thoughts?
Kim MoutesKim Moutes
Forgot to show you the code
Lead.Contract_Expiration_Date__c = '{!Account.Contract_Expiration_Date__c}';
Pankaj_GanwaniPankaj_Ganwani
Hi,

Please remove single quotes from the above mentioned statement:
Lead.Contract_Expiration_Date__c = {!Account.Contract_Expiration_Date__c};

Use this instead.
Kim MoutesKim Moutes

After removing the single quotes I received this error
{faultcode:'soapenv:Client', faultstring:"0.00004313319530710835' is not a valid value for the type xsd:date', }

Pankaj_GanwaniPankaj_Ganwani
I believe both of these fields are of date type. Isn't it?
Kim MoutesKim Moutes
Yes
Pankaj_GanwaniPankaj_Ganwani
Okay, For testing purpose try to assign today's date to this field and let's see what happens.

Lead.Contract_Expiration_Date__c = new Date();
Kim MoutesKim Moutes
It created the Lead. No errors. How is that working?
Pankaj_GanwaniPankaj_Ganwani
Okay, This is a javascript method that returns the today's date. However, just add alert('{!Account.Contract_Expiration_Date__c}') ;before Lead.Contract_Expiration_Date__c = {!Account.Contract_Expiration_Date__c}; statement and check what comes there.
Kim MoutesKim Moutes
Today's date was displayed. I clicked ok and received that same faultcode error with all the numbers. Was this the correct way to write it?
alert('{!Account.Contract_Expiration_Date__c}'); 
Lead.Contract_Expiration_Date__c = {!Account.Contract_Expiration_Date__c}; 
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Are you on skype or fb? If possible, Can you connect with me?
Kim MoutesKim Moutes
skype name kim.moutes
Pankaj_GanwaniPankaj_Ganwani
Hi Kim,

Please use below mentioned code as we have fixed this problem. Let us know if you still see any issues with it:

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')} 

var Lead = new sforce.SObject('Lead');

Lead.Company = '{!Account.Name}';
Lead.FirstName = '{!Contact.FirstName}';
Lead.LastName = '{!Contact.LastName}';
Lead.Phone = '{!Contact.Phone}';
Lead.MobilePhone = '{!Contact.MobilePhone}';
Lead.LeadSource = 'Concept Services';
Lead.Email = '{!Contact.Email}';
Lead.Street = '{!Account.BillingStreet}';
Lead.City = '{!Account.BillingCity}';
Lead.PostalCode = '{!Account.BillingPostalCode}';
Lead.State = '{!Account.BillingState}';
Lead.Country = '{!Account.BillingCountry}';
Lead.Title = '{!Contact.Title}';
Lead.Description = '{!Contact.Description}';
Lead.OwnerId = '{!Account.OwnerId}';
Lead.Familiar_with_Company__c = '{!Account.Familiar_with_Company__c}';
Lead.Competition__c = '{!Account.Competition__c}';
Lead.Annual_Transportation_Spend__c = '{!Account.Annual_Transportation_Spend__c}';
Lead.Shipping_Methods__c = '{!Account.Shipping_Methods__c}';
Lead.Warehouse_Management_System_WMS__c = '{!Account.Warehouse_Management_System_WMS__c}';
Lead.Transportation_Management_System_TMS__c = '{!Account.Transportation_Management_System_TMS__c}';
Lead.Happy_with_Current_Provider__c = '{!Account.Happy_with_Current_Provider__c}';
Lead.Why_Why_Not_Happy_with_Current_Provider__c = '{!Account.Why_Why_Not_Happy_with_Current_Provider__c}';
Lead.DC_Locations__c = '{!Account.DC_Locations__c}';
Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
Lead.CS_Lead_Write_up__c = '{!Contact.CS_Lead_Write_up__c}';
Lead.Account_LookupId__c = '{!Account.Id}';
Lead.Contract_Expiration_Date__c = fixDate('{!Account.Contract_Expiration_Date__c}');

result = sforce.connection.create([Lead]); 


if(result[0].success == 'true'){ 
alert('A New Lead was Created Successfully by {!$User.FirstName} {!$User.LastName}.'); 
}

function fixDate(date){

if(date == '' || date == null)
  return null;
var splittedDate = date.split('/'); 
var month = splittedDate[0] < 10 ? "0"+splittedDate[0] : splittedDate[0];
var day = splittedDate[1] < 10 ? "0"+splittedDate[1] : splittedDate[1];
  return splittedDate[2] + "-" + month + "-" + day + "T00:00:00Z";
}
Kim MoutesKim Moutes
It worked! This is awesome :) thank you!!!!!!!!!!!!!!
Kim MoutesKim Moutes
Hi Pankaj-
I had a scenario come up recently with the custom button you helped me create last month. One of the fields is a number field type and whenever we leave this field blank we receive a JavaScript error and it won't create the lead.
Field Line from Button: Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
Error: {faultcode:'soapenv:Client', faultstring:''' is not valid for the type xsd:double', }

Any help you could provide would be much appreciated.

Thanks,
Kim
Pankaj_GanwaniPankaj_Ganwani
Hi Kim,

I have updated your script to fix this issue. Please use the same and make sure you create backup of the existing one before replacement:

{!REQUIRESCRIPT('/soap/ajax/32.0/connection.js')} 

var Lead = new sforce.SObject('Lead');

Lead.Company = '{!Account.Name}';
Lead.FirstName = '{!Contact.FirstName}';
Lead.LastName = '{!Contact.LastName}';
Lead.Phone = '{!Contact.Phone}';
Lead.MobilePhone = '{!Contact.MobilePhone}';
Lead.LeadSource = 'Concept Services';
Lead.Email = '{!Contact.Email}';
Lead.Street = '{!Account.BillingStreet}';
Lead.City = '{!Account.BillingCity}';
Lead.PostalCode = '{!Account.BillingPostalCode}';
Lead.State = '{!Account.BillingState}';
Lead.Country = '{!Account.BillingCountry}';
Lead.Title = '{!Contact.Title}';
Lead.Description = '{!Contact.Description}';
Lead.OwnerId = '{!Account.OwnerId}';
Lead.Familiar_with_Company__c = '{!Account.Familiar_with_Company__c}';
Lead.Competition__c = '{!Account.Competition__c}';
Lead.Annual_Transportation_Spend__c = '{!Account.Annual_Transportation_Spend__c}';
Lead.Shipping_Methods__c = '{!Account.Shipping_Methods__c}';
Lead.Warehouse_Management_System_WMS__c = '{!Account.Warehouse_Management_System_WMS__c}';
Lead.Transportation_Management_System_TMS__c = '{!Account.Transportation_Management_System_TMS__c}';
Lead.Happy_with_Current_Provider__c = '{!Account.Happy_with_Current_Provider__c}';
Lead.Why_Why_Not_Happy_with_Current_Provider__c = '{!Account.Why_Why_Not_Happy_with_Current_Provider__c}';
Lead.DC_Locations__c = '{!Account.DC_Locations__c}';
Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
Lead.CS_Lead_Write_up__c = '{!Contact.CS_Lead_Write_up__c}';
Lead.Account_LookupId__c = '{!Account.Id}';
Lead.Contract_Expiration_Date__c = fixDate('{!Account.Contract_Expiration_Date__c}');

if('{!Account.Number_of_DCs__c}' == '')
   Lead.Number_of_DCs__c = null;
else
    Lead.Number_of_DCs__c = {!Account.Number_of_DCs__c};

result = sforce.connection.create([Lead]); 


if(result[0].success == 'true'){ 
alert('A New Lead was Created Successfully by {!$User.FirstName} {!$User.LastName}.'); 
}

function fixDate(date){

if(date == '' || date == null)
  return null;
var splittedDate = date.split('/'); 
var month = splittedDate[0] < 10 ? "0"+splittedDate[0] : splittedDate[0];
var day = splittedDate[1] < 10 ? "0"+splittedDate[1] : splittedDate[1];
  return splittedDate[2] + "-" + month + "-" + day + "T00:00:00Z";
}
Kim MoutesKim Moutes
Thank you so much for responding! I copied in your Script and received a JavaScript Error - Unexpected Token ;

Do I need to remove a semi-colon somewhere?
Pankaj_GanwaniPankaj_Ganwani
Okay, Just do this:

if('{!Account.Number_of_DCs__c}' == '')
   Lead.Number_of_DCs__c = null;
else
    Lead.Number_of_DCs__c = '{!Account.Number_of_DCs__c}';
This was selected as the best answer
Kim MoutesKim Moutes
Worked splendidly. Thank you again for all your help. Have a great weekend Pankaj!