How to make a field readonly in one catalog item which is part of mrvs

Anirudh_saraf
Tera Contributor

Hi Community,

 

The requirement is to make a variable read only in one catalog item.

 

For example there is a field called as hardware which is part of  a variable set. This variable set is part of catalog A and Catalog B. We should make this field editable in catalog A and it should be in readonly in Catalog B.

 

Please let me know if anyone have implemented this earlier

 

Thanks 

10 REPLIES 10

@Anirudh_saraf This happens because the MRVS control doesn't allow dot walk on the internal fields which are part of it. The only way you can get access to these fields are by creating client script/UI Policy within the MRVS itself.

Arya123
Tera Expert

Hi @Anirudh_saraf  In the catalog item where you want the variable to be read-only 

select the Catalog Client Script 

function onLoad() {
  g_form.setReadOnly('welcome_note',true); // Replace 'welcome_note' with the actual variable name
}

Anirudh_saraf
Tera Contributor

@Arya123  Already tried that it was not working

Satishkumar B
Giga Sage
Giga Sage

Hi @Anirudh_saraf ,

 

Using UI Policies:

  1. Identify the Variable Set and Fields:

    • Locate the variable set that contains the field hardware.
  2. Create a UI Policy:

    • Navigate to Service Catalog > Catalog Definitions > Maintain Items.
    • Open the catalog item (Catalog B) where you want the field hardware to be readonly.
    • Create a new UI Policy specific to this catalog item.
  3. Define Conditions:

    • Set conditions in the UI Policy to apply only when the catalog item is Catalog B. This typically involves using a condition based on the catalog item’s unique identifier or some other distinguishing feature.
  4. Set UI Policy Actions:

    • Configure the UI Policy action to make the hardware field readonly.

Example UI Policy Configuration:

  • Name: Make hardware Readonly in Catalog B
  • Table: Variable Set (or the specific table where hardware resides)
  • Condition: Catalog Item is Catalog B
  • Action: Set Field Read-only
    • Field: hardware
    • Read-only: True

Using Catalog Client Scripts:

Alternatively, you can use Catalog Client Scripts to dynamically control field behavior based on the catalog item:

  1. Navigate to the Catalog Item:

    • Open Catalog B in the Service Catalog.
  2. Create a Catalog Client Script:

    • Write a Client Script specific to Catalog B.
    • Use the onLoad or onSubmit script include to check the catalog item and set the hardware field as readonly.

 

 

function onLoad() {
    // Check if this is Catalog B
    if (g_form.getTableName() === 'sc_cat_item' && g_form.getUniqueValue() === 'catalog_b_item_sys_id') {
        // Make 'hardware' field readonly
        g_form.setReadOnly('hardware', true);
    }
}
​

 

 

 

By leveraging UI Policies or Catalog Client Scripts, you can effectively control field visibility and readonly states based on specific catalog item criteria in ServiceNow. Adjust the examples above to fit your specific variable set and catalog item configurations.

--------------------------------------------------------------------------------------------------------------------
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

Anirudh_saraf
Tera Contributor

@Satishkumar B  Thanks for detailed explanation. I have tried but it doesnot seem to work.

 

I have tried creating a ui policy on the catalog item where i want to make the variable read only and also tried with catalog client scripts.