Skip to main content
Andy Corbett ha preguntado en #Json

Hi - this is part of a JSON file where the productResults can be empty or it can contain one or more entries:

 

"quotation": {		"quotationId": 168862733,		"retrievalToken": "bTec+xrBlRBqq1liLqaq/Q==",		"quotationOutcome": "Scored",		"productResults": []	},

The data is deserialized, here is part of the data structure:

 

public class ProductResults {    public String productId;    public Integer likelihoodValue;    public String defactoId;    public ProductDetails productDetails;  }  public class Quotation {    public Integer quotationID;    public String retrievalToken;    public String quotationOutcome;    public String declineReason;    public List<ProductResults> productResults;  }

After it has been deserialized, in Apex I want to check to see if the productResults has any data, so I have tried the following, but even when it is empty as above this is still returning True:

 

 if(quotation != NULL && quotation.productResults != NULL){

Is the syntax correct?

 

#Apex  #Json

4 respuestas
  1. 19 jul 2021, 13:28

    I've not tried this through... but I would think since your productResults is being set to an empty list here in your JSON that you'd beed to use isEmpty() or size() instead of checking for null

    ... "productResults":[] ....

    So maybe more like this?  

     if(quotation != NULL && !quotation.productResults.isEmpty()){

    Also, you could use the null safe operator to cut your code down... I think this would work.

     if(!quotation?.productResults?.isEmpty()){
0/9000