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

Getting the null value in test class for RestRequest.params
Hello All,
Getting the null value in RestRequest.params.get in base class.
Apex Class Snippet:
Test Class:
Thanks.
VSK98
Getting the null value in RestRequest.params.get in base class.
Apex Class Snippet:
@RestResource(urlMapping='/getContactdetails/*') global class RESTServiceDemoClass { @HttpGet global static list<Contact> getContactList() { String strAccId = ''; String strLeadSource = ''; list<Contact> lstCons; RestRequest restReq = RestContext.request; RestResponse restRes = RestContext.response; // Reading parametrs from URL strAccId = restReq.params.get('accountId'); strLeadSource = restReq.params.get('leadSource'); if(!String.isBlank(strAccId) && !String.isBlank(strLeadSource)) { lstCons = [Select Id, Name, Email FROM Contact WHERE AccountId =:strAccId AND LeadSource =: strLeadSource]; } return lstCons; } }
Test Class:
RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/api/getContactdetails?'; req.addParameter('accountId', contact.accountid); req.addParameter('leadSource', 'test'); req.httpMethod = 'GET'; RestContext.request = req; RestContext.response = res;
Thanks.
VSK98
Can you try adding req.params.put in your test class method?
req.params.put('accountId', 'contact.accountid');
req.params.put('leadSource', 'test');
Saw this way of storing params value here https://salesforce.stackexchange.com/questions/190707/test-class-for-rest-api-get
Anudeep
Please try the below test class:
Thanks,
Maharajan.C