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.

UI Action Condition from Script Include

jrottman
Giga Contributor

I'm trying to get a UI Action to appear based on a function in a script include.

My script include contents:

function myCustomShowUIButton() {

  return false;

}

The condition line of my UI action:

myCustomShowUIButton()

I would expect the UI action button to not appear because the function returns false.   However this does not appear to work.   Any ideas?

Jason

1 ACCEPTED SOLUTION

jrottman
Giga Contributor

You are correct about UI actions being executed on the client side, but incorrect regarding the condition and visibility.   If the condition on a UI action is not met, the html and java-script is not even sent to the browser, so the button never appears at all (not even briefly).   If using a client script to hide a button, the html and javascript must first be sent to the browser, and it is displayed.   Once the client scripts then catch up, the button disappears, but this can take about 1 second so the button appears briefly.



Sadly I do not think a function from a Script Include (whether a simple function or using a class) can be used on the condition line of a UI Action.



Jason


View solution in original post

7 REPLIES 7

HV1
Mega Guru

1. What is your script include name?   The way to call script include functions are


new <script>.<method name>(<parameters>);



Also while calling it from conditions, I have always see using javascript before the call. i.e.:


javascript: new <script>.<method name>(<parameters>);



2. Have you confirmed that your function is being called correctly.   I would type a gs.log statement before return to confirm that function call is correct.



- Hardik Vora


zica
Giga Guru

Hi Jason,



A script include shouldd have a class and then a function, you can't use it without these two elements.


Here is an article concerning script include : Script Includes - ServiceNow Wiki



Why are you using script include ti hide/show button ? it could be done easily without script include. Here is another article for a sample of script : Client & Server Code in One UI Action - ServiceNow Guru



if you really want to use a script include, here is a way to achieve what are you trying to do, you have to script something like this in your script include:


  1. var TestFunction = Class.create();  
  2. TestFunction.prototype = Object.extendsObject(AbstractAjaxProcessor, {  
  3.     testingFunction: function() {  
  4. return false      
  5.           }  
  6.           });


Then in your ui script you have to put something like that


var ga = new GlideAjax('TestFunction');


ga.addParam('sysparm_name','testingFunction');


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


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


    alert(answer);


}



let me know it that works or not, this script should return the false value, you have to add in your ui action that if the answer == 'false' then


g_form.setVisible('button name', false)



Kind regards,


ZA


Mark Helpful or correct answer depending on the impact of the answer


One note   : here is another article which shows how to interact with script include : http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0


jrottman
Giga Contributor

I want the button only to appear if a record appears in a different unrelated table.



I'm sure I can do it with a UI policy or a client script, I just thought the condition on a UI Action would be cleaner.   Otherwise, with a ui policy or client script, it seems like the forms shows the button, and then as soon as the java-script catches up, quickly hides the button.



Jason