1

I'm looking to create a lead from an account using a button. When I go to create this lead, I get the following error. Can anyone tell me what is wrong with my approach/code?

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

var Lead = new sforce.SObject(Lead);

Lead.FirstName = 'XXX';
Lead.LastName = 'YYY';
Lead.Status = 'Unqualified';
Lead.Company = '{!Account.Name}';
Lead.Owner = '{!Account.OwnerId}';

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

if(result[0].success == 'true'){
    alert('An unqualified lead was successfully created.');
} 

enter image description here

1

Simple fix you have to pass sobject name as a string to to the sfroce.sobject

you are missing ""

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

Also as a best practice use success and failure messages to see what is going wrong.

if(result[0].getBoolean("success")){ 
//redirects/post save logic
} 
else{ 
alert( 
"Could not Create Record. Error: " + 
result[0].errors.message 
); 
}
| improve this answer | |
1

I have not worked with the SOAP API very much, but one approach (which I just implemented and checked in my DEV org) is to use a flow instead, and trigger this flow from the button. This way your logic would be completed using point and click config, and calling this flow would be a single line url in your button.

I used the following page as a general guide to figure it out: https://salesforcesidekick.com/2015/06/25/assign-to-me-button/

You would first need to create a flow with a single Record Create set as the Flow Start point (do this by clicking the green plus button that appears when hovering over it).

Set the Create Record to create a Lead, and then add each of the fields you want to populate under the Field section, and set them to new variables (naming the variables whatever you want).

options when adding Create Record step

Save your flow as an AutoLaunched Flow, and make sure to activate it. I called mine createLeadFromAccount.

Then when creating your button set it to be Detail Page Button and set the behaviour to Display in existing window without sidebar or header and set the Content Source to URL. Then for the URL set it to something like the following:

/flow/createLeadFromAccount?FirstName=XXX&LastName=YYY&LeadStatus=Unqualified&LeadOwnerId={!Account.OwnerId}&AccountName={!Account.Name}&retURL=/{!Account.Id}

This should then add a button to the page, that whenever it is clicked will run the flow, and generate the lead as required.

| improve this answer | |

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.