[SalesForce] Salesforce Custom Button OnClick Javascript – Create multiple child record

I have Error for my custom button javascript for create multiple child record.
my code is:

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
//identify parent record
var quote = new sforce.SObject("Quote");
quote.id = "{!Quote.Id }";
quote.name = "{!Quote.Name}";

//insert PO Record
var po = new sforce.SObject("Purchase_Order__c");
//po.Name = quote.name;
po.Quote__c = quote.id;
result = sforce.connection.create([po]);
var NewPoID = result[0].id;
alert(NewPoID);

//get quotelineitem
var query = "SELECT  product_name__c,Discount,Description, LineNumber, ListPrice, Quantity, QuoteId, Id, UnitPrice, Subtotal, TotalPrice FROM QuoteLineItem WHERE QuoteId = '" + quote.id + "'"; 
var records = sforce.connection.query(query); 
alert(records);
var createRecords = [];

if(records[0]==null)
    alert('no records to insert');
for(var i=0;i<records.length;i++){
    var poItem = new sforce.SObject("Purchase_Order_Line_Item__c");
    poItem.Purchase_Order__c = NewPoID;
    poItem.product_name__c = records[i].product_name__c;
    poItem.Quantity__c = records[i].Quantity;
    poItem.Discount__c = records[i].Discount;
    poItem.List_Price__c = records[i].ListPrice;
    poItem.Sales_Price__c = records[i].UnitPrice;
    createRecords.push(poItem);
}
result2=sforce.connection.create([createRecords]);
alert(result2);
//redirect to detail
//window.location.reload();

error on the 3rd allert'no records to insert'
the parent record already success and list of record that need to create under new parent record is already correct.
but when insert, the process is failed.

please help in what i missed on my code.

willy

Best Answer

It looks very much like the Quote that you're working with has no QuoteLineItem objects associated with it.

Based on just the code here, it's not really possible to tell whether this is correct or not - your code here is not inserting these QuoteLineItem objects, so I don't know if that query should have returned any data or not.

Related Topic