Need to call system property in UI policy client script

Aswin Chandras1
Tera Contributor

I have UI policy script doing some function.

In that I am checking for particular order guide using sys_id.

 

I need to replace that sys id with system property. 

I have written script include for the same but it is not working.

 

SI:

var GetOrderguide = Class.create();
GetOrderguide.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getProperty: function() {
        return gs.getProperty('name of property');
    },

    type: 'GetOrderguide'
});
 
UI policy script:
var ga = new GlideAjax('x_mosti_ms_hardwar.GetOrderguide');
ga.addParam('sysparm_name', "getProperty");
ga.getXML(myCallBack);
function myCallBack(response) {
var prop = response.responseXML.documentElement.getAttribute('answer');

}
 
With the prop I need to call in the below condition
 
if(finalParam == 'sys_id')// calling order guide sysy_id here
{
    g_form.setMandatory('variable_name',false);
g_form.setMandatory('variable_name',false);
g_form.setVisible('variable_name',false);
g_form.setVisible('variable_name',false);
 
}
 
 
9 REPLIES 9

Hi @Ankur Bawiskar 

 

Please find the full code.

 

function onCondition() {
var url = decodeURIComponent(top.location.href); //Get the URL and decode it
var workspaceParams = url.split('sysparm_guide')[1]; //Split off the url on Extra params
var serviceportalParams=url.split('sys_id');
var wkspaceParams=url.split('sc_cat_item_guide/');
//alert(workspaceParams);
var params = workspaceParams;
var allParams='';
var ans='';
//The params are split on slashes '/'
if(workspaceParams!=null)
{
allParams = params.split('&');
//alert(allParams.length);
//Search for the parameter requested
for (var i = 0; i < allParams.length; i++) {
if (allParams[i] == sysparm_guide) {
    ans=allParams[0];
    return allParams[0];

 

}

 

}
}
if(serviceportalParams!=null)
{
allParams=serviceportalParams;
}
//alert(ans);
var finalParam='';

 

if(ans!=null)
{
finalParam=ans.replace('=','');
}
if(wkspaceParams!=null)
{
finalParam=wkspaceParams[1];

 

}

 

var ga = new GlideAjax('GetOrderguide');
ga.addParam('sysparm_name', "GetOrderguideSYSID");
ga.getXMLAnswer(function(answer){
if(answer != ''){
    alert(answer);
}
});

 

//alert(finalParam);
if(finalParam == answer)
//if(finalParam == 'sys_id')
{
g_form.setMandatory('variable_name',false);
g_form.setMandatory('variable_name',false);
g_form.setVisible('variable_name',false);
g_form.setVisible('variable_name',false);
g_form.setMandatory('ms_ham_financial_approver',false);
g_form.setMandatory('ms_unlock_approvers',false);
g_form.setVisible('ms_ham_financial_approver',false);
g_form.setVisible('ms_unlock_approvers',false);
}
}

@Aswin Chandras1 

I won't suggest to use UI policy for this.

use onLoad client script since your script is too heavy

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Aswin Chandras1 

try this

function onCondition() {
    var url = decodeURIComponent(top.location.href); //Get the URL and decode it
    var workspaceParams = url.split('sysparm_guide')[1]; //Split off the url on Extra params
    var serviceportalParams = url.split('sys_id');
    var wkspaceParams = url.split('sc_cat_item_guide/');
    //alert(workspaceParams);
    var params = workspaceParams;
    var allParams = '';
    var ans = '';
    //The params are split on slashes '/'
    if (workspaceParams != null) {
        allParams = params.split('&');
        //alert(allParams.length);
        //Search for the parameter requested
        for (var i = 0; i < allParams.length; i++) {
            if (allParams[i] == sysparm_guide) {
                ans = allParams[0];
                return allParams[0];
            }
        }
    }
    if (serviceportalParams != null) {
        allParams = serviceportalParams;
    }
    //alert(ans);
    var finalParam = '';

    if (ans != null) {
        finalParam = ans.replace('=', '');
    }
    if (wkspaceParams != null) {
        finalParam = wkspaceParams[1];
    }

    var ga = new GlideAjax('GetOrderguide');
    ga.addParam('sysparm_name', "GetOrderguideSYSID");
    ga.getXMLAnswer(function(answer) {
        if (finalParam == answer)
        //if(finalParam == 'sys_id')
        {
            g_form.setMandatory('variable_name', false);
            g_form.setMandatory('variable_name', false);
            g_form.setVisible('variable_name', false);
            g_form.setVisible('variable_name', false);
            g_form.setMandatory('ms_ham_financial_approver', false);
            g_form.setMandatory('ms_unlock_approvers', false);
            g_form.setVisible('ms_ham_financial_approver', false);
            g_form.setVisible('ms_unlock_approvers', false);
        }
    });
    //alert(finalParam);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Aswin Chandras1 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

-O-
Kilo Patron
Kilo Patron

I would approach this a bit different.

I would create a reference variable with "Visible on Summaries" not-checked so it is not included in notifications and stuff, UI Policy to always hide it and with the default value so that a property is not even needed. If it is to be used in more than one place, you might make it a variable set and include as needed.

Now, because it is just a variable as any other, you can use it in UI Policy conditions and you don't need to write a single line of script.

Lite on the system, easy to maintain.