You need to sign in to do that
Don't have an account?

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.
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
here is updated code for you,
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
here is updated code for you,
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
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?
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
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
var result = sforce.connection.insert(CreateNewRecords);
as CreateNewRecords is array , so this is correct syntax
Please read my previous comment too
Cheers,
Bala
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
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...