function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VSK98VSK98 

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:
 
@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​​​​​​​
AnudeepAnudeep (Salesforce Developers) 
Hi 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

 
Maharajan CMaharajan C
HI VSK,

Please try the below test class:

 
@isTest
public class RestServiceDemoClassTest {
     @testSetup
    static void dataSetup() {
       // Add all the Account Mandatory fields to create the new account. 
        Account acc = new Account(Name = 'Testing');
        insert acc;
        
      // Add all the Contact Mandatory fields to create the new contact and use your correct lead source in below.
        contact con = new contact(LastName = 'Test contact',Email='TestEmail@test.com',LeadSource='Web',AccountId=acc.Id);
        insert con;
    }

    static testMethod void testGet() {
        Account acc = [ SELECT Id FROM Account LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/getContactdetails';
        req.httpMethod = 'GET';
        req.addParameter('accountId', acc.Id);
        //use your correct lead source in below as per the contact record.
        req.addParameter('leadSource', 'Web');
        RestContext.request = req;
        RestContext.response= res;
        List<Contact> conResp = RestServiceDemoClass.getContactList();
    }

}



Thanks,
Maharajan.C