How to remove the record when a row is removed from multirow variable set at Runtime

Community Alums
Not applicable

Hi,

 

Please guide me how can we achieve the below scenario.

 

I have a Multi Row variable set which will populate the records (Example : Show relationship based on the selected CI from other variable ) at runtime.

 

now i wanted to remove a record (CI Relationship) from target table when a perticular row is removed using the 'Remove Row' option from multiRow Variable set.

 

Please guide how can we do this?

4 REPLIES 4

Ratnakar7
Mega Sage
Mega Sage

HI @Community Alums ,

 

Here is the one of way to achieve this requirement:

 

Create a new Business Rule that is triggered when a row is removed from the multi-row variable set. In the 'When to run' field of the Business Rule, select the 'After' option.

In the script section of the Business Rule, write a script to get the record that needs to be deleted from the target table.

Use the 'gs.deleteRecord()' function to delete the record from the target table.

Here is a sample code to help you get started:

 

// Business Rule Script
(function executeRule(current, previous /null when async/) {
// Get the sys_id of the record to delete
var sysId = current.variable_name.sys_id; // Replace 'variable_name' with the name of your variable set
// delete record
var gr = new GlideRecord('Target_Table_Name'); // Replace 'Target_Table_Name' with the name of your target table
if (gr.get(sysId)) {
gr.deleteRecord();
}
})(current, previous);

 

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

Can you share what existing script you are using and what's not working?

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

Community Alums
Not applicable

Hi Ankur,

 

I have created a Multi Row variable set and populated the CI relationship records when a  CI is selected from catalog variable. (onChange Client Script)

Multi Row variable set have default Edit Row, Remove Row , Add options in the table

TamilselvanPar_0-1678962138919.png

 

after populating the existing details , if i remove the record using the Remove Row option then i want the corresponding entry in the cmdb_rel_ci table to be deleted after submitting the request. please suggest how can we do this ? i am not sure if it is possible to capture the changes on MRVS.

 

Please find the script which i used to populate the multiRow variable set when a CI is selected from variable (select CI)

 

fetch_ci_details: function()
{
 var json_data = {};

var parent_ci = this.getParameter('sysparm_ci');//get parent from client script
var gr3 = new GlideRecord('cmdb_ci_router');//sample class router
gr3.addQuery('sys_id', parent_ci);
gr3.query();
if (gr3.next()) {
var get_rel_detail = this.fetch_rel_Details(parent_ci);
json_data['list_of_available_relationships'] = get_rel_detail + '';
}
}
json_data = JSON.stringify(json_data);
return json_data;
},

fetch_rel_Details: function(parent_ci) {
var relArr = [];
var reldata = {};
var pass_parent = this.getParameter('sysparm_ci');
var gr_all_rel = new GlideRecord("cmdb_rel_ci");
gr_all_rel.addQuery('parent', parent_ci);
gr_all_rel.query();
while (gr_all_rel.next()) {
reldata = {};
reldata.display_relationship_class = gr_all_rel.child.sys_class_name.getDisplayValue();
reldata.display_ci_name = gr_all_rel.child.getDisplayValue();
reldata.display_relationship_type = gr_all_rel.type.getDisplayValue();
relArr.push(reldata);
}
return JSON.stringify(relArr);

},

 

I am setting the value in Onchange Client script  using setValue():

 

g_form.setValue('list_of_available_relationships',json_data['list_of_available_relationships']);

 

Note : MRVS name is 'list_of_available_relationships'

Vivek Reddy G
Tera Contributor

Hi Tamilselvan,

 

Were you able to get a solution for this?