Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Return values from array of objects and populate the fields

chatsaurav19
Tera Contributor

Hi All,

 

I have a system property where the data has been define like this ( array of objects format ):::

 

{

"email1@abc.com":["8452de8f8394865050ed47747daad391","Value 1.0"], //group1,version1
"email2@abc.com":["8965de9f8394811050edk7747daad391","Value 2.0"], //group2,version2
}

 

Using Inbound Action, I am trying to populate couple of fields by referring to this system property above, like this:::

 var group = gs.getProperty('Email To Group Mapping');
  var grp = JSON.parse(group);
  var email_to = email.to;
  email_to = email_to.toLowerCase();
  email_to = email_to.split(',');
  for (i = 0; i < email_to.length; i++) {
        if (email_to[i].includes('email')) {
            current.assignment_group = grp[email_to[i][0]];
            current.u_version = grp[email_to[i][1]];
            gs.info("Assignment Group: " + current.assignment_group);
            gs.info("Version: " + current.u_version);
   }
 }


But unfortunately both the Assignment Group and Version is returning false! Can someone please help me to debug this piece or logic?

 

Thanks and Regards,

Saurabh Chatterjee

1 ACCEPTED SOLUTION

Hi @chatsaurav19 
No Worries , here i have updated the code logic as per your system property obj.

Code :

var group = gs.getProperty('Email.To.Group.Mapping'); //Your Own Sys Property
var group_p = JSON.parse(group);
gs.info("Saurabh Group: " + JSON.stringify(group_p));

var email_to = email.to;
email_to = email_to.toLowerCase().split(',');
gs.info("Saurabh Email: " + email_to);
for (var i = 0; i < email_to.length; i++) {
    var emailKey = email_to[i].trim();

    gs.info("Saurabh Email Key: " + emailKey);
    gs.info("Saurabh Email Key PROP: " + group_p.hasOwnProperty(emailKey));
    gs.info("Saurabh Email Key KEY LENGTH: " + group_p[emailKey].length);

    if (group_p.hasOwnProperty(emailKey) && group_p[emailKey].length == 2) {
        current.assignment_group = group_p[emailKey][0];
        current.u_cockpit_version = group_p[emailKey][1];

        gs.info("Saurabh Assignment Group: " + current_assignment_group);
        gs.info("Saurabh Version: " + current_u_cockpit_version);
    }
}

 

Note : 

The "emailKey" is "abc.ab11.def@qwe.com", the key corresponding to the email address. The length of the array associated with this "emailKey" is 2 because there are two elements in the array: "8452de8f8394865050ed47747daad391" and "Value 1.0". Therefore, group_p[emailKey].length would evaluate to 2.

 

 BG Script Code :

image.png

 

System Property SS :


image.png

Please mark my answer helpful and correct.

 

Regards,

Subhashis

 

View solution in original post

10 REPLIES 10

1__AnithaHV
Kilo Sage

Hi Saurabh,

 

Could you please let me know what is value that is present in the variable email_to?

Hi @1__AnithaHV 

 

It is : email1@abc.com

Subhashis Ratna
Tera Guru

Hi @chatsaurav19 
Please use this tested code to fulfill your requirement, I have retested it in my own PDI ..

var group = gs.getProperty('test.pro'); //Your Own Sys Property
group_p = JSON.parse(group);
gs.print(group_p);
var email_to = 'email1@abc.com'; //let's assume email id we got
email_to = email_to.toLowerCase().split(',');
for (var i = 0; i < email_to.length; i++) {
    var emailKey = email_to[i].trim();
    if (group_p.hasOwnProperty(emailKey) && group_p[emailKey].length >= 2) {
		gs.print('in if');
        assignment_group = group_p[emailKey][0];
        u_version = group_p[emailKey][1];
        gs.print("Assignment Group: " + assignment_group);
        gs.print("Version: " + u_version);
        break;
    }
}


O/p:

forComm.png

Please mark my answer helpful and correct.

 

Regards,

Subhashis


 

Hi @Subhashis Ratna 

 

Thank you for your valuable input. Actually I am still unable to get the values after applying your logic.

 

var group = gs.getProperty('Email To Group Mapping'); //Your Own Sys Property
    group_p = JSON.parse(group);
    gs.info("Saurabh Group: " + group_p);
    var email_to = email.to;
    email_to = email_to.toLowerCase().split(',');
    gs.info("Saurabh Email: " + email_to);
    for (var i = 0; i < email_to.length; i++) {
        var emailKey = email_to[i].trim();
        gs.info("Saurabh Email Key: " + emailKey);
        if (group_p.hasOwnProperty(emailKey) && group_p[emailKey].length > 2) {
            current.assignment_group = group_p[emailKey][0];
            current.u_cockpit_version = group_p[emailKey][1];
            gs.info("Saurabh Assignment Group: " + current.assignment_group);
            gs.info("Saurabh Version: " + current.u_cockpit_version);
        }
    }

 For me the  the 'email_to' is like : abc.ab11.def@qwe.com . Also shouldn't the 'group_p' be a variable?

 

Regards,

Saurabh