The CreatorCon Call for Content is officially open! Get started here.

Calling UI Macro from Client Script

miyu
Tera Guru

How can I call UI macros from client scripts?
When I call UI Macro, I get a console error.
I want to call it from a client script, not a catalog client script.

1 ACCEPTED SOLUTION

Hi @miyu ,

 

It won't work that way. Reason for your error is you are calling a function setDesc() from your client script which is not defined in it, rather it's in UI Macro which is not callable from there.

 

Copy paste your script from client script and paste it inside <script> </script> section of your ui macro inside a function and call it as shown below:

 

<script>
function setValuesOnLoad(){
//Copy your client script here from line number 2 - 17

}

//Keep your previous code available in UI Macro i.e. setDesc() funstion as it is here


window.onload = setValuesOnLoad; // add this line just before </script>
</script>

 

I hope this help.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

View solution in original post

8 REPLIES 8

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You can call the macro function from CS.



Macro:

 

<script>

 

function a()

 

{}

 

</script>



Client script:

 

You have to call that a function from Client script

 

a();

 

 

Also you can follow the method mentioned in the below article:-

 

https://www.basicoservicenowlearning.in/2020/08/call-ui-script-from-client-script.html

 

Please mark my answer as correct based on Impact.

Thanks for the reply.
I have tried the above method but I get the following console error

error.png
Do I need to create a UI Script?

kamlesh kjmar
Mega Sage

Hi @miyu ,

 

Could you please elaborate what do you mean by calling UI macro in client script? What is in your UI Macro that you want to call? A screenshot will be more helpful to understand your issue, if you can provide.

 

Regards,

Kamlesh

Thanks for the reply.
I have created the following UI Macro and Client Script.

â–¼UI Macro

macro.png

 

â–¼Client Script

client script.png

However, I get the following console error.

error.png


What should I do?