The CreatorCon Call for Content is officially open! Get started here.

ServiceNow Widget to Approve/Reject

F_bio Santos
Kilo Sage

Hi everyone, Im trying to add two buttons on a portal widget. One that will approve the record and make the state "Closed Complete" and other that will reject and make the state "work in progress" it may be for the "incident" table or for the "sc_req_item". Im not being able to make it work, when I click on the buttons nothing happens. I believe that is something on the "Client Script" but Im not sure ... can someone help with this ?

Code:

HTML:

     <li>
						<a href="javascript&colon;void(0)" ng-click="approve()">Approve</a>
						<a href="javascript&colon;void(0)" ng-click="reject()">Reject</a>
         </li>


Client Script:

	$scope.approve = function() {
		console.log($scope.data.approve);
			$scope.server.update();
					c.server.update().then(function(){
					$scope.data.action = 'approve';	
				//$scope.update();
		})
		}

	$scope.reject = function() {
		console.log($scope.data.reject);
			$scope.server.update();
					c.server.update().then(function(){
					$scope.data.action = 'reject';	
				//$scope.update();
		})
		}


Server Script:

  data.sys_id = $sp.getParameter('sys_id');
  data.tableName = $sp.getParameter('table');		

    if (input && input.action){
		var action = input.action;
		if (action == 'approve' || data.tableName == 'sc_req_item'){
				REQgr.setValue('state', '3');
				REQgr.update();
			} else if(action == 'reject' || data.tableName == 'sc_req_item'){
				REQgr.setValue('state', '2');
				REQgr.update();
			} else if(action == 'approve' || data.tableName == 'incident'){
				INCgr.setValue('state', '6');
				INCgr.update();
			} else if(action == 'reject' || data.tableName == 'incident'){
				INCgr.setValue('state', '2');
				INCgr.update();
		  }

 

2 REPLIES 2

Sid_Takali
Kilo Patron

Hi @F_bio Santos Try below Scripts

Client Script :

$scope.approve = function() {
    console.log("Approving record with sys_id: " + $scope.data.sys_id);
    $scope.data.action = 'approve';
    $scope.server.update(); 
};

$scope.reject = function() {
    console.log("Rejecting record with sys_id: " + $scope.data.sys_id);
    $scope.data.action = 'reject';
    $scope.server.update(); 
};

Server Script : 

(function() {
    var sys_id = $sp.getParameter('sys_id');
    var tableName = $sp.getParameter('table');    

    var gr;
    if (tableName === 'sc_req_item') {
        gr = new GlideRecord('sc_req_item');
    } else if (tableName === 'incident') {
        gr = new GlideRecord('incident');
    }

    if (gr) {
        if (gr.get(sys_id)) {
            if (input.action === 'approve') {
                if (tableName === 'sc_req_item') {
                    gr.setValue('state', '3'); // Closed Complete for sc_req_item
                } else if (tableName === 'incident') {
                    gr.setValue('state', '6'); // Closed Complete for incident
                }
            } else if (input.action === 'reject') {
                if (tableName === 'sc_req_item') {
                    gr.setValue('state', '2'); // Work in Progress for sc_req_item
                } else if (tableName === 'incident') {
                    gr.setValue('state', '2'); // Work in Progress for incident
                }
            }
            gr.update();
        }
    }
})();

Hi @Sid_Takali I changed it, but it didnt work, anyway it helped me understand what I was doing wrong ... and now its working