-3

I am facing below error in test class:

Malformed JSON: Expected '[' at the beginning of List/Set

Error will throw in below line of the controller:

 warpList =( List<CaseAssociatedSerExc>)System.JSON.deserialize(CasesWrapperList,List<CaseAssociatedSerExc>.class);  

I created below class for serializing based on this other SFSE question and answer.

public class GSS_CaseHealthWrapperForTest {
    public case cases;
    public List<SelectOption> recTypeList;
    public Id RecordTypeId;
    public Map<String, ID> mapRecordType;
}
public class cases{
    public string Id;
    public string CaseNumber;
    public string OwnerId;
}

Test Class

public static testmethod void saveCasehealthCheck(){
    List<case> caseList=new List<case>();
    List<string>selectedCase=new List<string>();
    GSS_TestDataFactory.setupdata();
    for(integer i=0;i<=2;i++){
        Case C=new Case();
        C.Status='Pending';
        C.Type='Incident';
        C.Priority='4-Low';
        C.Description='qwertty'+i;
        C.Subject='xyz';
        caseList.add(c);
    }
    insert caseList;
    for(case c:caseList){
        selectedCase.add(c.id);
    }
   String SeOwnId=string.valueOf(userInfo.getUserId());
   GSS_CaseHealthWrapperForTest instance =new GSS_CaseHealthWrapperForTest();
     instance.cases=new case();
     instance.cases.id=selectedCase[0];
     instance.cases.OwnerId=userInfo.getUserId();
   // cs.cases.
    string myJson= JSON.serialize(instance);
    system.debug('myJson@@@@@@@@@@@@@@@@@@'+myJson);
   GSS_CaseHealthCheckController.CaseHelathCreation(myJSON,SeOwnId);
}

Controller

public static void CaseHelathCreation(string CasesWrapperList,string SEOwnId)
{ 
    system.debug('CasesWrapperList-->'+CasesWrapperList);
    Service_Excellence__c ServExc; 
    List<Service_Excellence__c> SElist = new List<Service_Excellence__c>();
    List<CaseAssociatedSerExc> warpList=new List<CaseAssociatedSerExc>();
   //below line causes issue       
   if(CasesWrapperList!=null){
        warpList =( List<CaseAssociatedSerExc>)System.JSON.deserialize(CasesWrapperList,List<CaseAssociatedSerExc>.class); 
    }
    for(CaseAssociatedSerExc cAssSerTra:warpList) {
        ServExc=new Service_Excellence__c();
        ServExc.Case__c=cAssSerTra.cases.id;
        ServExc.OwnerId=SEOwnId;
        ServExc.Service_Excellence_Champion__c =SEOwnId;
        if(cAssSerTra.RecordTypeId!=null){ //In case of null it selects default record type
            ServExc.RecordTypeId=cAssSerTra.RecordTypeId;
        }
        ServExc.Assigned_Date__c = System.Now();
        SElist.add(ServExc);
    }
    insert SElist;
    system.debug('SElist---->'+SElist);
}

I had taken following Json format from below line of the controller:

system.debug('CasesWrapperList-->'+CasesWrapperList);

[{
    "cases": {
        "Id": "5001k000009VP0AAAW",
        "CaseNumber": "18000001715",
        "GSS_Support_Level__c": "2",
        "OwnerId": "0051k000000HFRSAA4"
    },
    "mapRecordType": {
        "CS-AS Review": "0121k000000CiDDAA0",
        "CS-S Review": "0121k000000CiDEAA0",
        "LATF Review": "0121k000000CiDFAA0",
        "PSC Review": "0121k000000CiDGAA0"
    },
    "recTypeList": [{
            "disabled": false,
            "escapeItem": false,
            "label": "CS-AS Review",
            "value": "0121k000000CiDDAA0"
        },
        {
            "disabled": false,
            "escapeItem": false,
            "label": "CS-S Review",
            "value": "0121k000000CiDEAA0"
        },
        {
            "disabled": false,
            "escapeItem": false,
            "label": "LATF Review",
            "value": "0121k000000CiDFAA0"
        },
        {
            "disabled": false,
            "escapeItem": false,
            "label": "PSC Review",
            "value": "0121k000000CiDGAA0"
        }
    ],
    "RecordTypeId": "0121k000000CiDDAA0"
}]

closed as off-topic by glls, Pranay Jaiswal, Reshma, battery.cord, Jayant Das Feb 19 at 15:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – glls, Pranay Jaiswal, Reshma, battery.cord, Jayant Das
If this question can be reworded to fit the rules in the help center, please edit the question.

1

You're serializing a GSS_CaseHealthWrapperForTest and deserializing a List<CaseAssociatedSerExc>. Those types are not compatible with one another in at least one way - one of them is a List and the other one is not -, but possibly more than one way.

You will probably find it easier in your test class to construct an actual List<CaseAssociatedSerExc>, serialize it, and pass that JSON to your class. I don't see the need for a separate class definition to support the unit test.

  • I have searlized the actually list of case associated set exc finally I received same error.. – SFDC FAN Feb 14 at 13:22
  • 2
    That doesn't make sense. But fundamentally, this is a simple error. You are telling Salesforce that the type it will get back is a List, somewhere in your JSON, and it's not. So look at your types and see if you can determine where that is. – David Reed Feb 14 at 13:27

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