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
etoeto 

How to pass currency field from Page Button to Apex Webservice?

I have a pagebutton on the detail view of e.g. an Opportunityy. This button should pass a currency or a number value to an apex webservice method. In my example, I just take the AMount field.

 

The system however always sends a full string to the method, never a decimal (what I would have had expected).Typical error then is: {faultcode:'soapenv:Client', faultstring:''$235,000.00' is not a valid value for the type xsd:decimal', }

 

Any idea how I can pass a currency/number field from a button to the Apex method without it being converted to a formatted string?

 

Button:

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

result = sforce.apex.execute("DummyClass","convertNumber", {value:"{!Opportunity.Amount}"});
alert(result);

 Webservice class:

 

global with sharing class DummyClass {

	WebService static String convertNumber(Decimal value) {
		return 'did apply some magic to ' + String.valueOf(value);
	}

}

 

kiranmutturukiranmutturu

change the arguement type to string ....and try it

etoeto

That won't solve it as it still keeps the formatting. E.g. then I get a String that looks like "$235,000.00" which still is not a decimal. Depending on the locale/currency it could also be "235.000,00€".

 

So the question is still: can I pass a number/currency field value to the webservice?

Rahul SharmaRahul Sharma

I guess you want the Currency in "$235,000.00" format, even if its in any other format like "235.000,00€".

Then you have to parse the decimal data as a string, I'm not aware of any other easy method which would do the same.

etoeto

No, that was just an example to make clear I can't simply accept a string as the string will be different for every locale or currency available on this planet.

 

Problem is: I have a fleld in an object which is of type number or type currency. When I use this field in my code, Salesforce formats the number/currency to a String.

 

I would like to know if I can pass the number/currency field as a decimal and not as a string (as the field is a decimal and not  a string).

 

 

 

 

Rahul SharmaRahul Sharma

I know this is not an exact solution, but will work for most of the cases:

 

 

Button:

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

result = sforce.apex.execute("DummyClass","convertNumber", {value:"{!Opportunity.Amount}"});
alert(result);

 Webservice class:

 

global with sharing class DummyClass 
{
  WebService static String convertNumber(String value)
  {
    if(value != null && value != '')
    {
      value = value.replace('$','');
      value = value.replace('.','');
      value = value.replace(',','');
      value = value.substring(0,value.length() - 2) + '.' + value.substring(value.length() - 2,value.length());
     
      system.debug('==value==:'value);
      return 'did apply some magic to ' + Decimal.valueOf(value);
    }
    else
      return 'Value is null';
  }
}

Let me know if the doesn't work...

etoeto

Thanks a lot for your effort, but such a hack does not help me.  I'm still looking if I miss something so I can send a currency or number field value to a apex webservice method.

 

Your solution might be helpful in very specific situations, but I'm looking for a general solution as it has to work in any ORG as part of an installable package. Your solution will not work for any other currency but USD. It will also not work for numbers (if they have more or less than 2 decimal places.)

 

 

 

 

 

 

Rahul SharmaRahul Sharma

Yes, you are right..