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
QuiqueprQuiquepr 

List Button problem creating new opportunities from custom object getting error on literal string

Hello:

 

I am creating a list button to mass create new opportuies based on records from a custom object.  I am in a bind to get this into production early next week and would appreciate the communities help...

 

We have a policy sales record (PSR) custom object  and need to create renewal opportunities from selected records in a policy sales record  list view.  I have very little Javascript knowledge but using several postings from the boards & blogs I came up with the following code that throws the following error after selecting the policies in the view and clicking on the button to execute.  

 

When clicking onthe button we want to create new opportunities of a specific record type and prepopulate sevaral key records from the values on the policy sales record.

 

 

Error message in a windows  dialog box:
A problem with the OnClick JavaScript for this button or link was encountered:
unterminated string literal
I have reviewed the code and cannot find where these error seems to be coming from as the code in my limited knowledge seem properly formed.
Also, I am not sure if the insert portion is the correct syntax for Javascript to push the insert of the new records.

 

 

Here is the code for my button:

 

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}    //adds the proper code for inclusion of AJAX toolkit
var url = parent.location.href;    //string for the URL of the current page
var records = {!GETRECORDIDS($ObjectType.Policy_Sales_Record__c)};    //grabs the Policy Sales Records records that the user is requesting to update
var CreateNewRecords = [];    //array for holding records that this code will ultimately update

if (records[0] == null) { //if the button was clicked but there was no record selected
	alert("Please select at least one record to update.");    //alert the user that they didn't make a selection 
} else {   //otherwise, there was a record selection
	for (var a=0; a<records.length; a++) {    //for all records
		var create_new_opportunity = new sforce.SObject("opportunity");    //create a new sObject for storing updated record details
		create_new_opportunity.RecordTypeId = "012A0000000R4UX";    //set the record type to Renewal Opportunities
		create_new_opportunity.AccountId = "{!Policy_Sales_Record__c.Account__c}";    //set the account ID value from PSR 
		create_new_opportunity.Closedate = "{!Policy_Sales_Record__c.Policy_Renewal_Date__c}";     //set the value for renewal date from policy data
		create_new_opportunity.OwnerId = "{!Policy_Sales_Record__c.Account_Owner__c}";    //set the value for account owner
		create_new_opportunity.Primary_Producer__c = "{!Policy_Sales_Record__c.Producer__c}";    //set the value for the producer
		create_new_opportunity.Renewal_Base_Premium__c = "{!Policy_Sales_Record__c.Renewal_Base_Premium__c}"
		create_new_opportunity.Renewal_Base_No_of_Contracts__c = {!Policy_Sales_Record__c.Renewal_Base_No_of_Contracts__c}";

		CreateNewRecords.push(create_new_opportunity);   //add the updated record to our array
	}
	result = sforce.connection.insert(CreateNewRecords);    //insert new records into to Salesforce
	parent.location.href = url; //refresh the page
}

 

 

Regards and thanks in advance

 

Quique

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

here is updated code for you,

 

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")};
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Policy_Sales_Record__c)};
var CreateNewRecords = []; 
try
{

if (records[0] == null)
{
	alert("Please select at least one record to update.");
} 
else
 {
	for (var a=0; a<records.length; a++) 
	{
		var create_new_opportunity = new sforce.SObject("opportunity");
		create_new_opportunity.RecordTypeId = "012A0000000R4UX"; 
		create_new_opportunity.AccountId = "{!Policy_Sales_Record__c.Account__c}"; 
		create_new_opportunity.Closedate =new Date("{!Policy_Sales_Record__c.Policy_Renewal_Date__c}");
		create_new_opportunity.OwnerId = "{!Policy_Sales_Record__c.Account_Owner__c}";   
		create_new_opportunity.Primary_Producer__c = "{!Policy_Sales_Record__c.Producer__c}";
		create_new_opportunity.Renewal_Base_Premium__c = "{!Policy_Sales_Record__c.Renewal_Base_Premium__c}";
		create_new_opportunity.Renewal_Base_No_of_Contracts__c = {!Policy_Sales_Record__c.Renewal_Base_No_of_Contracts__c}";
		CreateNewRecords.push(create_new_opportunity);
	}
var result = sforce.connection.insert(CreateNewRecords);
if (result[0].success=='false')
{
	alert(result[0].errors.message);
}
elese
{
	location.reload(true);
}

}
}
catch(error)
{
alert("Error In Opportunity creation  = "+error);
}

 

 

I wrote the script in proper way ,

Handle the result ,

Line in green was causing error in your case 

Also I had update red line [date format]

Alway surround your code within try catch 

 

*** one suggestion don't go for inline comment ,

If want to write comment add them on separate line within

/* comment here */

 

 

Cheers ,

Bala

All Answers

b-Forceb-Force

