1

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

  • are you getting error in this line of alert. 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); – Suresh Jun 11 '15 at 13:32
0

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.

  • hi Daniel, the quote that i working on have line Item. and it success get the result. my goal is to create PO(custom object) under Quote. after po inserted under quote, all quote line item will we copied to po line item(custom object). the process already success to create record for PO under Quote. also success to query detail of quote line item to insert in PO line Item object. but no record is created under PO line item. – Kiunx Jun 12 '15 at 2:46
  • Thanks for the clarification. I can see that you managed to solve the problem yourself, can't believe I didn't think of that, nice catch. In any case, you should mark your answer as the correct one so that others who stumble across this question can easily find the solution. – Daniel Bernsons Jun 12 '15 at 13:44
0

Here I am suspecting the error is in the query. Query should look like this.

var query = "SELECT product_name__c,Discount,Description, LineNumber, ListPrice, Quantity, QuoteId, Id, UnitPrice, Subtotal, TotalPrice FROM QuoteLineItem WHERE QuoteId =" + quote.id;

  • hi Suri, Query Success Return quote line Item. my goal is to create PO(custom object) under Quote. after po inserted under quote, all quote line item will we copied to po line item(custom object). the process already success to create record for PO under Quote. also success to query detail of quote line item to insert in PO line Item object. but no record is created under PO line item. – Kiunx Jun 12 '15 at 2:46
  • result2=sforce.connection.create([createRecords]); place alert statement and see weather this createRecords is as expected and I suggest you to create the same using the dev console what ever the data is in the createRecords. So you may know if any fields are missing – Suresh Jun 12 '15 at 3:18
0

its done. the query should be in array. so code updated to:

var records = sforce.connection.query(query); 
var records1 = records.getArray('records');

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.