1

Problem Summary

I added a Create Contract Javascript Button to the Opportunity Page Layout. Once a user clicks on the button, it should create a new Contract with data populated from the Opportunity.

Layout

Layout Screenshot

Error

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client', faultstring:''USD 2.00' is not valid for the type xsd:dobule', }

Button Script

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

var contract = new sforce.SObject('Contract'); 
contract.Net_License__c = '{!Opportunity.Net_License__c }';
contract.Net_Maintenance_Amount__c = '{!Opportunity.Maintenance_Amount__c}';
contract.Net_Consulting_Amount__c = '{!Opportunity.Services_Amount__c }';
contract.Net_Training_Amount__c = '{!Opportunity.Net_Training_Amount__c}';
contract.Deal_Total_cal__c = '{!Opportunity.P_Amount__c}';
contract.OppOpp__c = '{!Opportunity.Opportunity_No__c}';

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

if(result[0].success == 'true') {
    window.location = '/' + result[0].id ;
}
else {
    alert('Error');
}
6
  • This error indicates the value 'USD 2.00' is not a valid number. Probably because you have 'USD' in there. What is the data type of the fields you are trying to assign each value? Do they match the data type of the opportunity fields?
    – Berg
    Sep 10, 2015 at 11:19
  • @Berg - all are Currency (16, 2) in both ends (Opportunity and Contract). Rregarding USD, in my org multiple currency feature is enabled, so on selection bases the currency will automatically change.
    – Nandu Raja
    Sep 10, 2015 at 11:21
  • Ok, can you test without 'USD' in your opportunity fields? Try using your button with just '2.00' as each value.
    – Berg
    Sep 10, 2015 at 11:29
  • @berg - getting the same error in the currency field ---> {Faultcode: 'soapenv: Client', faultstring: "$2.00" is not valid for the type xsd:double',}
    – Nandu Raja
    Sep 10, 2015 at 13:28
  • @SachinKumar drop the $
    – Eric
    Sep 10, 2015 at 13:54

1 Answer 1

0

At a basic level, all you have to do is replace all the non-digit characters (except .) so that you can parse the String into a Float. The resulting regex is /[^\.\d]/g. Try adopting the following approach:

{!REQUIRESCRIPT('/soap/ajax/33.0/connection.js')} 
var regex = new RegExp('[^\\.\\d]', 'g'),
    sanitize = new function (currency) {
        return parseFloat(currency.replace(regex, ''));
    },
    contract = new sforce.SObject('Contract'),
    result;

contract.Net_License__c = sanitize('{!Opportunity.Net_License__c }';
contract.Net_Maintenance_Amount__c = sanitize('{!Opportunity.Maintenance_Amount__c}');
contract.Net_Consulting_Amount__c = sanitize('{!Opportunity.Services_Amount__c }');
contract.Net_Training_Amount__c = sanitize('{!Opportunity.Net_Training_Amount__c}');
contract.Deal_Total_cal__c = sanitize('{!Opportunity.P_Amount__c}');
contract.OppOpp__c = sanitize('{!Opportunity.Opportunity_No__c}');

result = sforce.connection.create([contract]);
if(result[0].success == 'true') {
    window.location = '/' + result[0].id;
}
else {
    alert('Error');
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .