passing property to script include and getting back sys ids script help?

Kiddy1
Tera Contributor

Developer Community harshtimes ctomasi shloke04 explorenow

Aim:Need to remove hard coded sys_ids from client scripts

i have created a script include which take the properties and returns sysids

in the below script i have 3 sys ids i need to paas them by creating a property to script include and return back sys ids from script include,i need help on creating a script include

which takes multiple properties from client script and return sysids.

script include:

var Passing_Sys_Ids = Class.create();

Passing_Sys_Ids.prototype = Object.extendsObject(AbstractAjaxProcessor, {

Passing_Sys_Ids:function()

{

var answer;

var prop_name = this.getParameter('sysparm_name_in');

answer = gs.getProperty(prop_name);

return answer;

},

      type: 'Passing_Sys_Ids'

});

client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue == '') {

return;

}

    //Type appropriate comment here, and begin script below

var getSysIds = new GlideAjax('Passing_Sys_Ids');

getSysIds.addParam('sysparm_name','Passing_Sys_Ids');

getSysIds.addParam('sysparm_name_in',"domain1"); //domain1 is sys property which was created to store sys_id

getSysIds.getXML(sysid);

function sysid(response)

{

  var answer = response.responseXML.documentElement.getAttribute("answer");

  alert(answer);

}

if(g_user.hasRole('itil') && g_user.hasRole('DE') && !g_user.hasRole("admin")){

var   user = new GlideRecord('sys_user');

user.addQuery('sys_id',g_user.userID);

user.query();

while(user.next())

{

domain = user.sys_domain;

}

if(domain == b4d705920f061e00e316f77ce1050e80) // can i replace answer here?

{

g_form.setValue('assignment_group',b4d705920f061e00e316f77ce1050e93) ;//i need to remove hard coded sys ids from scripts

}

else

{

g_form.setValue('assignment_group',b4d705920f061e00e316f77ce1050e67);//i need to remove hard coded sys ids from scripts

}

}

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Kids,



Use getMessage in client script and store this sys_id's in that message.



This is how we replace gs.properties in client script.



Thank you,


Ashutosh



Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


View solution in original post

16 REPLIES 16

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Kids,



Use getMessage in client script and store this sys_id's in that message.



This is how we replace gs.properties in client script.



Thank you,


Ashutosh



Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


HarshTimes
Tera Guru

Hi Kid


As Asutosh mentioned you can create a message record for the SYSIDs and then you can call that message in client script .


See the below screenshot to   understand how to create message


find_real_file.png


Use the below function to read the message value


getMessage(KEY) ;


Chuck Tomasi
Tera Patron

Since the properties aren't changing, you don't need to look them up in the script include, you can pass them via a display business rule. Just don't forget to pass the display value also! Passing the sys_id and then using it in g_form.setValue() means you'll just have to do a second (implicit) trip to the server to get the display value for that group.



Since the properties aren't changing, you don't need to look them up in the script include, you can pass them via a display business rule. Just don't forget to pass the display value also! Passing the sys_id and then using it in g_form.setValue() means you'll just have to do a second (implicit) trip to the server to get the display value for that group.




Display business rule:



(function executeRule(current, previous /*null when async*/) {



g_scratchpad.domain1 = gs.getUser().getDomainID();



// Use a property to hold the group1 name


var name1 = gs.getProperty('my.assignment_group1'); //use the real property name


g_scratchpad.group1 = {


"sys_id" : getGroupID(name1),


"display_value" : name1


}



var name2 = gs.getProperty('my.assignment_group2');


g_scratchpad.group2 = {


"sys_id" : getGroupID(name2),


"display_value" : name2


}



function getGroupID(grpName) {


var grp = new GlideRecord('sys_user_group');


if (grp.get('name', grpName)) {


return grp.getValue('sys_id');


}



return;


}




})(current, previous);



Client script:



// Your GlideAjax code here (unchanged)



if (domain == g_scratchpad.domain1) {


        g_form.setValue('assignment_group', g_scratchpad.group1.sys_id, g_scratchpad.group1.display_value);



} else {


        g_form.setValue('assignment_group', g_scratchpad.group2.sys_id, g_scratchpad.group2.display_value);


}


if it is for catalog client script how can we specify the table ?