Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@RestResource(urlMapping='/abc/xyz/*')
global class MyClass{
@HttpPost
global static void updateMethod(){
try {
//get the json from the request
RestRequest req1=RestContext.request;
String jsonInput= req1.requestBody.toString();
//create map for the obtained json input
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(jsonInput);
Id opptyId = String.valueOf(jsonMap.get('opportunity_SF_Id')) ;
//get the opportunity object
Sobject oppty = [SELECT id, description FROM opportunity WHERE id=:opptyId];
//some more processing
update oppty;
}catch(Exception e){
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
}
Test Class:
@IsTest
private class MyClassTest
{
static testMethod void MyClassMethod()
{
Account acc = new Account();
acc.Name='Test';
acc.AccountNumber ='12345';
insert acc;
Opportunity mAWSUsage = new Opportunity( Name = 'mAWS Usage',
AccountId = acc.Id,
StageName = 'Negotiations',
CloseDate = System.today(),
Type = 'New Business - Add',
Amount = 555888555
);
insert mAWSUsage;
RestRequest request = new RestRequest();
request.requestUri ='/services/apexrest/abc/xyz/';
request.httpMethod = 'POST';
RestContext.request = request;
MyClass.updateMethod();
//System.assert(acc != null);
}
}
Please refer to the below links which might help you further with the above requirement.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Greetings to you!
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Test Class:
Please refer to the below links which might help you further with the above requirement.
http://www.vinaychaturvedi.com/blog/learn-how-to-test-your-rest-apis-in-salesforce/
http://www.infallibletechie.com/2018/07/sample-inbound-rest-api-test-class-in.html
https://developer.salesforce.com/forums/?id=9060G000000XdDoQAK
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas