IT Process Automation

Expand all | Collapse all

Looping through an Array, getting the value of field.

Jump to Best Answer
  • 1.  Looping through an Array, getting the value of field.

    Posted 01-19-2017 05:01 AM

    Hello, 

     

    We are passing data from a SC form into PAM by using Event-rule-action to trigger a SRF containing the $form_sd_data_row$ parameter . 

     

    In PAM we are recieving the passed data from the parameter as an object containing an array, as follows:

     

    {"10197" : [{"name" : "txtf_1",
    "value" : "SomeActiveDirectoryGroupName",
    "type" : "5"},{"name" : "txt_2",
    "value" : "TheUserId",
    "type" : "5"}]}

     

    How can we assign "SomeActiveDirectoryGroupName" and "TheUserId" to a process.variable when the name of the object/the number, 10197, is changing every time the user submits this request. 

     

    We have tried several methods that  work in regular Javascript and common libraries.

     

    1. Looping through the Array:

     

    // since the nameof first property will vary, always get it by looping the json
    var firstItem;
    for (var prop in json) {
    if (json.hasOwnProperty(prop)) {
    firstItem = json[prop];
    break;
    }
    }

    var groupFromForm = firstItem[1].value;

     

    It seems that hasOwnProperty is not accepted. 

    I'm not sure that looping through the Array works at all. 

     

    2. Using object.key

     

    var myText = {"10132" : [{"name" : "txtf_1",

    "value" : "SomeActiveDirectoryGroupName",

    "type" : "5"},{"name" : "txt_2",

    "value" : "TheUserId",

    "type" : "5"}]}

     

    var myKey = (Object.keys(myText));

    Object.keys is not accepted. 

     

    We have been able to make this work by using a regex expression that assigns the Array to a variable, and then refering to the variable[index].value. However, this is not very robust, as the form needs to remain unchanged for this to work, becuase the the content of the array must always be sent in the same exact order. 

     

    Any help is appreciated!! 

     

    Thank you! 

    --

    Sondre 

     

     

     

     



  • 2.  Re: Looping through an Array, getting the value of field.

    Posted 01-19-2017 05:17 AM

    In a similar situation, we used the convertJson function. Assuming that your string is in the Process.JsonString, this would work like this:

    if ( Process.JsonString.length < 1 )   
    return ;

    try {  Process.vmap1 = convertJson(Process.JsonString);} catch (_error) { 
    // If we have any parsing error, bail out.
    logEvent(2, "OPERATOR", "Failed to call ConvertJson on the input data: " + _error.message); 
    setOperatorStatus("Failure", 2, "Failed to call ConvertJson on the input data: " + _error.message); 
    return;
    }

    var keys = getValueMapFields(Process.vmap1);

    // Loop through each Form that's on the RequestItem
    for ( i=0; i< keys.length; i++ ) {
    logEvent(1, "CUSTOM", " * Form :  " + i + " = " + keys[i]);  
    _form =  vmap1[keys[i]];  logEvent(1, "CUSTOM", " ** has fields  " + _form.length);
    }

     

    One gotcha though - the convertJson fails if the root element is a numeric value - as it is the value produced by Service Catalog. So I used a small trick before running the above code, by running a JS operator before the code above:

     var _replacement = Process.JsonString;
    Process.JsonString = _replacement.replace(/"([0-9]+)"\s:/g, '"form_$1" :');


  • 3.  Re: Looping through an Array, getting the value of field.
    Best Answer

    Posted 01-19-2017 08:48 AM

    Hi, 

     

    I was working on this same idea earlier this week. I found it much simpler to use the getFormRateItemValues operator. This operator pulls more data than the form_data_sd_row parameter passes to the PAM json array, which is helpful because we don't need as many hidden fields on our catalog form to capture label/ID. 

     

    Assign Process Variables from FormVM array 



  • 4.  Re: Looping through an Array, getting the value of field.

    Posted 01-19-2017 09:51 AM

    Thank you both!! 

    Your answers provide two ways of solving our problems!