- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 06:55 AM
Hi Team,
I am working on a requirement.
Scenario:
I want to create a record with some default values in a custom table if any records get created in the incident, request, or change table.
I am trying to achieve this through Script Include and Business rule mentioned below. Please let me know what is wrong in that?
Script Include:
var CallApproval = Class.create();
CallApproval.prototype = {
initialize: function() {
},
insertapprovalrecord: function(){
var gr = new GlideRecord('x_intp_custom_appr_custom_approval');
gr.initialize();
gr.approver = this.requested_for.manager;
gr.state = 'Requested';
gr.source_table_new = this.sys_id;
gr.source_table = reqtab.sys_id;
gr.insert();
},
type: 'CallApproval'
};
Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var callapproval = new CallApproval().insertapprovalrecord(current);
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 07:40 AM
Hi Nick,
You can leave your business rule as is, it's fine to pass current into the script include function call as it's linked by reference.
Another point is that you should really return a value from the function in your script include, as your BR is expecting it as you're storing the result in a variable. Just replace this, gr.insert(); with return gr.insert();
This with then return the sys_id of your created record to your callapproval variable in your BR.
Thanks
Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 07:18 AM
Hi Nick,
You are passing current into your insertapprovalrecord function but then not using it. While current would be available to your script include functions directly it's good practice to pass them as arguments.
insertapprovalrecord: function(request){
if (gs.nil(request)){
return;
}
var gr = new GlideRecord('x_intp_custom_appr_custom_approval');
gr.initialize();
gr.approver = request.requested_for.manager;
gr.state = 'Requested';
gr.source_table_new = request.getUniqueValue(); //Assuming this is the sys_id for the source record
gr.source_table = request.getTableName(); //Assuming this is the table of the source record
gr.insert();
},
I'd also avoid call your variables gr and give them meaningful names.
Let me know how you go on.
Thanks
Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 07:27 AM
Hi Justin,
What will you suggest in business rule?
As per your suggestion, the code will look like.
Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var request = current;
var callapproval = new CallApproval().insertapprovalrecord(request);
})(current, previous);
Script Include:
insertapprovalrecord: function(request){
if (gs.nil(request)){
return;
}
var gr = new GlideRecord('x_intp_custom_appr_custom_approval');
gr.initialize();
gr.approver = request.requested_for.manager;
gr.state = 'Requested';
gr.source_table_new = request.getUniqueValue(); //Assuming this is the sys_id for the source record
gr.source_table = request.getTableName(); //Assuming this is the table of the source record
gr.insert();
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 07:40 AM
Hi Nick,
You can leave your business rule as is, it's fine to pass current into the script include function call as it's linked by reference.
Another point is that you should really return a value from the function in your script include, as your BR is expecting it as you're storing the result in a variable. Just replace this, gr.insert(); with return gr.insert();
This with then return the sys_id of your created record to your callapproval variable in your BR.
Thanks
Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2021 07:43 AM
Thanks Justin, it makes sense to me.