Updating sysapproval_approver table in UI Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2013 05:04 AM
In our form we have 'Request Approval' button. When user clicks this button what are all the approval
records with state = 'Request Approval' has to be updated with state = 'requested'. For this we added below code in 'Request Approval' action.
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.query();
while(gr.next()){
gs.print("State :"+gr.state );
if(gr.state == 'Request Approval'){
gs.print("gr.state in if :"+gr.state );
gr.state = 'requested';
}
gr.update();
gs.print("gr.sys_id : "+gr.sys_id );
}
current.u_mr_state = '2';
current.approval='requested';
current.update();
action.setRedirectURL(current);
This while loop is not working properly. It's going through the approval records one by one, once the approval record with state = 'Request Approval' is come and changing it's state to 'requested' and then updates this record. After this it's showing this print statement 'gs.print("gr.sys_id : "+gr.sys_id );' also, but it's not going to nex record in the loop and control is comming out of the loop. I don't understand what will be the problem here, even if we have more than one approval record with 'Request Approval' state it updating only one remaining are still showing wit 'Request Approval' state.
I verified the logs also, it's showing execution of before and after business rules on 'sysapproval_approver' including 'Approval Events (Task)' business rule.
Could some one tell me what will be the problem here?why it's not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2013 08:51 AM
First: whenever posting code to this forum, please surround it with code tags (
) so that the formatting is preserved.
,
Second: In your loop, you should probably only perform the gr.update() if you change a value. So it may as well be inside the if statement.
Third: I'm surprised that you are able to match on "Request Approval". In our choice list for that element, the values are all lower case. So for example the choice labeled "No Longer Required" actually has the value "not_required".
Unfortunately, none of these tips explain why your loop is exiting prematurely. You may want to confirm whether or not it really is ending prematurely. Here is a revision for testing:
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.query();
gs.print('Found ' + gr.getRowCount() + ' records.');
var x = 1;
while (gr.next()) {
gs.print(x + " - Approver : " + gr.approver.getDisplayValue() + ", State : "+ gr.state );
if (gr.state == 'Request Approval') { // make sure this is the real value
gs.print(x + " - Changing to requested.");
gr.state = 'requested';
gr.update();
}
gs.print(x + " - gr.sys_id : "+gr.sys_id );
}
current.u_mr_state = '2';
current.approval='requested';
current.update();
action.setRedirectURL(current);
I hope this helps!
Cheers,
Geoff.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2013 08:52 AM
I forgot to increment x in the loop. Here is a revision:
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id);
gr.query();
gs.print('Found ' + gr.getRowCount() + ' records.');
var x = 1;
while (gr.next()) {
gs.print(x + " - Approver : " + gr.approver.getDisplayValue() + ", State : "+ gr.state );
if (gr.state == 'Request Approval') { // make sure this is the real value
gs.print(x + " - Changing to requested.");
gr.state = 'requested';
gr.update();
}
gs.print(x + " - gr.sys_id : "+gr.sys_id );
x++;
}
current.u_mr_state = '2';
current.approval='requested';
current.update();
action.setRedirectURL(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2013 12:04 AM
It's not going to next record once sysapproval_approver is update, please see the below logs it showing records count as 9, but after 5th record is updated it's not going to next record:
Debug Output
--------------------------------------------------------------------------------
01:44:44.252: : Found 9 records.
01:44:44.254: : 1 - Approver : Dakshayani Polineni,State : requested
01:44:44.255: : 1 - gr.sys_id : 0cc213782b590100bb50525ee8da1520
01:44:44.257: : 2 - Approver : Adam Barefoot,State : requested
01:44:44.257: : 2 - gr.sys_id : 5d189af12bb1c1006754bb1319da1504
01:44:44.259: : 3 - Approver : Ahmet Mujkanovic,State : requested
01:44:44.259: : 3 - gr.sys_id : 69433a0398740d00bb500ac3b215d5cc
01:44:44.261: : 4 - Approver : Alan Cunningham,State : approved
01:44:44.261: : 4 - gr.sys_id : b6207acf98340d00bb500ac3b215d5b8
01:44:44.263: : 5 - Approver : Amy Fischer,State : Request Approval
01:44:44.263: : gr.state in if :Request Approval
01:44:44.263: : 5 - Changing to requested.
01:44:44.265: Execute before business rules on sysapproval_approver:Solution Owner before engines (order <1000)
01:44:44.269: ==> 'Set Requested on' on sysapproval_approver:Solution Owner
01:44:44.270: : current.u_requested_on : 2013-06-11 06:44:44
01:44:44.270: : current.u_approved_rejected_on :
01:44:44.270: u_requested_by: => b7bc66066590f4006754b3b35f58183f
01:44:44.270: u_requested_on: => 2013-06-11 06:44:44
01:44:44.270: <== 'Set Requested on' on sysapproval_approver:Solution Owner
01:44:44.281: Execute after business rules on sysapproval_approver:Solution Owner before engines (order <1000)
01:44:44.281: === Skipping 'SNC - Run parent workflows (Approval)' on sysapproval_approver:Solution Owner; condition not satisfied: current.state.changesTo('approved') || current.state.changesTo('rejected') || current.operation() == 'delete'
01:44:44.281: === Skipping 'SNC - Run parent workflows (Unapproved)' on sysapproval_approver:Solution Owner; condition not satisfied: (current.state.changesFrom('approved') || current.state.changesFrom('rejected')) && current.state.changesTo('requested')
01:44:44.288: Execute after business rules on sysapproval_approver:Solution Owner after engines (order >=1000)
01:44:44.289: === Skipping 'Approval Events (Non-Task)' on sysapproval_approver:Solution Owner; condition not satisfied: (current.source_table != '' && new TableUtils(current.source_table).getAbsoluteBase() != 'task') && current.state.changes()
01:44:44.289: ==> 'Approval Events (Task)' on sysapproval_approver:Solution Owner
01:44:44.289: : In approval events(task):
01:44:44.289: : event : approval.inserted
01:44:44.289: Execute before business rules on sys_user:
01:44:44.290: === Skipping 'user query' on sys_user:; condition not satisfied: gs.getSession().isInteractive() && !gs.hasRole("admin")
01:44:44.291: : approver: Amy Fischer
01:44:44.313: Execute before business rules on u_migration_request:MGR0000027 before engines (order <1000)
01:44:44.313: === Skipping 'Task Active State Management' on u_migration_request:MGR0000027; condition not satisfied: current.state.changes()
01:44:44.313: ==> 'Attach Delivery Plan' on u_migration_request:MGR0000027
01:44:44.316: <== 'Attach Delivery Plan' on u_migration_request:MGR0000027
01:44:44.316: === Skipping 'Calculate Risk' on u_migration_request:MGR0000027; condition not satisfied: gs.hasRole('itil') && gs.getProperty('glide.ui.risk_calculate_rule') == "business_rule"
01:44:44.316: === Skipping 'mark closed' on u_migration_request:MGR0000027; condition not satisfied: current.u_mr_state > 3
01:44:44.316: ==> 'mark_closed' on u_migration_request:MGR0000027
01:44:44.317: <== 'mark_closed' on u_migration_request:MGR0000027
01:44:44.317: ==> 'Outside Maintenance Schedule' on u_migration_request:MGR0000027
01:44:44.317: <== 'Outside Maintenance Schedule' on u_migration_request:MGR0000027
01:44:44.317: === Skipping 'reassignment counter' on u_migration_request:MGR0000027; condition not satisfied: current.assignment_group.changes()
01:44:44.317: === Skipping 'set awaiting CCB approval' on u_migration_request:MGR0000027; condition not satisfied: current.u_from =='7' || current.u_to == '7'
01:44:44.317: ==> 'set my migration request' on u_migration_request:MGR0000027
01:44:44.319: <== 'set my migration request' on u_migration_request:MGR0000027
01:44:44.323: === Skipping 'Set Priority for Incident and Problem' on u_migration_request:MGR0000027; condition not satisfied: current.parent.number.indexOf('INC') == 0 || current.parent.number.indexOf('PRB') == 0
01:44:44.323: === Skipping 'Stamp Approvals' on u_migration_request:MGR0000027; condition not satisfied: current.approval.changes()
01:44:44.323: ==> 'change reopen' on u_migration_request:MGR0000027
01:44:44.323: <== 'change reopen' on u_migration_request:MGR0000027
01:44:44.323: === Skipping 'mark closed' on u_migration_request:MGR0000027; condition not satisfied: current.state.changesTo(3) || current.state.changesTo(4) || current.state.changesTo(7) || current.state.changesTo(8) || current.u_incident_task_state.changesTo(4)
01:44:44.323: === Skipping 'task closer' on u_migration_request:MGR0000027; condition not satisfied: previous.active == true && current.active == false && current.state != 3 && current.state != 4 && current.state != 7
01:44:44.323: === Skipping 'task reopener' on u_migration_request:MGR0000027; condition not satisfied: (previous.active == false && current.active == true && (current.state == 3 || current.state == 4)) || (current.active == false && current.state == 1)
01:44:44.338: Execute before business rules on u_migration_request:MGR0000027 after engines (order >=1000)
01:44:44.338: ==> 'metrics events' on u_migration_request:MGR0000027
01:44:44.339: <== 'metrics events' on u_migration_request:MGR0000027
01:44:44.339: === Skipping 'Set Closure Fields' on u_migration_request:MGR0000027; condition not satisfied: current.active.changesTo(false)
01:44:44.354: Execute after business rules on u_migration_request:MGR0000027 before engines (order <1000)
01:44:44.355: Execute before business rules on sys_trigger:ASYNC: Affected group notifications before engines (order <1000)
01:44:44.355: ==> 'Ensure Valid Schedule' on sys_trigger:ASYNC: Affected group notifications
01:44:44.355: <== 'Ensure Valid Schedule' on sys_trigger:ASYNC: Affected group notifications
01:44:44.358: Execute after business rules on sys_trigger:ASYNC: Affected group notifications before engines (order <1000)
01:44:44.358: === Skipping 'Propagate run all nodes changes' on sys_trigger:ASYNC: Affected group notifications; condition not satisfied: current.system_id == "ALL NODES" || (previous != null && previous.system_id == "ALL NODES")
01:44:44.359: Execute before business rules on sys_trigger:ASYNC: Affected location notifications before engines (order <1000)
01:44:44.359: ==> 'Ensure Valid Schedule' on sys_trigger:ASYNC: Affected location notifications
01:44:44.359: <== 'Ensure Valid Schedule' on sys_trigger:ASYNC: Affected location notifications
01:44:44.360: Execute after business rules on sys_trigger:ASYNC: Affected location notifications before engines (order <1000)
01:44:44.360: === Skipping 'Propagate run all nodes changes' on sys_trigger:ASYNC: Affected location notifications; condition not satisfied: current.system_id == "ALL NODES" || (previous != null && previous.system_id == "ALL NODES")
01:44:44.361: === Skipping 'Cancel Workflows Upon Cancellation' on u_migration_request:MGR0000027; condition not satisfied: (current.getTableName() != 'sysapproval_group') && (current.getTableName() != 'rm_story') && (current.state.changesTo(4))
01:44:44.361: === Skipping 'change events' on u_migration_request:MGR0000027; condition not satisfied: current.number.startswith('CHG')
01:44:44.361: === Skipping 'Expected migration date changed' on u_migration_request:MGR0000027; condition not satisfied: current.u_expected_migration_date.changes() && !previous.u_expected_migration_date.nil()
01:44:44.361: === Skipping 'Moot Approvals Upon Cancellation' on u_migration_request:MGR0000027; condition not satisfied: current.getTableName() != 'sysapproval_group' && current.state.changesTo(4)
01:44:44.361: === Skipping 'Start Change Tasks' on u_migration_request:MGR0000027; condition not satisfied: current.approval.changesTo('approved') && !current.delivery_plan.nil()
01:44:44.361: === Skipping 'System Deploy Approved' on u_migration_request:MGR0000027; condition not satisfied: current.category=="Service-now Deployment"&& current.approval=="approved"&& current.state=="2"
01:44:44.361: === Skipping 'task assigned' on u_migration_request:MGR0000027; condition not satisfied: !current.assigned_to.nil() && current.assigned_to.changes()
01:44:44.361: ==> 'task events' on u_migration_request:MGR0000027
01:44:44.361: <== 'task events' on u_migration_request:MGR0000027
01:44:44.361: === Skipping 'Task Time Worked' on u_migration_request:MGR0000027; condition not satisfied: current.time_worked.changes()
01:44:44.362: Execute before business rules on sys_trigger:ASYNC: Process SLAs before engines (order <1000)
01:44:44.362: ==> 'Ensure Valid Schedule' on sys_trigger:ASYNC: Process SLAs
01:44:44.362: <== 'Ensure Valid Schedule' on sys_trigger:ASYNC: Process SLAs
01:44:44.363: Execute after business rules on sys_trigger:ASYNC: Process SLAs before engines (order <1000)
01:44:44.363: === Skipping 'Propagate run all nodes changes' on sys_trigger:ASYNC: Process SLAs; condition not satisfied: current.system_id == "ALL NODES" || (previous != null && previous.system_id == "ALL NODES")
01:44:44.364: ==> 'Run SLAs' on u_migration_request:MGR0000027
01:44:44.372: <== 'Run SLAs' on u_migration_request:MGR0000027
01:44:44.372: ==> 'task survey events' on u_migration_request:MGR0000027
01:44:44.375: <== 'task survey events' on u_migration_request:MGR0000027
01:44:44.375: === Skipping 'SNC - Run parent workflows' on u_migration_request:MGR0000027; condition not satisfied: current.state.changesTo(3) || current.state.changesTo(4) || current.state.changesTo(7) || current.operation() == 'delete'
01:44:44.375: ==> 'SNC - ITIL - Close Related' on u_migration_request:MGR0000027
01:44:44.376: <== 'SNC - ITIL - Close Related' on u_migration_request:MGR0000027
01:44:44.382: Execute after business rules on u_migration_request:MGR0000027 after engines (order >=1000)
01:44:44.382: ==> 'Workflow Release Lock' on u_migration_request:MGR0000027
01:44:44.383: <== 'Workflow Release Lock' on u_migration_request:MGR0000027
01:44:44.383: <== 'Approval Events (Task)' on sysapproval_approver:Solution Owner
01:44:44.383: : 5 - gr.sys_id : 0cc213782b590100bb50525ee8da1520
01:44:44.383: : current.approval:requested
01:44:44.431: >>> Preceding lines from previous transaction

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2020 10:51 AM
FYI, I had to change gr.addQuery('document_id', current.sys_id); to gr.addQuery('document_id', current.document_id); (not to mention changing gs.print to gs.addinfoMessage) to get this to work. But this was VERY helpful, thanks!