Client script to reference a true/false option on the selected variables form.

Stephen31
Kilo Expert

Hi All, so I'm trying to reference a field from the record being selected on the form.  I have it working on the back end up the g_form option doesn't work on the portal.  I'm trying to figure out how to do this without using that.  When it that piece out i get a null value for answer.  You can see the commented out piece that's causing the issue on service portal but without I'm getting a null.

Script include is this:

var SoftwareRequiresLicense = Class.create();
SoftwareRequiresLicense.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getLicenseInfo: function() {
var software = this.getParameter('sysparm_request_type');

var license = new GlideRecord ('u_software_catalog');
license.addQuery('sys_id',software);
license.query();
while(license.next()){
if(license.u_license_needed == true)
{
return true;
}
if(license.u_license_needed == false)
{
return false;
}
}
}
});

 

Client Script is this:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//var result = g_form.getReference('request_type').sys_id;

var ga = new GlideAjax('SoftwareRequiresLicense');
ga.addParam('sysparm_name', 'getLicenseInfo');
//ga.addParam('sysparm_request_type',result);
ga.getXML(myResponseParse);
}
function myResponseParse(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer == 'true') {

g_form.setDisplay('software_requires_license', true);

} else {

g_form.setDisplay('software_requires_license', false); }

}

1 ACCEPTED SOLUTION

I think you can simplify this by doing everything from the client side using getReference with a callback. I would try this in your client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    var reqType = g_form.getReference('request_type', getReqType);

}

function getReqType(reqType) {
    var licenseNeeded = reqType.u_license_needed;
    g_form.setDisplay('software_requires_license', licenseNeeded);
}

View solution in original post

12 REPLIES 12

Sounds like you got the idea.  I'll also be using a similar logic in the workflow to determine who the approval goes to.

I think you can simplify this by doing everything from the client side using getReference with a callback. I would try this in your client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    var reqType = g_form.getReference('request_type', getReqType);

}

function getReqType(reqType) {
    var licenseNeeded = reqType.u_license_needed;
    g_form.setDisplay('software_requires_license', licenseNeeded);
}

That is brilliant.  I actually haven't really made use of getReference much before so that seems like something I'll be making use of going forward.  

If I could impose further.  If I'm trying to base a workflow IF condition on that same true/false box being checked for the workflow selected, what would you use to do that?  My first attempt failed but I have some other ideas, however my deadline to fix this is also today so any help would be huge.  

I may not have the right syntax off the top of my head, but something like this should work:

answer = ifScript();

function ifScript() {
    var ifAnswer = '';
    var licenseNeeded = current.variables.u_license_needed;
    if (licenseNeeded) {
        ifAnswer = 'yes';
    } else {
        ifAnswer = 'no';
    }
    return ifAnswer;
}

That didn't work, it returned a no when it should of a yes.  I think the issue lies in the answer part but I could be wrong.  Will keep playing with the script.  I deeply appreciate the help.  If you'd like I can start a new question for this.