-3

Hi i am trying to write class for wrapper class , we just know that how we can cover the all wrapper class . please find the below code and give the solution test class .

@RestResource(urlMapping='/createUpdateLeads/*')
global class ABOnlineLead{

    // This Method use to Create new lead .
    @HttpPost 
    global static ResponseMessagesWrapper createLeadsFromOnline( LeadWrapper jsonLeads ){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        Lead NewLd = new Lead();

        NewLd.ExternalLeadId__c = jsonLeads.LeadId;NewLd.RecordTypeId = jsonLeads.RecordTypeId ;NewLd.FirstName = jsonLeads.firstName;
        NewLd.LastName = jsonLeads.LastName;NewLd.MobilePhone = jsonLeads.mobilePhone;NewLd.OptedPlan__c = jsonLeads.optedPlanName ;
        NewLd.Status = jsonLeads.Status ;NewLd.Street  = jsonLeads.billingStreet;NewLd.City = jsonLeads.billingcity;
        NewLd.State = jsonLeads.billingstate ;
        NewLd.PostalCode = jsonLeads.billingpostalCode ;NewLd.Country = jsonLeads.billingcountry ;
        NewLd.InstallationStreet__c = jsonLeads.installationStreet ;NewLd.InstallationCity__c = jsonLeads.installationCity ;
        NewLd.InstallationState__c = jsonLeads.installationState ;
        NewLd.Installation_PostalCode__c = jsonLeads.installationPostalCode;NewLd.InstallationCountry__c = jsonLeads.installationCountry;
        NewLd.Latitude__c = jsonLeads.latitude; NewLd.Longitude__c = jsonLeads.longitude;
        NewLd.FeasibilityStatus__c = jsonLeads.feasibilityStatus; NewLd.FeasiblePlans__c = jsonLeads.feasiblePlans ;
        NewLd.Email = jsonLeads.email ;NewLd.Company = jsonLeads.Company ;NewLd.FinalProductName__c = jsonLeads.finalProductName ;
        NewLd.FinalPlanName__c = jsonLeads.finalPlanName ;NewLd.FinalBandwidth__c = jsonLeads.finalBandwidth;
        NewLd.FinalPrice__c = jsonLeads.finalPrice;NewLd.FinalActivationCharge__c = jsonLeads.finalActivationCharge;
        NewLd.FinalDiscount__c = jsonLeads.finalDiscount ;NewLd.Channel__c = jsonLeads.channel ;
        NewLd.LOB__c = jsonLeads.lob ;NewLd.OrderId__c = jsonLeads.orderId ;NewLd.ESBStatus__c = jsonLeads.esbStatus ;
        NewLd.ESBProspectId__c = jsonLeads.esbProspectId;NewLd.ReservedField1__c = jsonLeads.ReservedField1;
        NewLd.ReservedField2__c = jsonLeads.ReservedField2;NewLd.ReservedField3__c = jsonLeads.ReservedField3;
        NewLd.ReservedField4__c = jsonLeads.ReservedField4;

        ResponseMessagesWrapper response = new ResponseMessagesWrapper();

        try{
            insert NewLd;

            response.status ='Success';
            response.message = 'Record created successfully with Id: '+NewLd.Id;
        }
        catch (Exception ex){
            response.status = 'Failure. Please check Header for Error Code and Message for Salesforce Error.';
            response.message = ex.getMessage();
        }
        return response;        
    }

    // This Method use to update Lead record 
    @HttpPatch
    global static ResponseMessagesWrapper updateLeadFromOnline( LeadWrapper jsonLeads ) {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        String LeadId = jsonLeads.LeadId;    

        Lead LD= [ Select Id, Company from Lead where ExternalLeadId__c =:LeadId limit 1];

        if( !isBlankOrNull(jsonLeads.LeadId) )LD.ExternalLeadId__c = jsonLeads.LeadId; 
        if( !isBlankOrNull(jsonLeads.RecordTypeId) )LD.RecordTypeId = jsonLeads.RecordTypeId ;
        if( !isBlankOrNull(jsonLeads.firstName) )LD.FirstName = jsonLeads.firstName;
        if( !isBlankOrNull(jsonLeads.LastName) )LD.LastName = jsonLeads.LastName;
        if( !isBlankOrNull(jsonLeads.mobilePhone) )LD.MobilePhone = jsonLeads.mobilePhone;
        if( !isBlankOrNull(jsonLeads.optedPlanName) )LD.OptedPlan__c = jsonLeads.optedPlanName ;
        if( !isBlankOrNull(jsonLeads.Status) )LD.Status = jsonLeads.Status ;
        if( !isBlankOrNull(jsonLeads.billingStreet) )LD.Street  = jsonLeads.billingStreet;
        if( !isBlankOrNull(jsonLeads.billingcity) )LD.City = jsonLeads.billingcity;
        if( !isBlankOrNull(jsonLeads.billingstate) )LD.State = jsonLeads.billingstate ;
        if( !isBlankOrNull(jsonLeads.billingpostalCode) )LD.PostalCode = jsonLeads.billingpostalCode ;
        if( !isBlankOrNull(jsonLeads.billingcountry) )LD.Country = jsonLeads.billingcountry ;
        if( !isBlankOrNull(jsonLeads.installationStreet) ) LD.InstallationStreet__c = jsonLeads.installationStreet ;
        if( !isBlankOrNull(jsonLeads.installationCity) )LD.InstallationCity__c = jsonLeads.installationCity ;
        if( !isBlankOrNull(jsonLeads.installationState) ) LD.InstallationState__c = jsonLeads.installationState ;
        if( !isBlankOrNull(jsonLeads.installationPostalCode) ) LD.Installation_PostalCode__c = jsonLeads.installationPostalCode;
        if( !isBlankOrNull(jsonLeads.installationCountry) )LD.InstallationCountry__c = jsonLeads.installationCountry;
        if( jsonLeads.latitude != null )LD.Latitude__c = jsonLeads.latitude;
        if( jsonLeads.longitude!= null )LD.Longitude__c =  jsonLeads.longitude;
        if( !isBlankOrNull(jsonLeads.feasibilityStatus) )LD.FeasibilityStatus__c = jsonLeads.feasibilityStatus; 
        if( !isBlankOrNull(jsonLeads.feasiblePlans) )LD.FeasiblePlans__c = jsonLeads.feasiblePlans ;
        if( !isBlankOrNull(jsonLeads.email) )LD.Email = jsonLeads.email ;
        if( !isBlankOrNull(jsonLeads.Company) )LD.Company = jsonLeads.Company ;
        if( !isBlankOrNull(jsonLeads.finalProductName) )LD.FinalProductName__c = jsonLeads.finalProductName ;
        if( !isBlankOrNull(jsonLeads.finalPlanName) )LD.FinalPlanName__c = jsonLeads.finalPlanName ;
        if( !isBlankOrNull(jsonLeads.finalBandwidth) )LD.FinalBandwidth__c = jsonLeads.finalBandwidth;
        if( jsonLeads.finalPrice!= null )LD.FinalPrice__c = jsonLeads.finalPrice;
        if( jsonLeads.finalActivationCharge!= null )LD.FinalActivationCharge__c = jsonLeads.finalActivationCharge;
        if( jsonLeads.finalDiscount != null )LD.FinalDiscount__c = jsonLeads.finalDiscount ;
        if( !isBlankOrNull(jsonLeads.channel) )LD.Channel__c = jsonLeads.channel ;
        if( !isBlankOrNull(jsonLeads.lob) )LD.LOB__c = jsonLeads.lob ;
        if( !isBlankOrNull(jsonLeads.orderId) )LD.OrderId__c = jsonLeads.orderId ;
        if( !isBlankOrNull(jsonLeads.esbStatus) )LD.ESBStatus__c = jsonLeads.esbStatus ;
        if( !isBlankOrNull(jsonLeads.esbProspectId) )LD.ESBProspectId__c = jsonLeads.esbProspectId;
        if( !isBlankOrNull(jsonLeads.ReservedField1) )LD.ReservedField1__c = jsonLeads.ReservedField1;
        if( !isBlankOrNull(jsonLeads.ReservedField2) )LD.ReservedField2__c = jsonLeads.ReservedField2;
        if( !isBlankOrNull(jsonLeads.ReservedField3) )LD.ReservedField3__c = jsonLeads.ReservedField3;
        if( !isBlankOrNull(jsonLeads.ReservedField4) )LD.ReservedField4__c = jsonLeads.ReservedField4;

        ResponseMessagesWrapper response = new ResponseMessagesWrapper();

        try {
            update Ld;
            response.status ='Success';
            response.message = 'Record Updated successfully with Id: '+Ld.Id;
        }
        catch (Exception ex){
            response.status = 'Failure. Please check Header for Error Code and Message for Salesforce Error.';
            response.message = ex.getMessage();
        }
        return response;

    }

    static boolean isBlankOrNull( String fieldName){

        if (fieldName == null || fieldName == '')   return true;
        else
            return false;
    }

    //Wrapper class to return Lead object 
    global class LeadWrapper{

        global String LeadId{ get; set; }
        global String RecordTypeId{ get; set; }
        global String firstName { get; set; }
        global String LastName{ get; set; }
        global string mobilePhone { get; set; }
        global String optedPlanName{ get; set; }
        global String Status{ get; set;}
        global String installationStreet{ get; set; }
        global String installationCity{ get; set; }
        global String installationState{ get; set; }
        global String installationPostalCode{ get; set; }
        global String installationCountry{ get; set; }
        global integer latitude{ get; set; }
        global integer longitude{ get; set; }
        global String feasibilityStatus{ get; set; }
        global String feasiblePlans{ get; set; }
        global String email{ get; set; }
        global String Company{ get; set; }
        global String billingStreet{ get; set; }
        global String billingcity { get; set; }
        global String billingstate{ get; set; }
        global String billingpostalCode{ get; set; }
        global String billingcountry{ get; set; }
        global String finalProductName{ get; set; }
        global String finalPlanName{ get; set; }
        global String finalBandwidth{ get; set; }
        global integer finalPrice{ get; set; }
        global integer finalActivationCharge{ get; set; }
        global integer finalDiscount{ get; set; }
        global String channel{ get; set; }
        global String lob{ get; set; }
        global String orderId{ get; set; }
        global String esbStatus{ get; set; }
        global String esbProspectId{ get; set; }
        global String ReservedField1{ get; set; }
        global String ReservedField2{ get; set; }
        global String ReservedField3{ get; set; }
        global String ReservedField4{ get; set; }
        //public String status;
        //public String message;      
    }
    //Wrapper class to return response status and message
    global class ResponseMessagesWrapper{
        public String status;
        public String message;      
    }

}  
6
  • Did you tried anything. Because your question look like you are asking to write test class for you. – Tushar Sharma Sep 7 '16 at 6:21
  • yes , but we are facing problem at this method createLeadsFromOnline( LeadWrapper jsonLeads ) i have no idea how its to be cover .can you help – Narey Jagwan Sep 7 '16 at 6:25
  • Can you share your test class code also what error you are facing. – Tushar Sharma Sep 7 '16 at 6:26
  • i 'm trying somethings lead Creater = new lead(Company='online test',Status='Draft',LastName='test'); "ABOnlineLead.ResponseMessagesWrapper results = new ABOnlineLead.createLeadsFromOnline(Creater); " and also getting this error "Invalid type: ABOnlineLead.createLeadsFromOnline" – Narey Jagwan Sep 7 '16 at 6:27
  • According to your code, ABOnlineLead.createLeadsFromOnline(Creater) would be a method invocation, hence there is a syntax error if you try to use new keyword before it. Also, Creater seems to be a Lead, but your method is expecting a LeadWrapper instance. – Joca Sep 7 '16 at 6:44
1

Try this:

Lead ld = new Lead(Company='online test',LastName='test'); 
insert ld;

ABOnlineLead.LeadWrapper ldw = new ABOnlineLead.LeadWrapper();
ldw.LeadId = ld.Id;
ldw.LastName = 'test';
.....
//Instantiate values for all the variables in the LeadWrapper class
.....

ABOnlineLead.ResponseMessagesWrapper res = ABOnlineLead.createLeadsFromOnline(ldw);

Not the answer you're looking for? Browse other questions tagged or ask your own question.