here is updated code for you,

 

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")};
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Policy_Sales_Record__c)};
var CreateNewRecords = []; 
try
{

if (records[0] == null)
{
	alert("Please select at least one record to update.");
} 
else
 {
	for (var a=0; a<records.length; a++) 
	{
		var create_new_opportunity = new sforce.SObject("opportunity");
		create_new_opportunity.RecordTypeId = "012A0000000R4UX"; 
		create_new_opportunity.AccountId = "{!Policy_Sales_Record__c.Account__c}"; 
		create_new_opportunity.Closedate =new Date("{!Policy_Sales_Record__c.Policy_Renewal_Date__c}");
		create_new_opportunity.OwnerId = "{!Policy_Sales_Record__c.Account_Owner__c}";   
		create_new_opportunity.Primary_Producer__c = "{!Policy_Sales_Record__c.Producer__c}";
		create_new_opportunity.Renewal_Base_Premium__c = "{!Policy_Sales_Record__c.Renewal_Base_Premium__c}";
		create_new_opportunity.Renewal_Base_No_of_Contracts__c = {!Policy_Sales_Record__c.Renewal_Base_No_of_Contracts__c}";
		CreateNewRecords.push(create_new_opportunity);
	}
var result = sforce.connection.insert(CreateNewRecords);
if (result[0].success=='false')
{
	alert(result[0].errors.message);
}
elese
{
	location.reload(true);
}

}
}
catch(error)
{
alert("Error In Opportunity creation  = "+error);
}

 

 

I wrote the script in proper way ,

Handle the result ,

Line in green was causing error in your case 

Also I had update red line [date format]

Alway surround your code within try catch 

 

*** one suggestion don't go for inline comment ,

If want to write comment add them on separate line within

/* comment here */

 

 

Cheers ,

Bala

This was selected as the best answer
QuiqueprQuiquepr

Bala tks a lot for your help, I notices you had a misspelled elsee statement that i corrected, but I still get the same error after updating th button code.  Any ideas?

 

 

b-Forceb-Force

I am sorry for "elsee" ,

 

In previous case, I didnt observer  that you are trying to crating opportunity records from selected policy records

 

In case of List button we are having only selected record id (comma sepereted list ) Not a set off whole record,

 

[We have whole record in case of detail page button]

 

 

So in order to implement your requirement,

 

You need to retrive first all records by SOQL

then iterating through you need to create a opportunity list

then insert it

 

 

If you need any more info ping me @   balasahebwani@gmail.com

 

 

Thanks,

Bala

QuiqueprQuiquepr

 

Is this the right syntax/command?

 

var result = sforce.connection.insert(CreateNewRecords);

 

or do i use

 

var result = sforce.connection.update(CreateNewRecords); 

 

instead, even if I am creating new records?

 

Tks a million....very very close!!!

Quique

b-Forceb-Force

var result = sforce.connection.insert(CreateNewRecords);

 

as CreateNewRecords is array , so this is correct syntax

 

Please read my previous comment too

 

 

Cheers,

Bala

rahulsharmarahulsharma

I know the post is too old, but then also i would like to post to help others:

 

Proper syntax to insert a record is:

var result = sforce.connection.create(CreateNewRecords);

 

Thanks

 

Rahul

DevWannabeDevWannabe

I know this is old, but I am hoping somebody see this.  I am doing a very similar operation as described in this post and I used the solution, adjusted to my situation, but I can't seem to get it to work.

 

The error I am getting while in Chrome, whether I select records on the related list or not, is..."A problem with the OnClick JavaScript for this button or link was encountered:  Unexpected token ;"

 

I get a similar error in Firefox, instead of "Unexpected token ;" I get a "Syntax error".

 

The only difference in the process that I can think of is the original post was created a standard object record from a custom object list of records, where I am creating a custom object record from a standard object list of records.

 

Here is my code...

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

var url = parent.location.href;
var records = {!GETRECORDIDS( $ObjectType.OpportunityLineItem )};
var CreateNewRecords = [];

try
{

if (records[0] == null) {
   alert("Please select at least one record to update.");
} else {
   for (var a=0; a<records.length; a++) {
         var newTPC = new sforce.SObject("Talent_Placement_Confirmation__c");
         newTPC.Opportunity__c = "{!OpportunityLineItem.Opp_Id__c}";
         newTPC.Rate__c = {!OpportunityLineItem.Cost_Num__c};
         newTPC.Resource__c= "{!OpportunityLineItem.ResourceId__c}";
         newTPC.Services_to_be_Provided__c = "{!Product2.Name}";
         newTPC.Units__c = {!OpportunityLineItem.Unit__c};
         CreateNewRecords.push(newTPC);
         }
var result = sforce.connection.insert(CreateNewRecords);
         if (result[0].success=='false') { 
         alert(result[0].errors.message); 
          } else{
location.reload(true);
}
}
}
catch(error)
{
alert("Error In Talent Placement Confirmation creation = "+error);
}