Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client Script not working on Service Portal

Zod
Giga Guru

Hi Experts,

I understood, that normal client scripts will not work on service portal ... I also found some posts regarding this issue, but I don't get it. How to achieve ... you anyone please tell me in more detail what to do?

My on client scripts is something like this ...

function onLoad() {

  if (g_form.getUniqueValue() !== g_user.userID)

  {

  g_form.removeOption('u_abc', 1);

  g_form.removeOption('u_def', 1);

}

  }

21 REPLIES 21

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hello,



Yes. I don't see any issue with the script. Could you please check the field name ? Also, can you please check browser console log (in-case if you are getting any error).


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Vem



Not sure if you copied your code from the instance but if this is the case you have a missing {



function onLoad() {


  if (!g_user.hasRole("xxxxxxx")){


  alert("Disable should be removed");


  g_form.removeOption('notification', 1);


  }



Cheers


R0b0


Thnx. Was just not copied.



SP_Profil_removeoption.JPG


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Vem



I had a look around and i think Brian is right. g_user probably is not completely supported.


I mean g_user.getUserID works but not other methods. I think makes sense to open a ticket in HI.


On the other hand you can achieve the same result in a different way..create an async ajax call to obtain the user ID.



I know it's a different kind of effort just to obtain the user_id but it's the only way for the moment.



Cheers


R0b0


Hi R0b0,


thanks! I would be fine with any workaround.


Just JS is already hard for me (as low-code person) ... ajax is absolutely not my area.


Could ou provide some code that could be used?


If fact i need an option to avoid showing certain sys_choice if it is not YOUR sys_user record you want to change ..



I also struggle hiding complete fields in the SP depending on other field values ... this client script is also not working ;-(



function onLoad() {


  if (g_form.getValue('u_xxx') !== 'true')                         // check condition


  g_form.setDisplay('u_yyy', false);                                     // Hide field


}



Somehow lit looks like a lot of additional effort to get things running properly also in the SP .-(