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

chatsaurav19_0-1711111286073.png

 

Hi @chatsaurav19 

You can use your own variable name and updated the System property name in code logic (Email.To.group.mapping).

 

var group = gs.getProperty('Email.To.Group.Mapping'); //Your Own Sys Property
var 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);
        }
    }

 



Use 'dot' for System Property naming convention, for example, 'Email.To.group.mapping' like this.
For ref : 

image.png

 

Please mark my answer helpful and correct.

 

Regards,

Subhashis

Hi @Subhashis Ratna 

 

Surprisingly when I tried checking this condition, I got 'false' and 'undefined' respectively.

 

if (group_p.hasOwnProperty(emailKey) && group_p[emailKey].length > 2)

 

Length is : undefined

Property is: false 

 

Regards,

Saurabh

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

 

Thank you @Subhashis Ratna  for your patience and help! It worked! Thanks a lot!