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

@gauravagarw 

Thanks for your reply , but since i am not good at coding, can you please write me the exacte script ?

 

gauravagarw
Tera Expert

sure i can help you with that most of the code looks fine did you try to console.log whether the sys id is coming or not?

before gr.update() add gr.setWorkFlow(false); if that works please mark my answer as helpful

@gauravagarw 

Hello, still not working : 

yasserbouat_0-1742312196444.pngyasserbouat_1-1742312221494.pngyasserbouat_2-1742312252101.png

 

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.