0

My Class

global class CreditActionSessionObjMgmt
{
    @future(callout=true)
    static webservice void deleteSessionRecord(String sessionRecIds)
    {
        if(sessionRecIds != null )
        {
            List<nForce__Session__c> objList = (List<nForce__Session__c>)system.JSON.deserialize(sessionRecIds,List<nForce__Session__c>.Class);

            if(objList != null && objList.size()>0)
            {
                System.debug('Delete records:'+objList);

                delete objList;    
            }
        }
    }
}

Test class i have written is

@istest
public class CreditActionSessionObjMgmt_test{

    static testmethod void deleteSession(){
     string str = ' { [ {"attributes" : {"type" : "nForce__Session__c","url" : "/services/data/v45.0/sobjects/nForce__Session__c/0039000001QYYa4AAH" },"Id" : "0039000001QYYa4AAH", "Name" : "Burlington Textiles Corp of America"}} ]}';
            test.starttest();
            CreditActionSessionObjMgmt.deleteSessionRecord(str);
        test.stoptest();
    }

}

Error Im getting

System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set Stack Trace Class.System.JSON.deserialize: line 15, column 1 Class.CreditActionSessionObjMgmt.deleteSessionRecord: line 8, column 1

2 Answers 2

3

As error says List starts with [ and ends with ]. Also I notice extra } inside the array. Try below in test class:

string str = '[{"attributes":{"type":"nForce__Session__c","url":"/services/data/v45.0/sobjects/nForce__Session__c/0039000001QYYa4AAH"},"Id":"0039000001QYYa4AAH","Name":"Burlington Textiles Corp of America"}]';

NOTE:

For understanding better, you can run below in anonymous apex and check:

System.debug(JSON.serialize([SELECT Id, Name FROM nForce__Session__c WHERE Id='0039000001QYYa4AAH']));
3
  • 1
    +1 nice answer! Commented Aug 16, 2019 at 10:46
  • Now im getting error as 'System.UnexpectedException: Salesforce System Error'.Here nForce__Session__c object is coming from managed package
    – Eagerin Sf
    Commented Aug 16, 2019 at 10:47
  • Does the test user has delete permission? Commented Aug 16, 2019 at 10:49
1

Not sure why you are using hard coded JSON to feed the records to your method to test the delete functionality.

Delete method requires the Id of the record which needs to be deleted and in your method, you are passing that Id in JSON as the hard coded value. If that record does not exist in the database, it will give you runtime exception. Your test method won't be able to find that record because your seeAllData attribute is by default set to false unless explicitly mentioned and you have not inserted any record of type nForce__Session__c in your test class.

Ideally I would write a test method like below by inserting the records and then testing it by passing a JSON and verifying if those records are getting deleted or not.

@istest
public class CreditActionSessionObjMgmt_test{
    static testmethod void deleteSession(){
        List<nForce__Session__c> objList = new List<nForce__Session__c>();
        nForce__Session__c obj1 = new nForce__Session__c();
        obj1.Name = 'Burlington Textiles Corp of America';
        objList.add(obj1);

        nForce__Session__c obj2 = new nForce__Session__c();
        obj2.Name = 'Burlington Textiles Corp of America';
        objList.add(obj2);

        insert objList;

        string str = JSON.serialize([SELECT Id, Name FROM nForce__Session__c]);

        Test.StartTest();
            CreditActionSessionObjMgmt.deleteSessionRecord(str);
        Test.StopTest();

        system.assertEquals(0,[SELECT count() FROM nForce__Session__c])
    }
}

And regarding the error you are getting, as error message suggests, the JSON you have used in invalid i.e. if you are trying to deserialize the JSON to List<nForce__Session__c> then you will have to have the JSON in array format. The square brackets [ ] are used to declare JSON array.

You can read about JSON arrays at this and this links.

Before you pass a string of JSON, it is always better to validate the JSON at any online JSON validator like JSONLINT.

So your JSON after validation should be

string str = '[{"attributes":{"type":"nForce__Session__c","url":"/services/data/v45.0/sobjects/nForce__Session__c/0039000001QYYa4AAH"},"Id":"0039000001QYYa4AAH","Name":"Burlington Textiles Corp of America"}]';

This above JSON would give you List<nForce__Session__c> with one instance of nForce__Session__c.

1
  • Now im getting error as 'System.UnexpectedException: Salesforce System Error'.Here nForce__Session__c object is coming from managed package
    – Eagerin Sf
    Commented Aug 16, 2019 at 10:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .