Client Script to set the value

carlh
Kilo Guru

Hi All,

Will anyone help me come up with a client script that will copy the value of a subcategory to the Configuration Item field?   I can't write javascript and am in a hurry

I believe it it should be "on change" type.

Incident table

psuedo script

If "subcategory" changes to "anything", set   "Configuration Item" to same as "subcategory"

Will copying the display name into that reference field work if they are the same?

Currently, I have a reporting issue with the way I've enhanced the form and think this may be the quickest fix Until we figure out how to fix the data issue we've created

if I had this script I could remove several ui policies and actions which are becoming confusing and sort of have me cornered at the moment.  

Please let let me know if you can help.

Thanks in advance for your time and assistance!

regards,   Carl

1 ACCEPTED SOLUTION

first create script include mark that check box client callable



find_real_file.png


var test = Class.create();


test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


ajaxFunction_LocationAjax : function() {


var fo='';


var customerID = this.getParameter('sysparm_custid'); //paramter from the client side


var ag = new GlideRecord('cmdb_ci');


ag.addQuery('name', customerID);


ag.query();


while (ag.next()) {


fo= ag.sys_id;



gs.log('value is :'+fo);


}


return fo;


},




type: 'test'


});






find_real_file.png




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


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


return;


}



//Type appropriate comment here, and begin script below


var customerNo = g_form.getValue('subcategory');


var gajax = new GlideAjax('test'); // call the Script Include class


// call the public method


gajax.addParam('sysparm_name', 'ajaxFunction_LocationAjax');


// pass the sysID to the method


gajax.addParam('sysparm_custid', customerNo);


//callback function is used to return the result, passed to the getXML function


gajax.getXML(callBackLocation);



function callBackLocation(response) {


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


alert(answer);


g_form.setValue('cmdb_ci', answer);


}


}


View solution in original post

41 REPLIES 41

yes, I agree with Harsh, Glide is not recommended as mentioned, Try this out



Client Script:


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


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


return;


}


var gajax = new GlideAjax('LocationAjax');


gajax.addParam('sysparm_name', 'ajaxFunction_LocationAjax');


gajax.addParam('sysparm_sub', newValue);


gajax.getXML(callBackLocation);


function callBackLocation(response) {


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


alert(answer);


g_form.setValue('cmdb_ci', answer);


}


}



find_real_file.png



Client Callable Script Include:


var LocationAjax = Class.create(); // create Class


LocationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


ajaxFunction_LocationAjax : function() {


var ag = new GlideRecord('cmdb_ci');


ag.addQuery('name', this.getParameter('sysparm_sub'));


ag.query();


if(ag.next())


return ag.sys_id;


},


type : 'LocationAjax'


});



find_real_file.png


first create script include mark that check box client callable



find_real_file.png


var test = Class.create();


test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


ajaxFunction_LocationAjax : function() {


var fo='';


var customerID = this.getParameter('sysparm_custid'); //paramter from the client side


var ag = new GlideRecord('cmdb_ci');


ag.addQuery('name', customerID);


ag.query();


while (ag.next()) {


fo= ag.sys_id;



gs.log('value is :'+fo);


}


return fo;


},




type: 'test'


});






find_real_file.png




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


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


return;


}



//Type appropriate comment here, and begin script below


var customerNo = g_form.getValue('subcategory');


var gajax = new GlideAjax('test'); // call the Script Include class


// call the public method


gajax.addParam('sysparm_name', 'ajaxFunction_LocationAjax');


// pass the sysID to the method


gajax.addParam('sysparm_custid', customerNo);


//callback function is used to return the result, passed to the getXML function


gajax.getXML(callBackLocation);



function callBackLocation(response) {


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


alert(answer);


g_form.setValue('cmdb_ci', answer);


}


}


Thank you Harsh!   I appreciate your helping us use the best practice method.   It works perfectly!



Thank you Shirshir explorenow for your efforts as well as you quickness.   You gave me the script and instructions first but Harsh gave the correct answer which was a different solution than you initially gave.




To all on this post   THANK YOU!   I have learned a lot from all of you.


Srvs
Kilo Guru

Hi ,



you can try this once...



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


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


          return;


    }


    //Type appropriate comment here, and begin script below


g_form.clearOptions('u_operational_tier_2');


var operational_Template_1 = g_form.getValue('u_operational_tier_1');


if (operational_Template_1 == "Add_Create")


{


g_form.addOption('u_operational_tier_2','Chargeback','Chargeback');


g_form.addOption('u_operational_tier_2','Data','Data');



}


else if (Condition)


{


Values to be set


}


Thanks for replying but I have no idea what you're showing here since I can't write it. The chargeback and add_create and data, data are confusing.



We're just trying to lookup the name and return the sys_id when the selected subcategory value matches a name on the cmdb_ci table



Get Outlook for iOS<https://aka.ms/o0ukef>