1

The previous Question is linked here: Apex Class Error Message: System.JSONException: No content to map to Object due to end of input

I have updated the code If I use the below code where String is Dynamic, I am getting the above error but when I hard code the values, I get the response

In debug Logs the value of selrec is : [{"recId":"abc"},{"recId":"def"}] Code Generating the error:

public with sharing class getmypicklistvalues{        
 @AuraEnabled(cacheable=true)
public static List<String> fetchPickListValue(String searchKey , String selrec){
system.debug('The value is selected'+selrec);
List<String> pickListValuesList= new List<String>();
 If(selrec != NULL  || String.isNotBlank(selRec)){
  List<Object> selectedvalues = (List<Object>)JSON.deserializeUntyped(selrec);
for(Object slval: selectedvalues){    
    Map<String,Object> data = (Map<String,Object>)slval;
    system.debug('The value of Object is'+data.get('recId'));
    }
   }
  }
}

But when I hardcode the values to Deserialize the JSON response, I am getting the desired result.

public with sharing class getmypicklistvalues{        
 @AuraEnabled(cacheable=true)
public static List<String> fetchPickListValue(String searchKey , String selrec){
system.debug('The value is selected'+selrec);
List<String> pickListValuesList= new List<String>();
 If(selrec != NULL  || String.isNotBlank(selRec)){
  List<Object> selectedvalues = (List<Object>)JSON.deserializeUntyped('[{"recId":"Angio Detect"},{"recId":"Animana Data Conversion"}]');
for(Object slval: selectedvalues){    
    Map<String,Object> data = (Map<String,Object>)slval;
    system.debug('The value of Object is'+data.get('recId'));
    }
   }
  }
}

I am not sure what I am missing here. FYI selrec will be NULL initially and the values will be loaded once users makes a selection.

1

Replace line If(selrec != NULL || String.isNotBlank(selRec)){

with

If(String.isNotBlank(selRec)){

The problem is occurring when selrec is null so you need to handle that.

See below code fire error.

String selrec = '';
List<Object> selectedvalues = (List<Object>)JSON.deserializeUntyped(selrec);
for(Object slval: selectedvalues){    
    Map<String,Object> data = (Map<String,Object>)slval;
    system.debug('The value of Object is '+data.get('recId'));
}

But the code below does not.

String selrec = '[{"recId":"abc"},{"recId":"def"}]';
List<Object> selectedvalues = (List<Object>)JSON.deserializeUntyped(selrec);
for(Object slval: selectedvalues){    
    Map<String,Object> data = (Map<String,Object>)slval;
    system.debug('The value of Object is '+data.get('recId'));
}

So you need to add null check before parsing.

3
  • 2
    String.isNotBlank handles null and empty string; no need to test for null – cropredy Nov 4 '19 at 18:36
  • I have updated the code, thanks @cropredy – Rahul Gawale Nov 4 '19 at 18:42
  • Thank you so much Rahul and cropredy, changing : If(selrec != NULL || String.isNotBlank(selRec)){ TO If(selrec != NULL && String.isNotBlank(selRec)){ works like a charm . I think I should quit programming an go to Himalaya if I am going to ask these kind of stupid questions. Thanks Again – Padam Nov 5 '19 at 19:01

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.