+ Start a Discussion
salesforce instancesalesforce instance 

Want test class for this code and it should cover 75%

@RestResource (urlMapping='/LeadbyLivServ/*')
global with sharing class LeadbyLivServ {
    @HttpPost
    global static string createLead(){
        String response  = '';
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        Map<String,String> paramsGot = req.params;
        System.debug('PARAMS GOT :'+paramsGot);
        System.debug('BODY GOT :'+(req.requestBody).toString());
        String callJsonData = (req.requestBody).toString();
        System.debug('callJsonData---'+callJsonData);
        JSONParser parser = JSON.createParser(callJsonData);
        Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(callJsonData);
        Lead leadRecord = new Lead();
        leadRecord.FirstName = ''+m.get('first_name');
        leadRecord.LastName = ''+m.get('last_name');
        leadRecord.Email = ''+m.get('email');
        leadRecord.MobilePhone = ''+m.get('mobile');
        leadRecord.Phone = ''+m.get('mobile');
        leadRecord.Description = EncodingUtil.urlDecode(''+m.get('description'),'UTF-8');
        leadRecord.Date_Of_Enquiry__c = System.today();
        List<Lead> existingLead = new List<Lead>();
        JSONGenerator gen = JSON.createGenerator(true);
        gen.WriteStartObject();
        if(existingLead.size() > 0){
            gen.WriteStringField('success','failure');  
            gen.WriteFieldName('errors');
            gen.writeStartArray();
            System.debug('99999'+leadRecord.Email.equalsIgnoreCase(''+m.get('email')));
            if(existingLead[0].Email.equalsIgnoreCase(''+m.get('email'))){
                gen.writeStartObject();
                gen.writeStringField('email','Already exists');
                gen.writeEndObject();
            }
            System.debug('00000'+leadRecord.MobilePhone.equalsIgnoreCase(''+m.get('mobile')));
            if(existingLead[0].MobilePhone.equalsIgnoreCase(''+m.get('mobile'))){
                gen.writeStartObject();
                gen.writeStringField('mobile','Already exists');
                gen.writeEndObject();
            }
            gen.writeEndArray();
        }else{
        
            // to turn the Assignment Rules on
            Database.DMLOptions dmo = new Database.DMLOptions();
            //dmo.assignmentRuleHeader.useDefaultRule = true;
            dmo.assignmentRuleHeader.assignmentRuleId = '01Q10000000UHEi';
            leadRecord.setOptions(dmo);  
            insert leadRecord;
            gen.WriteStringField('success','success');
            gen.WriteStringField('errors','');
        }
        gen.writeEndObject();
        response = gen.getAsString();
        System.debug('RESPONSE IS :'+response);        
           
           return response;
    }
}
Shashikant SharmaShashikant Sharma
Try to create the test class using this : http://forceschool.blogspot.in/2011/06/testing-apex-structure-of-test-class.html

Let me know if you face issues implementing.