Button 'Cancel' on the portal

yasserbouat
Tera Guru

Hello,

I created a catalog item in order to let users submit their request from the portal, and I created a cancel button that will display after submitting the form and when the request is still not approved, in order to let users cancel their request : but when i click on it , the request and RITM still open ! Can anyone please fix me the scripts of the widget ? 


yasserbouat_0-1742222948540.png

 

 

yasserbouat_1-1742222960759.png


Client script : 

api.controller = function($scope, spModal) {
var c = this;

c.cancelRequest = function() {
spModal.confirm("Voulez-vous vraiment annuler cette demande ?").then(function(confirmed) {
if (confirmed) {
c.server.get({
action: 'cancelRequest',
sys_id: c.data.sys_id
}).then(function(response) {
if (response.data.success) {
$scope.$emit('sp.sc_request.retrieved', response.data.request);
spModal.alert("Demande annulée avec succès");
} else {
spModal.alert("Échec de l'annulation : " + response.data.error);
}
});
}
});
};
};


---------------
Server script : 

(function() {
if (input && input.action === 'cancelRequest') {
var gr = new GlideRecord('sc_req_item');
if (gr.get(input.sys_id)) {
gr.state = 4; // Closed Incomplete
gr.stage = "Annulé par l'utilisateur";
gr.work_notes = "Annulation effectuée via le portail le " + new GlideDateTime().getDisplayValue();

if (gr.update()) {
output = {
success: true,
request: gr.getDisplayValue()
};
} else {
output = {
success: false,
error: gs.getMessage("Échec de la mise à jour du RITM")
};
}
}
}
})();

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @yasserbouat ,

 

Here is a working solution to your problem,

 

When a request is submitted, its sys_id appears in the URL. I am fetching the sys_id in a client and pass it to the server when the button is clicked.

 

HTML

<div class="panel panel-default">
<div class="panel-heading">Cancel request</div>
  <div class="panel-body">
    <button type="submit" class="btn btn-default" ng-click="cancelRequest()">
      Cancel Request
    </button>
  </div>
</div>

 

Client Script

api.controller=function($scope, spModal) {
  /* widget controller */
  var c = this;
	
	//code update 1
	//get the sys_id of the gererated req from the URL
	var params = new URLSearchParams(window.location.search);
	var value = params.get('sys_id'); 
	
	
	$scope.cancelRequest = function() {
		
		spModal.confirm("Voulez-vous vraiment annuler cette demande ?").then(function(confirmed) {
			if (confirmed) {
				c.server.get({
				action: 'cancelRequest',
				sys_id: value
				}).then(function(response) {
					//get the response from the server stored in data.output object
					if (response.data.output.success) {
						$scope.$emit('sp.sc_request.retrieved', response.data.output.request);
						spModal.alert("Demande annulée avec succès");
						//refresh the page if the changes were made at the backend tablels
						location.reload();
					} else {
						spModal.alert("Échec de l'annulation : " + response.data.output.error);
					}
				});
			}
		});
	};
};

 

Server Script

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	
			
	    if (input && input.action === 'cancelRequest') {
				
        var gr = new GlideRecord('sc_req_item');
				gr.addQuery("request",input.sys_id);
				gr.query();
        if (gr.next()) {
						gr.setWorkFlow(false);
            gr.state = 4; // Closed Incomplete
            gr.stage = "cancelled"; // Use the correct stage value
            gr.work_notes = "Annulation effectuée via le portail le " + new GlideDateTime().getDisplayValue();

            if (gr.update()) {
                // Update the parent request state if needed
                var req = new GlideRecord('sc_request');
                if (req.get(gr.request)) {
                    req.state = 4; // Closed Incomplete
                    req.update();
                }
								//use data object at the server side for storing variable details
                data.output = {
                    success: true,
                    request: gr.getDisplayValue()
                };
            } else {
                data.output = {
                    success: false,
                    error: gs.getMessage("Échec de la mise à jour du RITM")
                };
            }
        }
    }
})();

 

Mark this helpful/ Accept the solution if this helps you.

 

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@yasserbouat 

try this

Client controller:

api.controller = function($scope, spModal) {
    var c = this;

    c.cancelRequest = function() {
        spModal.confirm("Voulez-vous vraiment annuler cette demande ?").then(function(confirmed) {
            if (confirmed) {
                c.server.get({
                    action: 'cancelRequest',
                    sys_id: c.data.sys_id
                }).then(function(response) {
                    if (response.data.success) {
                        $scope.$emit('sp.sc_request.retrieved', response.data.request);
                        spModal.alert("Demande annulée avec succès");
                    } else {
                        spModal.alert("Échec de l'annulation : " + response.data.error);
                    }
                });
            }
        });
    };
};

Server:

(function() {
    if (input && input.action === 'cancelRequest') {
        var gr = new GlideRecord('sc_req_item');
        if (gr.get(input.sys_id)) {
            gr.state = 4; // Closed Incomplete
            gr.stage = "cancelled"; // Use the correct stage value
            gr.work_notes = "Annulation effectuée via le portail le " + new GlideDateTime().getDisplayValue();

            if (gr.update()) {
                // Update the parent request state if needed
                var req = new GlideRecord('sc_request');
                if (req.get(gr.request)) {
                    req.state = 4; // Closed Incomplete
                    req.update();
                }

                output = {
                    success: true,
                    request: gr.getDisplayValue()
                };
            } else {
                output = {
                    success: false,
                    error: gs.getMessage("Échec de la mise à jour du RITM")
                };
            }
        }
    }
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @Ankur Bawiskar , 

Thanks for your reply & your effort , but I tested and still not working : I went to the REQ and then to the RITM and found it still open : 

yasserbouat_0-1742227209077.png

 

@yasserbouat 

did you add logs and see if any business rule is stopping the RITM and REQ update?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

gauravagarw
Tera Expert

in above code try using gr.setWorkFlow(false) before updating the records