How to write for loop in ui macro
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2024 01:05 PM
I have a requirement where I am getting some array values from script include to ui macro for which I need to write a for loop and set the values to MRVS variable
here is the sample code:
var answer = ['test','test1','test2'];
var obj={};
var arr=[];
var split = answer.split(',');
for(var i=0; i<split.length; i++){
obj = {
'name' : split[i].toString()
};
arr.push(obj);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 05:04 AM
<j:var var="answer" value="['test','test1','test2']"/>
<j:var var="arr" value="[]" />
<j:forEach items="${answer}" var="item">
<j:var var="obj" value="{ 'name' : '${item}' }"/>
<j:expr value="arr.push(obj)"/>
</j:forEach>
<j:expr value="g_form.setValue('mrvs_field_name', JSON.stringify(arr))"/>
<j:var>
: This tag is used to declare and initialise variables in Jelly.<j:forEach>
: This tag is used to iterate over the array elements.<j:expr>
: This tag is used to execute JavaScript expressions within the Jelly script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 05:59 AM
You can try bellow script once:
<g:script>
// array from the Script Include
var answer = ['test', 'test1', 'test2'];
// empty array to store the formatted objects
var arr = [];
//push objects to the array
for (var i = 0; i < answer.length; i++) {
var obj = {
'name': answer[i].toString() // Set the MRVS field name to the array value
};
arr.push(obj); // Push object to array
}
// set value for the MRVS field
g_form.setValue('mrvs_field_name', JSON.stringify(arr));
</g:script>
i hope my answer helps you to resolve your issue, if yes please mark my answer helpful & correct.
thank you
rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:47 AM
Hi,
I have tried the script below and it seems to work for me:-
CS:-
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/