- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2023 03:35 AM
Hello All,
I want to pass 2 variables from script include to Business Rule...below are the codes...Can someone help me?
Script Include Function:
_businessApplication: function(ci) {
var service_offering;
var CI;
var cmdbRelGr = new GlideRecord("cmdb_rel_ci");
cmdbRelGr.addQuery("child.name", ci);
cmdbRelGr.query();
if (cmdbRelGr.next()) {
service_offering = cmdbRelGr.parent;
CI = cmdbRelGr.child;
}
return XXX;
},
I want to return both service_offering and CI....How can I send it? and how can I fetch it in business rule?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2023 05:54 AM - edited 01-07-2023 10:30 AM
Hello @Anirban3 ,
regarding your request I would suggest to create an object for the data you want to fetch in order to be more flexible.
Your Script Include could look like this:
var SIName = Class.create();
SIName.prototype = {
initialize: function() {},
_businessApplication: function(oCurrentCi) {
var oCiData = {};
var cmdbRelGr = new GlideRecord("cmdb_rel_ci");
// query al records where child name is current CI name
cmdbRelGr.addQuery("child.name", oCurrentCi.getValue('name')); // 1
cmdbRelGr.query(); // 2
if (cmdbRelGr.next()) { // 3
/* Note: This will run only once and fetch the values for the first record found
** Therefore the 3 lines commented with 1,2,3
** could be replaced by (if (cmdbRelGr.get(...) as follows with only one line of code:
** if (cmdbRelGr.get("child.name", ci.getValue('name'))) {
*/
oCiData.service_offering = cmdbRelGr.getDisplayValue('parent');
oCiData.ci = cmdbRelGr.getDisplayValue('child');
}
return oCiData;
},
type: 'SIName'
};
In your Business Rule you can fetch the data as follows:
var oCiData = new SIName()._businessApplication(current);
// Use values in an info message:
var sServiceOffering = oCiData['service_offering'];
var sCi = oCiData['ci'];
gs.addInfoMessage('ServiceOffering: ' + sServiceOffering + '\nCI: ' + sCi);
/* Output:
** ServiceOffering: SAP WEB01
** CI: SAP AppSRV01
*/
// Setting a field value:
current.setValue('description', 'Current CI is: ' + sCi);
// Log the complete object to System Log:
gs.info('Test Script Include SIName() for fetching CI data: \n' + oCiData);
/* Output can be found in System Log [syslog_list.do] table:
** {
** "service_offering": "SAP WEB01",
** "ci": "SAP AppSRV01"
** }
*/
-------------------
Please note, that your if condition runs only once and returns the values for the first record it can find.
If this is fine, then use the above code snippets.
If you need all values of all the relation records it can find, then you have to adjust your code by using a while (gr.next()) instead of if (gr.next()) and push the data into an array.
Quickly written it could look like this:
_businessApplication: function(ci) {
var oCiData = {}; // create object for Ci data
var getRels = []; // array for data of each records found
var cmdbRelGr = new GlideRecord("cmdb_rel_ci");
cmdbRelGr.addQuery("child.name", ci.getValue('name'));
cmdbRelGr.query();
while (cmdbRelGr.next()) {
oCiData.service_offering = cmdbRelGr.getDisplayValue('parent');
oCiData.ci = cmdbRelGr.getDisplayValue('child');
getRels.push(oCiData);
}
return getRels;
},
Logged output would look something like this:
Then you can access all the data needed as required in your Business Rule.
Quick example for accessing all data for all records found in order to display it in your Business Rule's info message could look like this:
var oCiData = new SIName()._businessApplication(current);
gs.addInfoMessage('Show me the data: <br>' + JSON.stringify(oCiData));
// Or if you want to format the data and add custom text you could do something like this:
for(var i=0;i<oCiData.length;i++)
gs.addInfoMessage('Service Offering: ' + oCiData[i]['service_offering'].toString() + ' - CI: ' + oCiData[i]['ci'].toString());
// This would display one info message per record found with the data you want to display, see sfollowing creenshot.
-------------------
Hope this helps.
If any of this was helpful please mark my answer as helpful.
If any of this helped to solve your issue please mark as correct.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2023 10:32 AM - edited 01-07-2023 10:32 AM
Hi Anirban,
Please check the following video where it is explained step by step to return multiple values. let me know if you have further questions.
Please mark this comment as Correct Answer/Helpful if it helped you.
Cheers,
Hardit Singh