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
sree prasadsree prasad 

Hi Please reply fast

How To write test class to this class

@RestResource(urlMapping='/LeadMapping/*')
global with sharing class Rest_Lead_Insert {

@HttpPost  
  global static String CreateNewLead(String lastname, String status,String Call_duration, string Service_Number, string AgentPhone, string Ofc_Call_Status,string Ofc_Call_Time,string office_lead_id,string AgentName,string Recording_File_Path,Boolean Office24by7,string call_type){
     System.debug('lead Name: '+lastname);
     System.debug('lead Status: '+status);
     try
     {
         lead le                    =          new lead();
         le.lastname                =          lastname;
         le.Office_Number__c        =          Service_Number;
         le.Phone                   =          AgentPhone;
         le.Ofc_Call_duration__c    =          Call_duration;
         le.Ofc_Call_Type__c        =          call_type;
         le.Ofc_Call_Status__c      =          Ofc_Call_Status;
         le.Ofc_Call_Time__c        =          Ofc_Call_Time;
         le.office_lead_id__c       =          office_lead_id;
         le.AgentName__c            =          AgentName;
         le.Recording_File_Path__c  =          Recording_File_Path;
         le.Office24by7__c          =          Office24by7;
         le.status                  =          status;
         insert le;
         return 'lead has been inserted succesfully: '+le.Id;
     }
     catch(DMLException de)
     {
        return de.getDmlMessage(0);
     }
               
    // return null;
  }
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sree,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement. As I don't have custom fields in lead object, so I have tested with standard fields.

Class:
@RestResource(urlMapping='/LeadMapping/*')
global with sharing class HttpPostLead {
    
    @HttpPost  
    global static String CreateNewLead(String lastname, String status, string AgentPhone, string company){
        System.debug('lead Name: '+lastname);
        System.debug('lead Status: '+status);
        try
        {
            lead le                    =          new lead();
            le.lastname                =          lastname;
            le.Phone                   =          AgentPhone;
            le.status                  =          status;
            le.Company                 =          company;
            insert le;
            return 'lead has been inserted succesfully: '+le.Id;
        }
        catch(DMLException de)
        {
            return de.getDmlMessage(0);
        }
    }
}

Test Class:
@istest
public class Test_HttpPostLead {
    
    static testMethod void  updateMethodTest1(){
        Test.startTest();
        HttpPostLead.CreateNewLead('TestLead1', 'Working - Contacted', '9999999999', 'TestComp1');
        Test.stopTest();
    }
    
    static testMethod void  updateMethodTest2(){
        Test.startTest();
        HttpPostLead.CreateNewLead('', 'Working - Contacted', '9999999999', 'TestComp1');
        Test.stopTest();
    }
}

According to your class, you can use below test class:
 
@istest
public class Test_HttpPostLead {
    
    static testMethod void  updateMethodTest1(){
        Test.startTest();
        HttpPostLead.CreateNewLead('TestLead1', 'Working - Contacted', '10', '1234', '9999999999', 'Call Status', '8', '123456789123456', 'Khan', 'File_Path', true, 'Call Type');
        Test.stopTest();
    }
    
    static testMethod void  updateMethodTest2(){
        Test.startTest();
        HttpPostLead.CreateNewLead('', 'Working - Contacted', '10', '1234', '9999999999', 'Call Status', '8', '123456789123456', 'Khan', 'File_Path', true, 'Call Type');
        Test.stopTest();
    }
}

Note: Please enter the fields name according to your org and type of field in CreateNewLead parameters. Also, please add other required fields if needed.

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
Raj VakatiRaj Vakati
Try this 
 
@istest
public class MyClassHTTPPost{

 static testMethod void  updateMethodTest(){

   lead le                    =          new lead();
         le.lastname                =          'Test';
         le.Office_Number__c        =          '123123213';
         le.Phone                   =          '213123123';
         le.Ofc_Call_duration__c    =          '213';
         le.Ofc_Call_Type__c        =          'Phone';
         le.Ofc_Call_Status__c      =          'Status';
         le.Ofc_Call_Time__c        =          'Time';
         le.office_lead_id__c       =          'lead';
         le.AgentName__c            =          'Test';
         le.Recording_File_Path__c  =          'locatopm.pdf';
         le.Office24by7__c          =          '213123';
         le.status                  =          'new';
         insert le;
		 
		 
   Test.startTest();

   RestRequest req = new RestRequest(); 
   RestResponse res = new RestResponse();

      String JsonMsg=JSON.serialize(le);

    req.requestURI = '/services/apexrest/LeadMapping/*';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    RestContext.request = req;
    RestContext.response= res;
    Rest_Lead_Insert.CreateNewLead();
    Test.stopTest();

   }
}

 
sree prasadsree prasad
Thankyou so much for your replies,.... 
am getting this error            Method does not exist or incorrect signature: void CreateNewLead() from the type Rest_Lead_Insert