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
Jonas MyhreJonas Myhre 

Unexpected identifier, custom JavaScript button

Hey, im trying to create a Javascript button that creates a custom object from Opportunity. The code i have so far is probably not the best, i've pasted it below. I get "Unexpected Identifier" error message.

Thanks for help!

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

var billing = new sforce.SObject('Billing__c');

{!Opportunity.Name} = "{!Billing__c.Name}";
{!Opportunity.Contact_Id__c}="{!Billing__c.Contact_Person__c}";
{!Opportunity.AccountId}="{!Billing__c.Account_nameId__c}";


result = sforce.connection.create([billing]);
Fabien TaillonFabien Taillon
If you want to create a Billing from an Opportunity, you should do the opposite, populate Billing fields with Opportunity fields.
Also, you should use the javascript variable billing you created, and not Salesforce object :

billing.Name = "{!Opportunity.Name}";
billing.Contact_Person__c = "{!Opportunity.Contact_Id__c}";
billing.Account_nameId__c = "{!Opportunity.AccountId}";

Jonas MyhreJonas Myhre
Thanks Fab, the error message has dissapeared! No record is created now though, do you have any idea why? I also have an intention to redirect the users to the record, how would i do this?
Fabien TaillonFabien Taillon
Are you filling in all the mandatory fields on your object ?
You can check on your browser's console that there isn't any javascript error, or try to display result with alert(result);
Then once it work you can easily redirect with the following line :
window.location.href = '/'+result[0].id;
Jonas MyhreJonas Myhre
Yes, mandatory fields are those 3 we copy from Opportunity. The error message i get in developer console is : "refused to set unsafe header "user-agent"". I've tried to update Chrome, and use Internet Explorer, but still no luck.
Fabien TaillonFabien Taillon
This message shouldn't be a problem. And if you display result, with alert(result) or console.log(result) ? 
Jonas MyhreJonas Myhre
I dont have a result variable defined, how would i do this?
Fabien TaillonFabien Taillon
Did you remove by mistake the last line ?

result = sforce.connection.create([billing]);
Jonas MyhreJonas Myhre
Oh yes haha. My bad. The error message now is pasted below. The field is a lookup field, i get the same error message for Account if i remove the contact line in my code. Are there any issues with lookup field here? They are both lookup fields.:





 User-added image
Fabien TaillonFabien Taillon
Well, it seems the Contact Id you are trying to put in your new object is null ! Maybe the source field hasn't any value on your Opportunity.
Jonas MyhreJonas Myhre
There is a contact there. Do i need some other fields to be transfered as well? 

User-added image
Fabien TaillonFabien Taillon
This field is you custom field Contact_Id__c ?
What is "Beredskap Test Kommune" displayed in the error message ?