1

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. Its similar kind of LEAD Conversion button but I need for Bulk Conversion. DO any one have sample codes or logic for this requirement. I also viewed the below page, its some what similar. But I don't need to have a VF page.

Creating a custom list-view button that handles multi-record selection

Looping : https://salesforce.stackexchange.com/users/286/andrew-fawcett

EDIT: My Code.. I am started to learn doing coding. So please bare my mistakes in codes. Now the problem in Code is : In Destination object records are not created. The selected source object records should be created in destination record, after clicking the button. All the field values need to be captured dynamically. But I am not able to get the result...

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}     
var url = parent.location.href;       
var records = {!GETRECORDIDS($ObjectType.SourceObject__c)}; 
var newRecords = [];
if (records[0] == null) 
{ 
    alert("Please select at least one record to create.");     
} 
else 
{ 
    for (var a=0; a<records.length; a++) 
    {              
        var newDestinationrecords = new sforce.SObject("DestinationObject__c");
        newDestinationrecords.id = records[a]; 
        newDestinationrecords.test__c = 'test'; // Hard code data
        newRecords.push(newDestinationrecords);  
    }    
    result = sforce.connection.create(newRecords); 
    parent.location.href = url;
}

Thank you for your response.. Regards Syed

  • 2
    Welcome to SFSE. It would help if you shared what yopu have tried so far. This shows the group that you have tried this yourself and aren't just looking for people to write code for you. There are a lot of great people on here willing to help, but questions like these often times will be ignored, as it seems there hasn't been any effort put in prior to asking the question. Please edit the question with what you have tried so far and where you are having issues. – Chris Duncombe Jan 9 '14 at 14:22
2

Generically speaking, it could be something like this:

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
var selectedLeads = {!GETRECORDIDS($ObjectType.Lead)}, index = 0; leads, newobjs = {}, newrec, saveresults;
if(!selectedLeads.length) {
    return; // Alert a message here if you want.
}
leads = sforce.connection.retrieve('Id,FirstName,LastName,Email', 'Lead', selectedLeads);
while(index < leads.length) {
    newrec = new sforce.SObject('CustomType__c');
    newrec.FieldA__c = leads[index].FirstName;
    newrec.FieldB__c = leads[index].LastName;
    newobjs.push(newrec);
    index += 1;
}
saveresults = sforce.connection.create(newobjs);

I leave error handling to you, and there are other means of achieving this, but this is one possible method. sforce.apex.execute would be another alternative, deferring the logic to server-side code, which may be easier to develop.

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.