snippet code to set a model condition based on a JSON string

edited October 2019 in Questions
I have situation where a field in Salesforce stores the list of id's in JSON format. I need a code to set the condition on a model with the values IN (the list of id's in JSON string). Thanks in advance.

Comments

  • edited November 2017
    Hey Shiva,

    I haven't tested this code. This is just conceptual. You'll have to adapt it to an actual snippet.

    The general idea is that you can do this by using javascript's JSON.parse() function to parse the ids into a javascript list, then populate a multiple value condition with the ids in that list.

    So something like this.
    modelToFilter = skuid.model.getModel('modelToFilter');
    multipleCond = modelToFilter.getCondition('multipleIdCondition');
    ids = []
    //assuming the field is in the first model. Adjust this to get the correct row
    row = skuid.model.getModel('ModelWithStoredIds').getFirstRow();
    //get unparsed ids
    unparsed = row.fieldWithJson;
    //here's where you'll need to do a little messing around. Depending on your JSON schema/structure,
    // there may be a different path to your ids. 
    //for example if your list is named 'ids' in json, you would accedd the ids through be parsed.ids
    //I'm going to assume it's directly in your JSON as the root item
    parsed = JSON.parse(unparsed);

    modelToFilter.setCondition(multipleCond,parsed)
    return modelToFilter.updateData();
  • edited October 2019
    Did you find a solution for this? I think it is setting the condition as '=' rather than 'IN' and throwing an exception for me.
Sign In or Register to comment.