0

I am getting response with below format from external system and I struggling to parse this response because this format is build by external system not in salesforce.

Response:

NEW_ITEM-CUST_FIELD1[1]  =  ABC ,
NEW_ITEM-CUST_FIELD2[1]  =  1 ,
NEW_ITEM-CUST_FIELD3[1]  =  100923423725 ,
NEW_ITEM-CUST_FIELD4[1]  =  TOYOTA ,
NEW_ITEM-CUST_FIELD5[1]  =  CAMRY ,
NEW_ITEM-CUST_FIELD6[1]  =  TRSES ,
NEW_ITEM-EXT_PRODUCT_ID[1]  =   je691814 ,
NEW_ITEM-DESCRIPTION[1]  =  TestDescription ,
NEW_ITEM-EXT_PRODUCT_ID[2]  =   Ke691814 ,
NEW_ITEM-DESCRIPTION[2]  =  TestDescriptiontwo ,
NEW_ITEM-EXT_PRODUCT_ID[3]  =   Le691814 ,
NEW_ITEM-DESCRIPTION[3]  =  TestDescriptionThree 

I Can able to get the values of FIELD1....FIELD6 parameters using below code but how can get productIds and descriptions because those parameters are not unique.

Rest API Contexts

 RestRequest req=RestContext.request;
 RestResponse res=RestContext.response;
 Blob body = req.requestBody;
 Map<String,String> params = RestContext.request.params;

String AppId = params.get('NEW_ITEM-CUST_FIELD1[1]'); //working fine
String AppId1= params.get('NEW_ITEM-CUST_FIELD2[1]'); //working fine
String AppId2= params.get('NEW_ITEM-CUST_FIELD3[1]'); //working fine
String AppId3= params.get('NEW_ITEM-CUST_FIELD4[1]'); //working fine
String AppId4= params.get('NEW_ITEM-CUST_FIELD5[1]'); //working fine
String AppId5= params.get('NEW_ITEM-CUST_FIELD6[1]'); //working fine
String AppId6= params.get('NEW_ITEM-CUST_FIELD7[1]'); //working fine

i can also get the ProductId information easily when i get only 1 or 3 product details from external system but some times i am getting more than 10 products, at this time how can i parse the response without hardcoding the NEW_ITEM-EXT_PRODUCT_ID[1]....NEW_ITEM-EXT_PRODUCT_ID[3] product parameters on apex?

closed as unclear what you're asking by glls, Pranay Jaiswal, Derek F, Raul, Jayant Das Nov 12 '18 at 17:54

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You have not provided enough information or context here to meaningfully address your question. Would you please make some edits to show what your code is doing and explain clearly what the structure of your response is, and how you're struggling to interpret it? – David Reed Nov 8 '18 at 12:28
  • @David Reed : Thank you for your response. Just now i have updated my question. can you please check and give me any solution. – Test userADG23 Nov 8 '18 at 13:29
0

You can iterate and generate these keys dynamically. Here's a rough example you can refine and translate into your own code.

List<String> productIds = new List<String>();
List<String> productDescs = new List<String>();

Integer i = 1;

while (params.containsKey('NEW_ITEM-EXT_PRODUCT_ID[' + String.valueOf(i) + ']')) {
    productIds.add(params.get('NEW_ITEM-EXT_PRODUCT_ID[' + String.valueOf(i) + ']'));
    productDescs.add(params.get('NEW_ITEM-DESCRIPTION[' + String.valueOf(i) + ']'));
    i++;
}

Then you'll have two List<String> variables with all of the provided parameters in order.

Note that the code above is not defensive - you should add logic to guard against the situation where the count of Ids and Descriptions isn't the same or where a value is missing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.