Onclick UI action should be grayed out

coolsaurabh
Tera Contributor

I have created a UI action Locked. I want when user click on it , it should   be grayed out . Screenshot attached (UI Action encircled in red) :-

Qt.jpg

Thanks,

Saurabh

1 ACCEPTED SOLUTION

ark6
Mega Guru

Hi Sourav,



Here you go. The below procedure works for me



1. Create a custom checkbox on your form(for me its flag)


find_real_file.png



2. Write the below code on the locked out UI action



function hello()


{



  document.getElementById("159c99bb4f303200b5d50ab18110c747").disabled = true;


  var ga = new GlideAjax("hello");


    ga.addParam("sysparm_name","hello2");


    ga.addParam("sysparm_parent",g_form.getValue('number'));


    ga.getXML(test2);


function test2(response){


var answer = response.responseXML.documentElement.getAttribute("answer");


}


  }


find_real_file.png



3. And the script include



var hello = Class.create();


hello.prototype = Object.extendsObject(AbstractAjaxProcessor, {


hello2: function() {


var groupID=this.getParameter('sysparm_parent');


  gs.log('testing1'+groupID);


  var gr=new GlideRecord("incident");


gr.addQuery('number',groupID);


gr.query();


if(gr.next())


{


gr.u_flag='true';


gr.update();


}




},


      type: 'hello'


});



find_real_file.png



4. To control the reloading issue, write the below onload client script



function onLoad() {


    //Type appropriate comment here, and begin script below


    if(g_form.getValue('u_flag')=='true')


  {


  document.getElementById("159c99bb4f303200b5d50ab18110c747").disabled = true;


  }


}




Please note: I haven't used the try/catch statements, but you need to use it in your code while playing with DOM.


Also you need to hide the flag button from the form through UI policy or ACL(dont remove it from form layout or else it will not work).



Please hit LIKE or mark HELPFUL if found appropriate


View solution in original post

27 REPLIES 27

Rakesh Mamidi
ServiceNow Employee
ServiceNow Employee

Use the UI action as both client and server side .



In client side , use $j('#<Button ID>').attr("disabled", true);


coolsaurabh
Tera Contributor

Hi Rakesh,



I just create a UI action and when user click on it some fields will be grayed out as mentioned below :-



function set(){


  g_form.setReadOnly('start_date', true);


  g_form.setReadOnly('end_date', true);


  g_form.setReadOnly('cost', true);


  g_form.setReadOnly('budget_cost', true);


  g_form.setReadOnly('u_Tag', true);


  g_form.setReadOnly('dependency', true);



}



But I didn't get your point. Could you please explain in detail.


I would say that:


  1. clicking the button should really change a status record, and save this back to the DB.
  2. An ACL can make those fields read-only, so that they're greyed-out to the viewer.
  3. A condition on the UI action can show it only if the record's not currently locked. (If it is, then don't display the button).

At present, the UI action only changes the form appearance because there's nothing persisting this event. Click it, refresh/reload the form.. and the fields return to their unchanged state.


ark6
Mega Guru

Though not recommended, this requirement is possible with DOM manipulation.



Get the id of your button by inspection the element through F12 (browser developer tools)



On the UI action check the client checkbox and mention an onclick function.



Use the below script in the body. Also use a try/catch statements while using DOM manipulation



function hello()


{


  document.getElementById("159c99bb4f303200b5d50ab18110c747").disabled = true;


}



find_real_file.png



Please hit LIKE or mark HELPFUL if found appropriate