+ Start a Discussion
jls_74_txjls_74_tx 

OnClick JavaScript - Multiple Rows - Single Child Record each Row

Warning: I'm a complete newbie to JavaScript.

I have a parent object floor_plan__c. I am trying to create a list button that will allow me to select multiple rows, and when clicked will create one child record on a custom object list__c for each of the floor_plan__c rows I have selected. The list__c object only requires two fields to be passed on: floor_plan__c.ID and floor_plan__c.renterID__c.

Problem #1: I can get the code below to work if I hard code the ID numbers. I don't know how to write a var statement to capture them dynamically.
Problem #2: No matter how many rows I click, only one child record is created for the first row I select.

I'm pretty sure this has something to do with an array? HELP!

Thank you in advance!
Jodi

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

//identify parent record
var floorplan = {!GETRECORDIDS($ObjectType.Floor_Plan__c)};
floorplan.id = "{!Floor_Plan__c.Id}";
floorplan.renter = "{!Floor_Plan__c.RenterID__c}";

//insert LIST Record
var list = new sforce.SObject("List__c");
list.Floor_Plan__c = floorplan.id;
list.Renter__c = floorplan.renter;
result = sforce.connection.create([list]);

var NewListID = result[0].id;
alert(NewListID);
Alain CabonAlain Cabon
Hi,

Could you try the code below? It is not the complete solution. I don't understand clearly what you expect.

