You need to sign in to do that
Don't have an account?

How to pass parameters in request body for test class
Request body :
{
"StartDate": "2019-08-01",
"EndDate": "2019-12-03"
}
Method : Post
Need to pass these parameters in my test class not sure how i cna do this
Test clasS:
static testMethod void testPost() {
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/WorkdayIntegration/';
req.httpMethod = 'POST';
RestContext.request = req;
//req.requestBody=('2019/05/05','2019/06/05');
RestContext.response= res;
APTS_InvoiceDetailsWS.APTS_WorkdayIntegrationWrapper results = APTS_InvoiceDetailsWS.getInvoiceDetails();
}
Method :
{
"StartDate": "2019-08-01",
"EndDate": "2019-12-03"
}
Method : Post
Need to pass these parameters in my test class not sure how i cna do this
Test clasS:
static testMethod void testPost() {
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/WorkdayIntegration/';
req.httpMethod = 'POST';
RestContext.request = req;
//req.requestBody=('2019/05/05','2019/06/05');
RestContext.response= res;
APTS_InvoiceDetailsWS.APTS_WorkdayIntegrationWrapper results = APTS_InvoiceDetailsWS.getInvoiceDetails();
}
Method :
String str = '{"StartDate": "2019-08-01","EndDate": "2019-12-03"}';
req.requestBody = Blob.valueof(str);
http://cloudyworlds.blogspot.com/2012/12/writing-test-classes-for-apex-rest.html
Thanks,
Maharajan.C
But after tryng this getting below error :
System.JSONException: Unexpected character ('+' (code 43)): was expecting comma to separate OBJECT entries at [line:1, column:30]
Stack TraceClass.System.JSON.deserializeUntyped: line 11, column 1
Class.APTS_InvoiceDetailsWS.getInvoiceDetails: line 17, column 1
Class.APTS_InvoiceDetailsWS_test.testPost: line 26, column 1
Web Service Class :
@RestResource(urlMapping='/WorkdayIntegration/')
global with sharing class APTS_InvoiceDetailsWS
{
@HttpPost
global static APTS_WorkdayIntegrationWrapper getInvoiceDetails()
{
Date startDate;
Date endDate;
integer countHeader;
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString()); // fetch parameters from request body
startDate=Date.valueOf((String)params.get('StartDate'));
endDate=Date.valueOf((String)params.get('EndDate'));
String approved='Approved';
String query1='Select id,Name,EQ_TotalInvoiceAmount__c,EQ_InvoiceApprovedDate__c,Apttus_Billing__TotalDueAmount__c,Apttus_billing__status__c,CurrencyIsoCode,Apttus_Billing__ShipToAccountId__r.EQ_ClientRef__c,Apttus_Billing__ShipToAccountId__r.EQ_ShippingState__c,Apttus_Billing__InvoiceDate__c,EQ_DueDateCustom__c,EQ_TotalTaxAmount__c from Apttus_Billing__Invoice__c where EQ_InvoiceApprovedDate__c>=:startDate and EQ_InvoiceApprovedDate__c<=:endDate and EQ_TotalInvoiceAmount__c > 0 and Apttus_billing__status__c=:approved';
List<Apttus_Billing__Invoice__c> invoiceObjectList1=Database.query(query1);
APTS_WorkdayIntegrationWrapper intWrap= new APTS_WorkdayIntegrationWrapper();
List<APTS_InvoiceWrapper> listToStoreInvoices = new List<APTS_InvoiceWrapper>();
Integer count;
Please try this :
Stiill Same error , dont know what i am missing
System.JSONException: Unexpected character ('+' (code 43)): was expecting comma to separate OBJECT entries at [line:1, column:30]
Stack TraceClass.System.JSON.deserializeUntyped: line 11, column 1
Class.APTS_InvoiceDetailsWS.getInvoiceDetails: line 17, column 1
Class.APTS_InvoiceDetailsWS_test.testPost: line 26, column 1
Please check the line number at which your are getting this error, as I have compiled this code in my org and it ran properly till line - String approved='Approved';
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString());
I am not sure if this is the correct format to get the parameters from req body.