Rollback state using UI action

anup4253
Kilo Contributor

Hi everyone,

I need to create a UI action which changes my state backward on my   approval form.

eg. if the state is Authorize it should change it to New.

How to achieve it?

please describe!!

1 ACCEPTED SOLUTION

Hi Anup,



I could write down a UI action code for you which on click would satisfy your requirements (I had the code tested fully, works fine):



Since the approver will initiate the process so we need to make the Button on Approval [sysapproval_approver] table.


refer.PNG


In the "Condition" field you can give your own condition according to convenience.


I have done this for a change request approval.


The code below :



  1. function referBack(){
  2.   if (g_form.getValue('comments') == '') {
  3.   // Remove any existing field message, set comments mandatory, and show a new field message
  4.   try {
  5.   g_form.hideFieldMsg('comments');
  6.   } catch(e) {}
  7.   g_form.setMandatory('comments', true);
  8.   g_form.showFieldMsg('comments', getMessage('Please enter a comment why would you like to refer back'), 'error'); // If you want the approver to describe the changes he needs
  9.   return false;   // Abort submission
  10.   }
  11.   gsftSubmit(null, g_form.getFormElement(), 'refer_back'); // MUST call the 'Action name' set in this UI Action
  12. }
  13. // Code that runs without 'onclick'
  14. if (typeof window == 'undefined')
  15.   serverReopen();
  16. function serverReopen() {
  17.   var chg = new GlideRecord("change_request");
  18.   chg.addQuery('sys_id' , current.getValue('sysapproval'));
  19.   chg.query();
  20.   if(chg.next())
  21.   {
  22.   chg.state = 1; //changing the state of change req back to 'Open' from 'Approval Requested'
  23.   current.state = 'not requested'; // you might want to change the state of approval in the sysapproval_approver table from "requested" to "Not requested"
  24.   }
  25.   chg.update();
  26.   current.update();
  27.   gs.addInfoMessage(gs.getMessage("Request is referred back for changes")); // user friendly msg on success
  28.   action.setRedirectURL(current);
  29. }


Copy paste this code and do your necessary changes.


Below screenshot shows error msg when approver tries to click on Refer Back button without any comments :


refer1.PNG


Below screenshot shows success msg and changes the state to not yet requested :


You can also notice that the "Refer Back" button is gone since it is already referred.(we don't need the button now. Do we?)


refer2.PNG


Happy working Anup. Please let me know your doubts if any.


Also please mark my answer correct if it fulfills your requirement


View solution in original post

12 REPLIES 12

Hi Anup,



I could write down a UI action code for you which on click would satisfy your requirements (I had the code tested fully, works fine):



Since the approver will initiate the process so we need to make the Button on Approval [sysapproval_approver] table.


refer.PNG


In the "Condition" field you can give your own condition according to convenience.


I have done this for a change request approval.


The code below :



  1. function referBack(){
  2.   if (g_form.getValue('comments') == '') {
  3.   // Remove any existing field message, set comments mandatory, and show a new field message
  4.   try {
  5.   g_form.hideFieldMsg('comments');
  6.   } catch(e) {}
  7.   g_form.setMandatory('comments', true);
  8.   g_form.showFieldMsg('comments', getMessage('Please enter a comment why would you like to refer back'), 'error'); // If you want the approver to describe the changes he needs
  9.   return false;   // Abort submission
  10.   }
  11.   gsftSubmit(null, g_form.getFormElement(), 'refer_back'); // MUST call the 'Action name' set in this UI Action
  12. }
  13. // Code that runs without 'onclick'
  14. if (typeof window == 'undefined')
  15.   serverReopen();
  16. function serverReopen() {
  17.   var chg = new GlideRecord("change_request");
  18.   chg.addQuery('sys_id' , current.getValue('sysapproval'));
  19.   chg.query();
  20.   if(chg.next())
  21.   {
  22.   chg.state = 1; //changing the state of change req back to 'Open' from 'Approval Requested'
  23.   current.state = 'not requested'; // you might want to change the state of approval in the sysapproval_approver table from "requested" to "Not requested"
  24.   }
  25.   chg.update();
  26.   current.update();
  27.   gs.addInfoMessage(gs.getMessage("Request is referred back for changes")); // user friendly msg on success
  28.   action.setRedirectURL(current);
  29. }


Copy paste this code and do your necessary changes.


Below screenshot shows error msg when approver tries to click on Refer Back button without any comments :


refer1.PNG


Below screenshot shows success msg and changes the state to not yet requested :


You can also notice that the "Refer Back" button is gone since it is already referred.(we don't need the button now. Do we?)


refer2.PNG


Happy working Anup. Please let me know your doubts if any.


Also please mark my answer correct if it fulfills your requirement


anup4253
Kilo Contributor

Thanks Arnab,



I have applied it and it is working fine.Really appreciate your help.



Thanks,


Anup


Thanks a lot for the recognition Anup !


Keep posting the community with such unique requirements.



Thanks,


Arnab