var ids = {!GETRECORDIDS($ObjectType.Floor_Plan__c)}.toString().replace(/,/g , "','");   // Array of only Ids (selected records)
var result = sforce.connection.queryAll("Select RenterID__c, Id from Floor_Plan__c in ('"+ids+"')"; // get all the RenterID__c
var records = result.getArray("records");
for (var i=0; i<records.length; i++) {
  var record = records[i];
  alert(record.RenterID__c + " -- " + record.Id);
}

http://salesforce.stackexchange.com/questions/55966/query-for-a-list-of-ids-using-dynamic-soql-from-javascript
https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_more_samples.htm
https://www.sundoginteractive.com/blog/three-reasons-to-use-javascript-and-the-salesforce-api-behind-custom-button

Regards

Alain
Alain CabonAlain Cabon
var result = sforce.connection.queryAll("Select RenterID__c, Id from Floor_Plan__c where Id in ('"+ids+"')"; // get all the RenterID__c
jls_74_txjls_74_tx
Alain,

Thank you for your help and the resources. I used your code and then added my insert new records code.

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

var ids = {!GETRECORDIDS($ObjectType.Floor_Plan__c)}.toString().replace(/,/g , "','");   

// Array of only Ids (selected records)
var result = sforce.connection.queryAll("Select RenterID__c, Id from Floor_Plan__c where Id in ('"+ids+"')";

// get all the RenterID__c
var records = result.getArray("records");
for (var i=0; i<records.length; i++) {
  var record = records[i];
  alert(record.RenterID__c + " -- " + record.Id);
}

//insert LIST Record
var list = new sforce.SObject("List__c");
list.Floor_Plan__c = record.Id;
list.Renter__c = record.RenterID__c;
result = sforce.connection.create([list]);

var NewListID = result[0].id;
alert(NewListID);

It gives me an error: missing ) after argument list

I apologize I didn't explain my expectations clearly. Of course it sounds clear to me. LOL I understand the array you sent me to obtain the ID's. What I want next is to insert records on the List__c custom object.

This is my Floor_Plan__c custom object where the OnClick Button is located. In this example I have three rows selected with the Floor_Plan__c.ID and Floor_Plan__c.RenterID__c captured from the array you sent me.  NEXT....

User-added image

I want three records created on a child related list: List__c custom object, inserting the Floor_Plan__c.ID and Floor_Plan__c.RenterID__c fields into the new records. 

User-added image

I Googled missing ) in argument list and I tried JSLint to try to figure it out but I couldn't figure it out. Any ideas?

Thank you so much!!
Alain CabonAlain Cabon
Ok, it lacks a parenthesis here at the end of the line:

var result = sforce.connection.queryAll("Select RenterID__c, Id from Floor_Plan__c where Id in ('"+ids+"'))";

Each parenthesis must be closed.

Sorry, for the mistake (I didn't test the code).  

If you see this first result, you can understand if that works as you expect (perhaps not).

With your new explanations, we can help you.

What is the result of the alerts?

Alain
jls_74_txjls_74_tx
It almost works ;-) It captures multiple ID's correctly but it only creates one record. When I click the button, SalesForce pops up a window showing the ID's captured, but when I go to List__c to see the new records, there is only one record created. var list = new sforce.SObject("List__c"); must be incorrect.

Jodi
Alain CabonAlain Cabon
The second part of your code must be mixed with the first part (inside the loop):

var updateRecords = [];   // the list of records to update

for (var i=0; i<records.length; i++) {
    var record = records[i];
    alert(record.RenterID__c + " -- " + record.Id);
   // insert LIST Record = it is just one record of the list
    var list = new sforce.SObject("List__c");
    lst.Floor_Plan__c = record.Id;
    list.Renter__c = record.RenterID__c;
    updateRecords.push(list);    // push every single new list record in the updateRecords
}
result = sforce.connection.update(updateRecords); 

https://www.sundoginteractive.com/blog/three-reasons-to-use-javascript-and-the-salesforce-api-behind-custom-button

Is the result better?

Alain
jls_74_txjls_74_tx
Alain,

I've played with it and came up with the code below. It does everything I want it to do EXCEPT, it only inserts one record. It does not show an error message at all. When I run it, three alerts showing the ID numbers selected pop up, then the page refreshes BUT when I go to the related list, only 1 record is created.

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

var url = parent.location.href; 
var ids = {!GETRECORDIDS($ObjectType.Floor_Plan__c)}.toString().replace(/,/g , "','");
var result = sforce.connection.queryAll("Select RenterID__c, Id from Floor_Plan__c where Id in ('"+ids+"')");
var records = result.getArray("records");

var updateRecords = [];   // the list of records to update

for (var i=0; i<records.length; i++) {
    var record = records[i];
    alert(record.RenterID__c + " -- " + record.Id);
   // insert LIST Record = it is just one record of the list
    var list = new sforce.SObject("List__c");
    list.Floor_Plan__c = record.Id;
    list.Renter__c = record.RenterID__c;
    updateRecords.push(list);    // push every single new list record in the updateRecords
}
result = sforce.connection.create([list]);

if(result[0].getBoolean("success")){
parent.location.href = url;
}else{
alert('Could not create record '+result);
}

User-added image

If I select three rows, only the last row is inserted as a new record on the related list. It's something in my insert statement. I'm not sure why all three alerts would pop up with "success" but then only insert one record. Any ideas?

Thank you so much for all of your help!
Jodi
Alain CabonAlain Cabon
Hi Jodi,

The syntax of the call of create should be wrong (remove the brackets) and display the errors like below :

result = sforce.connection.create(list);
//Now check if there are any errors 
var hasErrors = false;
var errorReport = "The following List items could not be updated: "; 
 for (var j = 0; j < result.length; j++) 
 { 
    if (!result[j].getBoolean("success")) {
          hasErrors = true; 
          errorReport += ("List Id: " + updateRecords[j].Id + ", error: " + result[j].errors.message + " ");
          
          if (j == 10 && j < result.length) { 
               errorReport += ("Maximum errors to display. Will show first 10 out of " + "result.length ");
                break; 
          } 
    }

if (hasErrors) { 
    alert(errorReport); 


(I am not a javascript "champion", needless to say)

I hope this will be helpful to you.

Regards

Alain
Alain CabonAlain Cabon
Hi, 

If you have a suitable solution now with the help of people here or in other forums, I am interested in the result.

Your problem is not easy and the solution here is still incomplete.

Regards