how to call a ui macro from on change client script which(UI macro) should create dynamic input text fields based on input form variable ?

naveen_snow
Kilo Contributor

Still there are many questions unanswered even after searching community   ?

1. How can we call UI macro from client script? can any one help me with syntax ?

(as said in one community post .. calling required function in ui macro but this is not actaully working)

2. Is there a posibility to create dynamic input fields based on   any input of a catalog form , if so how can we do that. i.e lets say if i want to create multiple users using catalog request , i will provide number of users to add.Let's Say 6 so the macro should dynamically create a from which should have 6 fields   !..

How can we achieve this scenario   ??

7 REPLIES 7

Masha
Kilo Guru

Not sure if this is a bit late but:



1. You could put <script></script> section inside your UI Macro and declare a function there that can be called from a Client Script as long as that UI Macro is present on the form. But I would limit your UI Macro code to just HTML if possible and have javascript in client scripts.


2. Yes, there is more than one way to do it and it is a lot of work. Here is how I do it in a nut shell: 1. Create a UI Macro to hold you table HTML structure (make sure your table has a unique id). You can also add styling for the table in the <style></style> section of your UI Macro if needed. 2. Create an onLoad script in your Catalog Item that will hold your addRow() function that will add rows of data or input fields to your table dynamically using your table id and DOM manipulation (google for "javascrip add rows to a table dynamically", there are plenty of examples out there). At this point your will be flying completely outside of SN variables and will be building your input fields using DOM and styling them on your own. 3. Have a trigger that will call your addRow() function, which will go inside one of the onChange scripts or UI Policies or a button... 4. What are you doing with these rows onSubmit? I would suggest reading them in (google it), validating inputs as needed and putting them in to a JSON object, converting it to a string and storing them in a hidden field on your form. You can convert the string from this field back into JSON object and rebuild you table on an RTIM or SC Task or read in the values to save them to a SN table.



Hope this helps.


Veera14
Tera Contributor

Hi Masha

 

Do you know how do i call function in the onLoad Client Script from a macro. I have button that is showing in the RITM, on click of that button i want the function in onLoad Script to trigger and do calculation from a column Multi row variable set and update a field in the catalog item.

 

UI Macro

<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<input id="calc" type="button" value="Calculate Total Area" onclick="xyz();" />
<script>

function xyz(){
javascript:alert('Hi');

calcTotal();
}


</script>


</j:jelly>

 

 

onLoad Script

 

function onLoad() {
//Type appropriate comment here, and begin script below

}
function calcTotal() {
var mrvs = this.my_g_form.getValue('area_allocations');

var objList = JSON.parse(mrvs);
for (var i = 0; i < objList.length; i++) {
calculatedArea += parseFloat(objList[i].allocated_area);
}

g_form.setValue('total_allocated', calculatedArea);

}

 

find_real_file.png

Artemis15
Kilo Guru

Well you can create a script include and call UI Macro through that script include.



Use this code to invoke UI Macro at server side



var runner = new GlideJellyRunner();


var result =   runner.runMacro('<<--UI Macro Name-->');


gs.print(result);




Hope this would help you.



--


Cheers,


AR


Ankur Bawiskar
Tera Patron
Tera Patron

Hi,



you can create a UI macro and include a function inside the script tag


you can then call the ui macro function from catalog client script.


This will work only when you have the UI macro included as a variable on the catalog form



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader