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
Vishal K SVishal K S 

Need test class help for below class

Hi ,

Can anyone help test class for below.


@RestResource(urlMapping='/dcleads')
global without sharing class DC_RestResourceCls {
    
    @HttpPost
    global static PostResponseWrapper doPost() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        PostResponseWrapper response = new PostResponseWrapper();
       
        intreq__c integrationObj = New intreq__c();
        try {
            LeadWrapper LeadWrapperObj = (LeadWrapper)JSON.deserialize(req.requestBody.toString(), LeadWrapper.class);
            
            integrationObj.Request_Body__c = req.requestBody.toString();
            List<lead> ld = [SELECT id,APIMOBILE__c,Call_Stage__c,Status,DC_Lead_Status__c,CreatedDate,convertedAccountId,convertedOpportunityId,
                             Follow_Up_Date_Time__c,FirstName,LastName,Meeting_Type__c,Meeting_Venue__c,Email,Country_Code__c,Home_Type__c,
                             Property_Possession_Status__c,Preferred_Time_to_Contact__c,Voucher_Code__c,Page_URL__c,Project_Name__c,OwnerId,
                             City,GClid__c,Area__c,Device_Name__c,Campagin__c,Ad_Group__c,Keyword__c,Match_Type__c,First_Visit_Time__c
                             from Lead WHERE APIMOBILE__c =:LeadWrapperObj.Mobile Limit 1];
            IF(ld.size() != 0){ 
                
                
                if((ld[0].Status == 'Followup' && LeadWrapperobj.dsaid!=Null && (ld[0].Follow_Up_Date_Time__c < system.today().addDays(-29) || ld[0].LastModifiedDate < system.today().addDays(-29)))||
                   (ld[0].Call_Stage__c == 'Undialed' && LeadWrapperobj.dsaid!=Null && ld[0].LastModifiedDate < system.today().addDays(-29))||
                   (ld[0].Status == 'Junk' && LeadWrapperobj.dsaid!=Null)||
                   (ld[0].DC_Lead_Status__c =='Qualified' && LeadWrapperobj.dsaid!=Null && (ld[0].Follow_Up_Date_Time__c < system.today().addDays(-29) || ld[0].LastModifiedDate < system.today().addDays(-29)))
                  )
                  {
                //---------------- Attribution DSA(1) ------------------------------//
                //---------------- Logic 1 ------------------------------//
                }
                else if(LeadWrapperobj.dsaid!=Null){
                    Lead LeadObj = new Lead();
                    LeadObj.id = ld[0].id;
                    LeadObj.DC_Lead_Status__c = 'Recontacted';
                    LeadObj.Re_Contact_Date__c = System.now();   
                    update LeadObj;
                    response.message = 'Updated Successfully -1[Record Already Exists]';
                    response.status  = 'Success';
                    response.recordId = LeadObj.id;
                }    
                
                }}}}
CharuDuttCharuDutt
Hii Sfdc Learner
Try Below Test Class
@isTest
public class DC_RestResourceClsTest {
	@isTest
	public static void dataSetup() {
       lead l = new lead();
        l.Status = 'Followup';
        l.Call_Stage__c = 'Undialed';
        l.DC_Lead_Status__c ='Qualified';
        l.Follow_Up_Date_Time__c = system.today().addDays(-25);
        /*Fill All Required Fields*/
            insert l;
	
    
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/dcleads/';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response= res;
        lead Resp = DC_RestResourceCls.doPost();
    }
}
Please Mark It As Best Answer if it Helps
Thank You!