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
BangoBango 

Quote Button not working

Hello all, I created this button on the Quote object to create QuoteLineItems but it's not working. Below is the code BUT please see my note below the code for some clarification:

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

var pricebookid = "01s60000000AaPr"; 
var quoteId = "{!Quote.Id}"; 
var quoteQuery = sforce.connection.query("Select Id, Name FROM Quote WHERE Id='" + quoteId + "'"); 
var quotyResult = quoteQuery.getArray("records"); 
quotyResult[0].Pricebook2Id = pricebookid; 
var updatequotyResult = sforce.connection.update([quotyResult[0]]); 


var result = sforce.connection.query("Select Id,Name,HE__c,HE_Qauntity__c,Price_Book_Entry_ID__c From Product2 Where HE__c = TRUE"); 
var records = result.getArray("records"); 

var CreateRecords=[]; 

if(records[0]==null) 
alert('No Product Set is Retrieved!'); 
for(var i=0;i<records.length;i++) 

var linky=new sforce.SObject("QuoteLineItem"); 
linky.QuoteID = '{!Quote.Id}'; 
linky.PriceBookEntryId = records[i].Price_Book_Entry_ID__c; 
linky.Quantity = records[i].HE_Qauntity__c; 
linky.UnitPrice = 1; 
linky.Quoting_Tool__c = 1; 
CreateRecords.push(linky); 


result=sforce.connection.create([CreateRecords]); 
alert(result); 


window.location.reload();


// The code works fine if I create them one by one within the loop but I wanted to create them at once from an array. For example, if I put 

result=sforce.connection.create([linky]);

in the loop it works fine but slow. That's whay I wanted to go the other way around to create them at once from an array. Please help. 
Best Answer chosen by Bango
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Are you getting any error? Also, please make sure you have added all the required scripts.
<script type="text/javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}'; 
</script>
<script src="/soap/ajax/35.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/35.0/apex.js" type="text/javascript"></script>
Also, I would like you to try it like this:
result=sforce.connection.create(CreateRecords); 
 

All Answers

SalesFORCE_enFORCErSalesFORCE_enFORCEr
Are you getting any error? Also, please make sure you have added all the required scripts.
<script type="text/javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}'; 
</script>
<script src="/soap/ajax/35.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/35.0/apex.js" type="text/javascript"></script>
Also, I would like you to try it like this:
result=sforce.connection.create(CreateRecords); 
 
This was selected as the best answer
BangoBango
Thank you :) The removal of the backets did it :) Silly me ;)