0

Main Class:

@RestResource(urlMapping='/cases/*')
global with sharing class InsertCaseHelper {
    global static string  responsePayLoad;

    @HTTPPOST  
    global static void insertCase(){
        RestRequest req;
        RestResponse res;
        String jsonPayLoad;  
        try{ 

            req = RestContext.request;
            res = RestContext.response;
            jsonPayLoad=req.requestBody.toString();
            responsePayLoad=jsonPayLoad;
            CaseJsonParser obj=CaseJsonParser.parse(jsonPayLoad);

            if(!String.isBlank(responsePayLoad)){   //check json is blank 

                    List<recordtype> recrodtypeList;
                        recrodtypeList=[select recordtype.id from recordtype where recordtype.name=:obj.cse.recTypeId limit 1]; 
                    if(recrodtypeList.size() > 0) {  

                        case  Newcase=new case();                
                        System.debug('inside IF::');
                        Newcase.status=obj.cse.Status;
                        Newcase.Origin=obj.cse.Origin;
                        Newcase.RecordTypeId=recrodtypeList[0].Id;
                        Newcase.For_a_Business_or_Residential_Customer__c=obj.cse.customerType;
                        Newcase.Reason_for_Completing_this_AIM__c=obj.cse.reasonForCompletingAIM;
                        Newcase.Service_Id__c=obj.cse.serviceId;

                                Insert Newcase; 
                    }
                    else
                    {
                        createExceptionLog(system.label.Recordtype_Error,'400');

                    }
                }
            }
            else
            {
                createExceptionLog(system.label.Blank_Payload,'400');
            }       
        }
        catch(Exception e){
            //createExceptionLog(system.Label.Recordtype_Error,400, System.Label.Failed_To_Process_Request,jsonPayLoad);            
            createExceptionLog(String.valueOf(e),'500');

        }       
    }

    public static void createExceptionLog(String errorMessage, string errorCode){
        String [] ExceptionLog  = new String [] {'Source_Name__c',
            'Destination_System__c','End_Point_URL__c','HTTP_Status_Code__c','Interface_Name__c','Status__c',
            'Correlation_ID__c','Error_Description__c'};
        Exception_Log__c ex1 = new Exception_Log__c();        
        ex1.Source_Name__c = 'AIMS';

            insert ex1; 
    }
}

JSON Cls:

   public class CaseJsonParser {

    public  cls_case cse;

    public class cls_case {
        public string status;
        public string Origin;
        public string recTypeId;
        public string customerType;
        public string reasonForCompletingAIM;
        public string serviceId;
        public string enquiryType;


    }

    Public static CaseJsonParser parse(string json){
        return (CaseJsonParser) System.JSON.deserialize(json, CaseJsonParser.class); 
    }

}

Instead of this requirement is to create inner Jsonclass under main class.Please suggest

1

Just put the class inside the other class, and move the inner class and static methods outside of the class, like this:

@RestResource(urlMapping='/cases/*')
global with sharing class InsertCaseHelper {
  public class CaseJsonParser {
    public  cls_case cse;
  }
  public class cls_case {
    ...
  }
  Public static CaseJsonParser parse(string json){
    return (CaseJsonParser) System.JSON.deserialize(json, CaseJsonParser.class); 
  }
  @HTTPPOST  
  global static void insertCase(){
    ...
  }
}
  • I am getting this error "Inner types are not allowed to have inner types " – tarun Jun 28 '18 at 7:05
  • @tarun You need to move all inner stuff outside. I updated my answer to reflect this. – sfdcfox Jun 28 '18 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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