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
DPhilDPhil 

Error creating record with currency field from a custom button

I'm using a custom button to create a new record in a custom object, but the code is failing when I try to insert a value which is type currency during the process.

I have created a custom object called "Project" with an api name of "Production__c".

I have added a custom button to the Opportunity page to create a new Project record and pre-populate it with some fields from the Opportunity.

It works fine until I try to add a value from a currency field, at which point it fails.

If I use no quotes around the value it gives the error "Unexpected number", if I use quotes around it then it give the error "'GBP 1,250' is not valid for the type xsd:double".

So I can't work out how to get the type right, any help would be much appreciated!

Here is the code:

==========

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}

var Proj = new sforce.SObject("Production__c");

Proj.Name = "{!Opportunity.Name}";
//Proj.Monthly_Revenue__c = 1234; **This works**
//Proj.Monthly_Revenue__c = '{!Opportunity.Monthly_Revenue__c}'; **This gives error 'GBP 1,250' not valid for type: xsd:double**
//Proj.Monthly_Revenue__c = {!Opportunity.Monthly_Revenue__c}; **This gives error 'unexpected number'**

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

if(result[0].success == 'true'){
    window.location = "/" + result[0].id + "/e";}
else {alert("Error creating Project");}

============
Best Answer chosen by DPhil
izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Ok, then try using:
moRev = moRev.substring(4);
moRev = moRev.replace(/,/g,'');
Proj.Monthly_Revenue__c = moRev;

or

moRev = moRev.substring(4);
moRev = moRev.replace(/,/g,'');
var moRevNum = parseFloat(moRev);
Proj.Monthly_Revenue__c = moRevNum;

All Answers

izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Try removing the 'GBP 1,250' from the value before assigning it. Ex:

var moRev = '{!Opportunity.Monthly_Revenue__c}';
moRev = moRev.substring(4);
Proj.Monthly_Revenue__c = moRev;
DPhilDPhil
That just gets the same error as I had when converting it to a string - "'1,250' is not valid for type: xsd:double" .  It is expecting a number...
izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Ok, then try using:
moRev = moRev.substring(4);
moRev = moRev.replace(/,/g,'');
Proj.Monthly_Revenue__c = moRev;

or

moRev = moRev.substring(4);
moRev = moRev.replace(/,/g,'');
var moRevNum = parseFloat(moRev);
Proj.Monthly_Revenue__c = moRevNum;
This was selected as the best answer
DPhilDPhil
That's great, both of those solutions worked perfectly, much appreciated!

Now to get the date fields working... :-)

Thanks for your help.