Catalog UI policies getting overridden by a onload client script

abcmh
Giga Contributor

Hello ,

I have a catalog item which has multiple catalog ui policies and action on the variables. The policy and actions hide variables or make them available on selection of

certain variables.

It was working fine until I added an onload client script to make some variables read only onload,.

Basically I have about 6 date variables that were editable and using a calendar control and the user could still type in the date if needed.

it was suggested to make these variables read only and can only be populated by selection from the calendar control. So I added the following script below to make them readonly with calendar control to add the date. All these 6 date variables had a catalog ui policy and action that would make them visible only when certain variables were selected otherwise these date variables will be hidden. However after adding the onload client script all the date variables are now visible all the time and the catalog ui policy is being over ridden

Why would the behavior change when I added the onload client script. The script below makes the variables readonly.

Any input will be appreciated. Thanks.

function onLoad() {
    //Type appropriate comment here, and begin script below
  // Sets the field's background to a gray, making is appear read-only.  
  g_form.getControl('CRE_BC_ContractStartDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_BC_ContractStartDate').readOnly=true;  

  g_form.getControl('CRE_BC_ContractEndDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_BC_ContractEndDate').readOnly=true;  

  g_form.getControl('CRE_BLR_ReqCompletionDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_BLR_ReqCompletionDate').readOnly=true;

  g_form.getControl('CRE_BUO_ContractStDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_BUO_ContractStDate').readOnly=true;

  g_form.getControl('CRE_BUO_ContractEndDt').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_BUO_ContractEndDt').readOnly=true;

  g_form.getControl('CRE_OCC_ReqOccupanyDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.getControl('CRE_OCC_ReqOccupanyDate').readOnly=true;

}

5 REPLIES 5

Chuck Tomasi
Tera Patron

Hi Amol,



The proper method for making something read-only is



g_form.setReadOnly('field_name', true);



Making a date field read-only (except for the calendar control) is not currently supported.


I invite you to open an enhancement request! Our product managers DO listen.


Enhancement requests: Tell us how you would improve the ServiceNow product


Thanks Chuck. I modified the script per you suggestion but it is only making the first 2 date variables in the script read only. The others are editable.



So do you mean that making a   date variable readonly on a catalog item is not supported? All the date variables on my item do have a calendar control.


Plus why is the catalog ui policy being over ridden by the onload client script below? If I remove the script , the ui policy and actions on the item work fine.



function onLoad() {
    //Type appropriate comment here, and begin script below
  // Sets the field's background to a gray, making is appear read-only.  
  g_form.getControl('CRE_BC_ContractStartDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_BC_ContractStartDate', true);

  g_form.getControl('CRE_BC_ContractEndDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_BC_ContractEndDate',true);  


g_form.getControl('CRE_BLR_ReqCompletionDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_BLR_ReqCompletionDate',true);


  g_form.getControl('CRE_BUO_ContractStDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_BUO_ContractStDate',true);


  g_form.getControl('CRE_BUO_ContractEndDt').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_BUO_ContractEndDt',true);


  g_form.getControl('CRE_OCC_ReqOccupanyDate').disabled = 'true';  
  // Sets the field read-only  
  g_form.setReadOnly('CRE_OCC_ReqOccupanyDate',true);

}



}


Hi Amol,



Yes, you can make the fields read-only using g_form.setReadOnly(). You don't need to use g_form.getControl() if all you are doing is making the field read-only. g_form.setReadOnly() makes the entire field read-only (including the ability to use the calendar picker.)



The requirement I heard from you is that you want to make the text entry area read only while leaving the calendar picker active. That's the part that is unsupported. The field is either completely on or completely off, not half-way.


Hi Chuck,



Yes, I want to make the text are read only and make the user select the date from the calendar control.


It works if I use this script where it makes the text area of the variable read only and allows me to select the date from the calendar control. But as I said it is over riding the catalog ui policies and making the variables visible all the time.


So my question is why are ui policies and action being overridden ?



function onLoad() {


    //Type appropriate comment here, and begin script below


  // Sets the field's background to a gray, making is appear read-only.  


  g_form.getControl('CRE_BC_ContractStartDate').disabled = 'true';  


  // Sets the field read-only  


  g_form.getControl('CRE_BC_ContractStartDate').readOnly=true;


}


Thanks,


Amol