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"
}
System.debug('requestBody=' + requestBody);
to understand the problem.