0

I am reluctant to post this, knowing the question has been raised several times. But after reading everything and trying a few different approaches I am unable to work out why I can not get this unit test to work.

I need to test a method that takes a String of Ids and passes them to JSON. Deserialize. But if I pass a String I get an error of Unexpected character ('a' (code 97)). And my attempts to pass a JSON Object result in an error of Illegal Value for Primitive in JSON.

Below is the method I am trying to test.

 @AuraEnabled
    public static void deleteEventMembers(String idsToDelete){
        Type listIdType = Type.forName('List<Id>');
        System.debug('value of Ids to delete in method' +  idsToDelete);
        List<Id> listIdsForDelete = (List<Id>) JSON.deserialize(idsToDelete, listIdType);

        List<BA_EIMA_Event_Members__c> listEventMembers = [SELECT ID, Action_By__c, Action_On__c, Action_Operation__c FROM BA_EIMA_Event_Members__c WHERE ID IN:listIdsForDelete];
        List<BA_EIMA_Event_Members__c> listEventMembersForUpdate = new List<BA_EIMA_Event_Members__c>();

        if(listEventMembers.size() > 0){                        
            for(BA_EIMA_Event_Members__c ev : listEventMembers) {
                ev.Action_by__c = UserInfo.getUserId();
                ev.Action_On__c = System.now();
                ev.Action_Operation__c = 'Deleted';

                listEventMembersForUpdate.add(ev);
            }

            update listEventMembersForUpdate;
        }
    }

Below is the test method. I have left all three approaches in the code and noted them with comments.

@IsTest
    static void deleteEventMembersTest() {
        Integer testSize = 10;
        List<Account> accounts =
                BA_EIMA_TestDataFactory.creatPersonAccounts(testSize);
        insert accounts;
        BA_EIMA_Events__c event = BA_EIMA_TestDataFactory.createEIMAEvent();
        insert event;
        List<BA_EIMA_Events__c> updatedEvent = [SELECT Id FROM BA_EIMA_Events__c LIMIT 1];
        List<Contact> contacts = [SELECT Id FROM Contact];
        List<BA_EIMA_Event_Members__c> members = BA_EIMA_TestDataFactory.createEIMAEventMembers(updatedEvent[0].Id, contacts);
        insert members;
        BA_EIMA_Event_Members__c updatedMember = [SELECT Id FROM BA_EIMA_Event_Members__c LIMIT 1];

        //**** Attempt 1 This variable throws a System.JSON Exception Illegal value for a primitive error.
        //String idsToDelete = '[{"idsToDelete": "' + updatedMember.Id + '"}]';


        //*** Attempt 2 this  throws an Unexpected Character error ('a', code 97)
        String idsToDelete = (String) updatedMember.Id;


        //*** Attempt 3 This also throws a Illegal value for a primitive error
        //String lstSelectedIds = (String)System.JSON.deserialize(idsToDelete, List<Id>.class);

        Test.startTest();
        BA_EIMA_utilityMethods.deleteEventMembers(idsToDelete);
        Test.stopTest();

    }
3
  • 2
    Why not do JSON.serialize(aListOfIDs) to generate the input to the method to be tested? NB: the JSON for a list (array) of IDs will be of the form '["id", "id", "id"]'
    – Phil W
    Commented Feb 2, 2020 at 23:36
  • Phil that worked. Can you tell me why? I don't have lot of experience working with JSON serialize/deserialize etc.. Commented Feb 2, 2020 at 23:43
  • 1
    A list in JSON is [...], while an object is {...}. An Id or String is "...". Since you have a list of Id, you have simply ["...","...",...] (as noted by Phil).
    – sfdcfox
    Commented Feb 3, 2020 at 0:48

1 Answer 1

1

The JSON syntax for a list or array of strings (IDs are strings) is:

["string", "string", "string"]

etc.

The syntax "{...}" represents an object, which is different to an array.

To simplify your case, I suggest your test set up an actual list and serialize to JSON, since this will automatically use the correct syntax. E.g.:

Map<Id, SObject> objectsById = new Map<Id, SObject>([SELECT Id FROM BA_EIMA_Event_Members__c LIMIT 1]);
List<Id> someIds = new List<Id>(objectsById.keySet());
String someIdsAsJSON = JSON.serialize(someIds);

You can then use someIdsAsJSON as the parameter to your call. Note that I have used the conventional approach to using a query to get some objects and then turn this into a list of their IDs.

1

You must log in to answer this question.

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