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
Bilal 25Bilal 25 

REST API Test class

Can someone help with Test class for below.

Thanks in Advance
 
@RestResource(urlMapping='/referrallead/*')
global with sharing class ReferalProgram {
   
    @HttpGet
    global static lead getLeadById() {
        //  global static opportunity getOpportunityById() {
        RestRequest request = RestContext.request;
        String leadId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
        
        lead result =  [SELECT lastname,firstname,email,Phone,status FROM lead WHERE Id = :leadId]; 
        return result;
    }
    
    @HttpPost
    global static ID createlead(String firstname, String lastname,String email, String phone, String company, String leadsource, String origin, String reffname, String reflname,String rectype) {
        
        lead thislead = new lead();
        List<Lead> leadlist = [select id,lastname,email from lead where email =: email];
        contact thiscontact = new contact();
        List<Contact> conList = [select id,name,email from Contact where email =: email];
        if(leadlist.size() >0 || conList.size() >0){
            thislead.Email.addError('Email already exists');
        }
        else
        thislead.Lastname = lastname;
        thislead.Firstname = firstname;
        thislead.Email = email;
        thislead.Company = company;
        thislead.phone = phone;
        thislead.leadsource = leadsource;
        thislead.origin__c = origin;
        thislead.Referrer_First_Name__c = reffname;
        thislead.Referrer_Last_Name__c = reflname;
        thislead.RecordTypeId = '012D00000007Q8D';
        insert thislead;
        return thislead.Id;
    }

    @HttpPatch
    global static ID updateleadFields() {
        RestRequest request = RestContext.request;
        String leadId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        lead thislead = [SELECT Id FROM lead WHERE Id = :leadId];
        // Deserialize the JSON string into name-value pairs
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
        // Iterate through each parameter field and value
        for(String fieldName : params.keySet()) {
            // Set the field and value on the Lead sObject
            thislead.put(fieldName, params.get(fieldName));
        }
        update thislead;
        return thislead.Id;
    }    
}

 
Best Answer chosen by Bilal 25
Santosh Kumar 348Santosh Kumar 348
Hi Moh,

Refer Below link it is very well explained, how you can write a class for Rest Resource.

https://apexcoder.com/2016/12/10/how-to-write-test-class-for-apex-rest-services/#:~:text=test%20class%3A%2D-,Test%20Class%3A%2D,POST%2C%20PUT%2C%20DELETE%20etc.

If it helped then can you please mark it as the best answer so that it can be used by others in the future.

Regards,
Santosh Kumar

All Answers

Santosh Kumar 348Santosh Kumar 348
Hi Moh,

Refer Below link it is very well explained, how you can write a class for Rest Resource.

https://apexcoder.com/2016/12/10/how-to-write-test-class-for-apex-rest-services/#:~:text=test%20class%3A%2D-,Test%20Class%3A%2D,POST%2C%20PUT%2C%20DELETE%20etc.

If it helped then can you please mark it as the best answer so that it can be used by others in the future.

Regards,
Santosh Kumar
This was selected as the best answer
Santosh Kumar 348Santosh Kumar 348
Hi Moh,

If my answer has helped you please cloae the thread by marking it as the best answer so that it can be used by others in the future.

Regards,
Santosh Kumar
Bilal 25Bilal 25
Dear Santosh,

Please help me to complete another issue.

https://developer.salesforce.com/forums/ForumsMain#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=9062I000000ISHdQAO

Thanks