How to make date text box Read-Only in Service Portal

Kyu Min
Kilo Guru

Hi all,

I'm kind of new developing in SN.

I need to find a way to make read-only the text box of the date variable so force the end user to use the calendar (datepicker) to select the date in Service Portal.

datepicker.png

The image is from the standard UI. The text box is disabled but I'm able to select the date with the calendar (datepicker).

So far, I've been able to make it work in the standard UI with this Catalog Client Script:

UI Type: Both

Type: onLoad

function onLoad() {

    // Checks if the form is loaded in mobile or desktop

    if (window == null) {

          // For Service Portal - Currently not working

          //g_form.hasField('date_variable').style.backgroundColor = '#F0F0F0';

          //g_form.hasField('date_variable').readOnly = true;

          document.observe("dom:loaded", function(){

                $('date_variable').setAttribute('disabled', true);

          });

    } else {

          // For standard UI

          g_form.getControl('date_variable').style.backgroundColor = '#F0F0F0'; // Sets gray color to background

          g_form.getControl('date_variable').readOnly = true; // Sets the text box as RO

    }

}

g_form.getControl() does not work in Service Portal.

I've researched all day and still haven't found a way to make it work in SP.

Any help is greatly appreciated.

Thanks!

1 ACCEPTED SOLUTION

sndangibbard
Mega Guru

If you only need this on one specific Date variable...



Get the sys_id of the variable


  • Service Catalog > Maintain Items > [open item the variable belongs to]
  • Variables (related list) > right-click on the variable > Copy sys_id


Create a Catalog UI Policy


  • Service Catalog > Maintain Items > [open item the variable belongs to]
  • UI Policies (related list) > New
    • Onload: true
    • Run script: true
    • Run scripts in UI type: All
    • Execute if true:


function onCondition() {


var $ = this.jQuery;


$('input[id*="yourSysIdHere"]').attr('readonly', true);


}


View solution in original post

24 REPLIES 24

sndangibbard
Mega Guru

If you only need this on one specific Date variable...



Get the sys_id of the variable


  • Service Catalog > Maintain Items > [open item the variable belongs to]
  • Variables (related list) > right-click on the variable > Copy sys_id


Create a Catalog UI Policy


  • Service Catalog > Maintain Items > [open item the variable belongs to]
  • UI Policies (related list) > New
    • Onload: true
    • Run script: true
    • Run scripts in UI type: All
    • Execute if true:


function onCondition() {


var $ = this.jQuery;


$('input[id*="yourSysIdHere"]').attr('readonly', true);


}


Thanks for you recommendation Dan.


I've tested it but unfortunately, it didn't worked in SP. I was still able to type in the date text box.


Hi Kyu,



I've tested this in Service Portal and it was working, can you add an alert or some other debug output to the script to ensure that it is executing?



Thanks


Dan


This works great for the main application, but will not work with the Service Portal.  JQuery and Angular is not getting along these days.  Also I never could find a UI type field in the Catalog UI Policy form.

We were able to achieve the ready only text box on the date variable in Service Portal by using your suggestion with Kingston:

function onCondition() {

 

var $ = this.jQuery;

 

$('input[id*="yourSysIdHere"]').attr('readonly', true);

 

}

 

However, it is no longer working for us in London.  The text box is no longer grayed out.  Any new suggestions for achieving the same effect in the London release?