Catalog client script is not making variable read only on catalog form.

Obito
Tera Expert

Catalog client script is not making variable read only on catalog form. The variable name is 'Tier Confirmation' 

Here I am checking if catalog task state is closed complete and task name is 'xyz Tier Confirmation'

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('state').toString() == '3' && g_form.getValue('u_task_name') == 'xyz Tier Confirmation') {
        g_form.setReadOnly('tier_confirmation', true);
    }
}

 

 

6 REPLIES 6

ashishdevsingh
Tera Expert

Make sure you are not making that field as mandatory before making read only. 
rest as suggested: use Alert to find it out if it is going inside the condition or not.  

Juhi Poddar
Kilo Patron

Hello @Obito 

I think the reason why you are not getting the expected result is:

  • Catalog task "state" is on catalog task table whereas variable "Tier Confirmation" is on catalog item or request item.
  • These two are at different table & position. 
  • The above client script will work when both the fields are at same form.

To achieve the desired result:

  • Write an onload client script on sc_req_item table and an script include to check the sc_task state.

Client script:

 

function onLoad() {
    // Get the sys_id of the current request item
    var reqItemSysId = g_form.getUniqueValue();

    // Call the Script Include asynchronously
    var ga = new GlideAjax('CheckTaskStatus');
    ga.addParam('sysparm_name', 'isTaskClosedComplete');
    ga.addParam('sysparm_req_item_id', reqItemSysId);
    ga.getXMLAnswer(function (response) {
        var isClosedComplete = response;

        // If the related task is in "Closed Complete" state, make the variable read-only
        if (isClosedComplete === 'true') {
            g_form.setReadOnly('variables.tier_confirmation', true); // Use variables.<variable_name> for catalog variables
        }
    });
}

 

script include:

 

    isTaskClosedComplete: function (reqItemSysId) {
        var task = new GlideRecord('sc_task');
        task.addQuery('parent', reqItemSysId);
        task.addQuery('state', 3); // Assuming "Closed Complete" has the value '3'
        task.query();
        return task.hasNext().toString(); // Returns true if a task in "Closed Complete" state exists
    },

 

Note: Script include should be client callable

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar