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
Rajesh SFDCRajesh SFDC 

how to solve thiserror:using javascript in list button, lead is not defined

I have a requirement like, need to select multiple records on the list view. After done with the selection of records. If I click the List view custom button, those selected records must be created into another object. here my code giveb below
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
var selectedLeads = {!GETRECORDIDS($ObjectType.Lead)}, index = 0; leads, newobjs = {}, newrec, saveresults;

if(selectedLeads[0]== NULL)
{
    alert("select a record atleast");
    return;
}

leads = sforce.connection.retrieve('Id,Name', 'Lead', selectedLeads);

while(index < leads.length)
{
    newrec = new sforce.SObject('position__c');

    newrec.candidate__c = leads[index].Name;

    newrec.job_description__c = leads[index].Name;

    newobjs.push(newrec);

    index += 1;
}
saveresults = sforce.connection.create(newobjs);
error: lead is not defined 
Rahul SharmaRahul Sharma
Hi,

I tried to rewrite the code, In onclick javascript return do not mean return like a function.
Try this and let me know if there are any issues!

***************************************Code Start***************************************
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
var selectedLeads = {!GETRECORDIDS($ObjectType.Lead)};

if(selectedLeads.length === 0)
{
    alert("select a record atleast");
}
else
{
    var index = 0, newobjs = {}, saveresults;
    var leads = sforce.connection.retrieve('Id,Name', 'Lead', selectedLeads);
   
    while(index < leads.length)
    {
        var newrec = new sforce.SObject('position__c');

        newrec.candidate__c = leads[index].Name;

        newrec.job_description__c = leads[index].Name;

        newobjs.push(newrec);

        index += 1;
    }
    saveresults = sforce.connection.create(newobjs);
   
}
***************************************Code End***************************************
Rajesh SFDCRajesh SFDC
again i am getting an error: object has no method'' push
Rahul SharmaRahul Sharma
Try this:

***************************************Code Start***************************************
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
var selectedLeads = {!GETRECORDIDS($ObjectType.Lead)};

if(selectedLeads.length === 0)
{
    alert("select a record atleast");
}
else
{
    var index = 0, newobjs = new Array(), saveresults;
    var leads = sforce.connection.retrieve('Id,Name', 'Lead', selectedLeads);
  
    while(index < leads.length)
    {
        var newrec = new sforce.SObject('position__c');

        newrec.candidate__c = leads[index].Name;

        newrec.job_description__c = leads[index].Name;

        newobjs.push(newrec);

        index += 1;
    }
    saveresults = sforce.connection.create(newobjs);
  
}
***************************************Code End***************************************
Rajesh SFDCRajesh SFDC
now its working thanks, but one thing, suppose i am going to change the name for previous nam='rajesh', suppose i am change the rajesh to rahul in lead object, that name doesnot update in another object. please help me and also only one time click on button , suppose i am click 2 times on button.. its creating same record to another object,. i want to create only one time on another object. how to solve it rahul