-1

I developed an Apex REST web service in Salesforce with the method 'POST' and i am trying to insert the string value in currency field of Product__c object, so when I try to hit the API im getting the following error message as "System.TypeException: Invalid decimal: 30,000\n\nClass.MG_RestClass.createRecord"

Here is the class

public class Product {
    public String pName;
    public String pRating;
    public String pAmount;
    public List<Item> Items;
}

public class Item{
    public String Name;
    public String Code;
}
@HttpPost
global static String createRecord() {
    
    RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String jSONRequestBody=req.requestBody.toString();
        Map<String,Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jSONRequestBody);
        String root = JSON.serialize(jsonMap.get('Product'));
        List<Product> Products = (List<Product>)JSON.deserialize(root,Product[].class);
    
       for(Product lstProducts : Products ){
             Product__c objP = Product__c;
             objP.Amount__c = Decimal.ValueOf(lstProducts.pAmount);//getting error at this line of code->System.TypeException: Invalid decimal
              // Other logic for inserting records 
          }

below is the json structure

{
    "Product": [{
        "pName": "ProdName", 
        "pRating": "cold", 
        "pAmount": "30,000" ,
    "Item":[                                 
        {                      
        "Name": "Item Name" ,
        "Code" : "54abt1cd294" 
        }
     ],
    }]
}

anyone knows how to resolve this

3
  • What is the JSON you're working with here? if pAmount contains a grouping separator (e.g. 30,000.00 with NA-style separators, or 30.000,00 with EU-style separators), then that's not something Decimal.valueOf() can handle (additional processing would be required)
    – Derek F
    Dec 20, 2021 at 17:21
  • Can you post the JSON you are passing to this REST API? Dec 20, 2021 at 17:22
  • 1
    @BryanAnderson I have posted a JSON structure
    – user104634
    Dec 21, 2021 at 3:15

1 Answer 1

3

The error is telling you exactly what is wrong.

System.TypeException: Invalid decimal: 30,000

30,000 is not a valid numeric literal in Apex. (The comma, specifically, is disallowed).

Consider changing your service so that the caller provides an actual JSON number value, or at minimum a string value that can be parsed by Apex.

2
  • In addition, it'd be better to convert this to a decimal client-side, and then pass in that decimal directly.
    – sfdcfox
    Dec 20, 2021 at 17:22
  • Cool, Thanks for your suggestions
    – user104634
    Dec 21, 2021 at 3:06

You must log in to answer this question.