- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2022 10:17 PM
Hi All,
There are three fields on the form, based on one field I am trying to populate data in two other fields
On Process table we have two reference fields ABC Process Owner and XYZ Process Owner
1) "Impacted Process" (Field 1 - List collector - can select two or more values) - Multiple options present in the list collector
2) Based on selection in Impacted Process "ABC Process owner"(Field 2 - Multiline text - read only) data should populate line by line (If one Impacted Process is selected then that Process' owner should be populated. If two selections, respectively owner should get populated here line by line)
3) Based on selection in Impacted Process "XYZ Process Owner"(Field 2 - Multiline text - read only) data should populate line by line (If one Impacted Process is selected then that Process' owner should be populated. If two selections, owner should get populated here line by line)
PFB form design
Script Include -
populateImpactServices: function() {
var sysid = this.getParameter('sysparm_user_name');
var obj = {};
var rec = new GlideRecord('cmdb_ci_business_process');
rec.addQuery('name', sysid);
rec.query();
while (rec.next()) {
obj.owned_by = rec.getDisplayValue('owned_by').toString();
obj.managed_by = rec.getDisplayValue('managed_by').toString();
}
var data = JSON.stringify(obj);
return data;
Client Script -
var arr = {};
var caller = g_form.getDisplayValue('impacted_process');
var glide = new GlideAjax('populateImpactServices');
glide.addParam('sysparm_name', 'populateImpactServices');
glide.addParam('sysparm_user_name', caller);
glide.getXMLAnswer(Hello);
function Hello(response) {
arr = JSON.parse(response);
var owner = '';
var manage = '';
for (var i = 0; i < arr.length; i++) {
var value = 'Process' + (i + 1) + ': ';
var value1 ='Process' + (i + 1) + ': ';
value += arr[i].owned_by;
value1 += arr[i].managed_by;
owner += value + '\n';
manage += value1 + '\n';
}
g_form.setValue('abc_process_owner', owner);
g_form.setValue('xyz_process_owner',manage);
}
}
Please let me know where I am lagging.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:47 PM
Please use the below code block in your script include
populateImpactServices : function() {
var sysid = this.getParameter('sysparm_user_name');
var obj = {};
var owned_by_arr = [];
var managed_by_arr = [];
var rec = new GlideRecord('cmdb_ci_business_process');
rec.addQuery('name', "IN", sysid);
rec.query();
while (rec.next()) {
var process_name = rec.name;
owned_by_arr.push(process_name+":"+rec.getDisplayValue('owned_by').toString());
managed_by_arr.push(process_name+":"+rec.getDisplayValue('managed_by').toString());
}
obj.owned_by = owned_by_arr.join("\n");
obj.managed_by = managed_by_arr.join("\n");
return JSON.stringify(obj);
}
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 09:40 PM
Hi Vasantharajan,
with following script include and client script
Script Include -
populateImpactServices: function() {
var name = this.getParameter('sysparm_user_name');
var obj = {};
var arrayOfObj = [];
var impacted_processIds = name.split(",");
var count = impacted_processIds.length;
gs.log("Line 11:" +count);
for (var i = 0; i < count; i++) {
var rec = new GlideRecord('cmdb_ci_business_process');
rec.addQuery('sys_id', impacted_processIds[i]);
rec.query();
while (rec.next()) {
obj.owned_by = rec.owned_by.getDisplayValue();
obj.managed_by = rec.managed_by.getDisplayValue();
}
arrayOfObj.push(obj);
}
var data = JSON.stringify(arrayOfObj);
return data;
},
Client Script - (onChange of Impacted Process field)
var caller = g_form.getValue('impacted_process');
var arr = {};
var glide = new GlideAjax('populateImpactServices');
glide.addParam('sysparm_name', 'populateImpactServices');
glide.addParam('sysparm_user_name', caller);
glide.getXMLAnswer(Hello);
function Hello(response) {
arr = JSON.parse(response);
var owner = [];
var manage = [];
for (var i = 0; i < arr.length; i++) {
owner += arr[i].owned_by + '\n';
manage += arr[i].managed_by + '\n';
}
g_form.setValue('abc_process_owner', owner);
g_form.setValue('xyz_process_owner', manage);
}
}
Output - It is populating values of first input
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:06 PM
Please use the code in onChange catalog client script so that script will automatically iterate for the values changed and set the value of the respective fields.
I've use "IN' condition in the script include which takes care of it
var rec = new GlideRecord('cmdb_ci_business_process');
rec.addQuery('name', "IN", sysid);
rec.query();
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:12 PM
Thanks Vasantharajan, It is working
What to add If I want to print like below
"Process name : Owner"
Process name : Owner
Instead of printing "Owner" only, because it is not coming in sequence.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:47 PM
Please use the below code block in your script include
populateImpactServices : function() {
var sysid = this.getParameter('sysparm_user_name');
var obj = {};
var owned_by_arr = [];
var managed_by_arr = [];
var rec = new GlideRecord('cmdb_ci_business_process');
rec.addQuery('name', "IN", sysid);
rec.query();
while (rec.next()) {
var process_name = rec.name;
owned_by_arr.push(process_name+":"+rec.getDisplayValue('owned_by').toString());
managed_by_arr.push(process_name+":"+rec.getDisplayValue('managed_by').toString());
}
obj.owned_by = owned_by_arr.join("\n");
obj.managed_by = managed_by_arr.join("\n");
return JSON.stringify(obj);
}
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 11:05 PM
Thank you so much Vasantharajan !
learnt so many things from this thread.