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
Hi @Community Alums
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:-