Making variables Read Only -Source Record Producer not Request Item

a_necho
Giga Contributor

I am working in the Facilities Management Catalog and am creating record producers the create facilities requests. I want to make the variables read only after record submission.   I have it working for the Service Catalog request items, but I cannot get it to work for the variables on the Facilities Request table.

I created a business rule and a client script to try and accomplish this. I tried to leverage a similar solution from the ServiceGuru site with a few modifications

Business Rule

When to run: Display

Script:

-------------------------------------------------------

//Initialize the scratchpad variable

g_scratchpad.ReadOnlyVars = '';

//Check to see if a variable pool exists

var count = 0;

for(vars in current.variable_pool){

    count++;

    break;

}

//If a variable pool exists then collect empty variable names

if(count > 0){

          var ReadOnlyVars = [];

          var producerVars = new GlideRecord('question_answer');

          producerVars.addQuery('table_sys_id', current.sys_id);

          //Exclude Label and Container variables

          producerVars.addQuery('question.type', '!=', 11);

          producerVars.addQuery('question.type', '!=', 19);

          producerVars.addQuery('question.type', '!=', 20);

          producerVars.query();

          while(producerVars.next()){

                //Add variable names to the ReadOnlyVars array

                ReadOnlyVars.push(producerVars.question.name.toString());

          }

    }

    //Store the result in the scratchpad

    g_scratchpad.ReadOnlyVars = ReadOnlyVars.join();

-------------------------------------------------------

Client Script:

Type: Onload

Script:

-------------------------------------------------------

function onLoad() {

    //Lock down variables using the scratchpad object passed from 'Hide Empty Variables' business rule

    if(g_scratchpad.ReadOnlyVars != ''){

          var ReadOnlyVars = g_scratchpad.ReadOnlyVars.split(',');

          for(i = 0; i < ReadOnlyVars.length; i++){

                g_form.setReadOnly('variables.' + ReadOnlyVars[i], true);

          }

    }

}

-------------------------------------------------------

Any help would be appreciated.

Thanks,

Allister

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Can you try just this in your client script :




$(variable_map).select("item").each(function (elmt){


try {


g_form.setDisabled('variables.' + elmt.getAttribute('qname'),true);


} catch (err) {


}


});


View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I think the variable editor may display a little differently on records created from RPs, so you might need to modify the client script a bit.



However, in Jakarta you can now apply catalog client scripts and ui policies to variables on records created from record producers, so you could just write a ui policy that does not apply when the record producer is being filled out, but does apply on the back end.



Service Catalog variable editors


Thanks Brad, I am currently on Istanbul.


manikorada
ServiceNow Employee
ServiceNow Employee

Can you try just this in your client script :




$(variable_map).select("item").each(function (elmt){


try {


g_form.setDisabled('variables.' + elmt.getAttribute('qname'),true);


} catch (err) {


}


});