0

In certain callout I want to send JSON Array in following format. And based on this I want to parse this into doPost method and generate a dynamic query to query on Account object. But, I cannot access the List of Country into doPost method from wrapper class.

JSON Format I have to send:

{

"country":[ "IN", "PH", "KR" ], "Type":[ "Installation Partner", "Technology Partner"], "industry":[ "Energy", "Education" ]

}

The Wrapper class I have written:

public class accountforPostRequestWrapper {

    public List<String> country{get; set;}
    public List<String> Type{get; set;}
    public List<String> industry{get; set;}

    public static List<accountforPostRequestWrapper> parse(String json){
         return (List<accountforPostRequestWrapper>) System.JSON.deserialize(json, List<accountforPostRequestWrapper>.class);
    }

}

Am I missing something. In RestResource Class I have write something like below.

but its throwing error:

"Variable does not exist: country"

global with sharing class AccountResource {
@HttpPost
global static void doPost() {
    RestRequest req = RestContext.request;
    Blob body = req.requestBody;
    String jsonString = body.toString();

    List<String> listofCountry = new List<String>();
    listofCountry.clear();

    List<accountforPostRequestWrapper> accWr = accountforPostRequestWrapper.parse(jsonString);
    listofCountry.addAll(accWr.country);

    // In Above line this giving ERROR: "Variable does not exist: country"

    //Generate dynamic SOQL based on JSON Input       
    String searchStr = 'SELECT Id,Name,Industry,Type,Country_code__c FROM Account WHERE';
    String conditionStr = 'Country_code__c =: null';
    if(!accWr.isEmpty()){
            //to generate dynamic conditionStr here based on 
            //   country,Type, and industry value
        }
    }
}

Please help me how to parse this kind of JSON and how to fetch value from wrapper class and to generate the dynamic query considering null as well. Scenario is possible also that JSON contains no country, but has Type and industry. Please help.

0

1 Answer 1

2

Problem is accWr is declared as a List.

parse method must return an object:

public static AccountforPostRequestWrapper parse(String json){
     return (AccountforPostRequestWrapper) System.JSON.deserialize(json, AccountforPostRequestWrapper.class);
}

And your code would be as:

List<String> listofCountry = new List<String>();
listofCountry.clear();

AccountforPostRequestWrapper accWr = AccountforPostRequestWrapper.parse(jsonString);
listofCountry.addAll(accWr.country);
5
  • That was typo here. I already have given the '=' operator. Sorry for typo. Commented Jun 12, 2019 at 12:42
  • no worries, updated the answer, you have declared the wrapper variable as list instead of an object. Commented Jun 12, 2019 at 12:46
  • Am I doing wrong over wrapper variable. Could you please show me a code snippet how to declare this in my case. Commented Jun 12, 2019 at 12:52
  • Answer was updated, did you see the changes :) Commented Jun 12, 2019 at 12:59
  • That worked for me :) I will let you know if I get trouble in generating the query. Commented Jun 12, 2019 at 13:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.