-1

Unexpected parameter encountered during deserialization: Name

@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {

    @HttpDelete
    global static void doDelete() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        try {
            String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            Account account = [SELECT Id FROM Account WHERE Id = :accountId LIMIT 1];
            if (account != null) {
                delete account;
                res.statusCode = 204; // No Content
            } else {
                res.statusCode = 404; // Not Found
                res.responseBody = Blob.valueOf('Error: Account not found');
            }
        } catch (Exception e) {
            res.statusCode = 500; // Internal Server Error
            res.responseBody = Blob.valueOf('Error: Unable to delete the account');
        }
    }

    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        try {
            String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId LIMIT 1];
            if (result != null) {
                return result;
            } else {
                res.statusCode = 404; // Not Found
                res.responseBody = Blob.valueOf('Error: Account not found');
                return null;
            }
        } catch (Exception e) {
            res.statusCode = 500; // Internal Server Error
            res.responseBody = Blob.valueOf('Error: Unable to retrieve the account');
            return null;
        }
    }

    @HttpPost
    global static String doPost(String name, String phone) {
        RestResponse res = RestContext.response;
        RestRequest req = RestContext.request;
        try {
            Account account = new Account();
            account.Name = name;
            account.Phone = phone;
            insert account;
            return account.Id;
        } catch (Exception e) {
            res.statusCode = 500; // Internal Server Error
            res.responseBody = Blob.valueOf('Error: Unable to create the account');
            return null;
        }
    }

    @HttpPatch
    global static void doPatch(String requestBody) {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
       
         System.debug(req.requestURI);
        System.debug(req.requestBody);
        try {
            String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
            System.debug(accountId);
            Map<String, Object> patchData = (Map<String, Object>) JSON.deserializeUntyped(requestBody);
            Account account = [SELECT Id, Phone FROM Account WHERE Id = :accountId LIMIT 1];
          
           if (account != null) {
                if (patchData.containsKey('Phone')) {
                    account.Phone = String.valueOf(patchData.get('Phone'));
                    update account;
                    res.statusCode = 200; 
                    res.responseBody = Blob.valueOf('Account updated successfully');
                } else {
                    res.statusCode = 400;
                    res.responseBody = Blob.valueOf('Error: Phone parameter missing in the JSON.');
                }
            } else {
                res.statusCode = 404; 
                res.responseBody = Blob.valueOf('Error: Account not found');
            }
        } catch (Exception e) {
            res.statusCode = 500;
            res.responseBody = Blob.valueOf('Error: Unable to update the account');
        }
    }
}
/* from workbench  Rest Explore */

URL :- /services/apexrest/Account/

{
   "Name":"NANAA",
   "Phone":"9504438571"
}
1
  • Best you add System.debug('requestBody=' + requestBody); to understand the problem.
    – Keith C
    Commented Jun 7 at 8:59

3 Answers 3

0

When using Apex Rest with method parameters, Salesforce automatically deserializes the POST payload into the variables. In order to do this, you need to provide the correct format.

For XML:

<?xml version="1.0" encoding="UTF-8"?>
<request>
  <name>NANAA</name>
  <phone>9504438571</phone>
</request>

And for JSON:

{ 
    "name": "NANAA",
    "phone": "9504438571"
}

Please note that the parameters are case-sensitive. In other words, Name is not valid here, but name is.

To specify the format, use the Content-Type header, either application/xml or application/json. See the documentation for more details.

1
  • Thank you for your valuable answer
    – Rahul
    Commented Jun 7 at 12:24
0

Your JSON is in this format:

{
   "Name":"NANAA",
   "Phone":"9504438571"
}

That means "Name" and "Phone" becomes your attributes and are case sensitive. Always ensure the JSON keys are case-matched accordingly.

You need to change your apex post method as

global static String doPost(String Name, String Phone) {

or you need to change the JSON to :

{
   "name":"NANAA",
   "phone":"9504438571"
}
1
  • thank you for the correction
    – Rahul
    Commented Jun 7 at 12:26
0

In your doPost() method, you're expecting name and phone parameters as strings. If the JSON payload contains a parameter other than name and phone, like Name, the deserialization process fails because it's expecting a different structure.

To fix this issue, ensure that the JSON payload sent in the POST request contains only the expected parameters (name and phone) and that their names match exactly with what you're expecting in the Apex code.

For example, the JSON payload should look like this:

{
   "name": "Some Name",
   "phone": "1234567890"
}

You must log in to answer this question.

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