how to update the field in sysapproval_approver table based in change state in change record.

narenreddy
Giga Guru

Hi All,

  I have a requirement that I have two phases of approvals in change management, one is for build and another one is for implementation. I have crated one   field that us Approvals type in sysapproval_approver table so When user requested for build approvals then approval type field should be updated as build in approvals related list and when user requested for implementation approvals then the approval type should be implementation in approval related list. Could you please help me to achieve this.

9 REPLIES 9

Hi Sumeet,



  I am new to the scripting, so could you validate below script and correct me where I am wrong.



function onAfter() {


    //This function will be automatically called when this rule is processed.


    var id = current.sysapproval.nil() ? current.document_id : current.getValue('sysapproval');


    var table = current.source_table.nil() ? 'task' : current.source_table;


    if (id != null && table != null ) {


          var gr = new GlideRecord(table);


          gr.get(id);


          var st = gr.getValue('state');                                         // Change state


          if (st==10) {                                                                                     // 10 - Pending for build approvals


            current.u_string_1='build';


            current.update();


          }


    }


}


Create a Before Insert BR on Sys_approval


var chg = new GlideRecord('change_request');


chg.addQuery('sys_id', current.sysapproval);


chg.query();


if(chg.next()){


current.<field name of approval table> = chg.<field name of change table>;


}


Hi Amlesh,



  The approval type should be changes based on change state. When the change state is 10(pending build approvals) then the approval type filed should be build in sysapproval_approver table. When the change state is 5(pending implementation approvals) then the approval type filed should be implementation in sysapproval_approver table.


I have tried below script but its not working.


function onAfter(current, previous) {


var cr = new GlideRecord ('change_request');


cr.addQuery('sys_id', current.document_id);


cr.query();


while(cr.next()){


  if (cr.state==10){


  current.u_string_1='build';


  current.update();          


  }


}


}


Did I miss any thing ?


Hi Narender,



I've got something in our system that sets an approval type field ('u_approval_type' in my case).



This modified section of my business rule might work for you. You'll need to replace 'u_approval_type' with the name of your field.



If you set it to run before insert on the sysapproval_approver table the following might work:



------------------------------------------------------------



//get the current reference number its the first three characters as variables


var referenceNumber = current.sysapproval.number;


var refNumberThreeChars = referenceNumber.substring(0,3);



/**************************/


/*****CHANGE APPROVALS*****/


/**************************/


/*


Change states:


10 = Pending Build Approval


5 = Pending Implementation Approval


*/


if (refNumberThreeChars == 'CHG') {


  //get the current state of the change


  var stateValue = current.sysapproval.state;


  if (stateValue == 10){


  current.u_approval_type = 'Build Approval';


  } else if (stateValue == 5){


  current.u_approval_type = 'Implementation Approval';


  }


}



----------------------------------------------------------------