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

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");}
============
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");}
============
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
GBP1,250' from the value before assigning it. Ex:var moRev = '{!Opportunity.Monthly_Revenue__c}';
moRev = moRev.substring(4);
Proj.Monthly_Revenue__c = moRev;
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;
Now to get the date fields working... :-)
Thanks for your help.