We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Create Delete action button in Service Portal for deleting record

mahfooz1
Tera Contributor

Create Delete action button in Service Portal for deleting record

1 ACCEPTED SOLUTION

mr18
Tera Guru

Create a widget as below

HTML:

<div class="panel b ng-scope">
    <div ng-if="data.states != 4"> <!--4 is the value of cancel -->
        <div class="panel-heading bg-primary panel-la-jolla-default">Actions</div>
        <div class="panel-body" >
          <button type="button" class="btn btn-danger btn-block" ng-click="c.uiAction('delete')">Delete</button>
        </div>
      </div>
</div>


CSS:

.header {
 background-color: #2b0c99 
}

 

Client Script:

function() {
 var c = this;
    c.uiAction = function(action) {

        c.data.action = action;
 if (c.data.action == 'delete') {
var ans = confirm("Are you sure, you want to delete it?");
      if (ans == true)
	update();
		else
		return false;
            }

 function update() {
            c.server.update().then(function() {
                c.data.action = undefined;
                c.data.result = "completed";
            });
            window.setTimeout(restartscreen, 2000);

            function restartscreen() {
                location.reload(true);
            }
        }
 };
}

 

Server Script:

(function() {
    var gr = $sp.getRecord();

    // Get table & sys_id
    data.table = input.table || $sp.getParameter("table");
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");

    data.canRead = gr.canRead();
    if (!data.canRead)
        return;

    data.canWrite = gr.canWrite();
    if (!data.canWrite)
        return;

    var states = "";
    var b = $sp.getField(gr, 'state');
    if (b != null)
        states = b.value;
	
	var stages = "";
	var t = $sp.getField(gr, 'stage');
	if(t != null)
		stages = t.value;
	
	var ReqCancel = "";
	var c = $sp.getField(gr, 'u_asked_cancellation');
	if(c != null)
	ReqCancel = c.value;	


    // Valid GlideRecord
    gr = new GlideRecord(data.table);
    if (!gr.isValid())
        return;

    // Valid sys_id
    if (!gr.get(data.sys_id))
        return;

if (input && input.action) {
        var action = input.action;
 if (action == 'delete')
gr.deleteRecord();
}

View solution in original post

6 REPLIES 6

mr18
Tera Guru

Create a widget as below

HTML:

<div class="panel b ng-scope">
    <div ng-if="data.states != 4"> <!--4 is the value of cancel -->
        <div class="panel-heading bg-primary panel-la-jolla-default">Actions</div>
        <div class="panel-body" >
          <button type="button" class="btn btn-danger btn-block" ng-click="c.uiAction('delete')">Delete</button>
        </div>
      </div>
</div>


CSS:

.header {
 background-color: #2b0c99 
}

 

Client Script:

function() {
 var c = this;
    c.uiAction = function(action) {

        c.data.action = action;
 if (c.data.action == 'delete') {
var ans = confirm("Are you sure, you want to delete it?");
      if (ans == true)
	update();
		else
		return false;
            }

 function update() {
            c.server.update().then(function() {
                c.data.action = undefined;
                c.data.result = "completed";
            });
            window.setTimeout(restartscreen, 2000);

            function restartscreen() {
                location.reload(true);
            }
        }
 };
}

 

Server Script:

(function() {
    var gr = $sp.getRecord();

    // Get table & sys_id
    data.table = input.table || $sp.getParameter("table");
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");

    data.canRead = gr.canRead();
    if (!data.canRead)
        return;

    data.canWrite = gr.canWrite();
    if (!data.canWrite)
        return;

    var states = "";
    var b = $sp.getField(gr, 'state');
    if (b != null)
        states = b.value;
	
	var stages = "";
	var t = $sp.getField(gr, 'stage');
	if(t != null)
		stages = t.value;
	
	var ReqCancel = "";
	var c = $sp.getField(gr, 'u_asked_cancellation');
	if(c != null)
	ReqCancel = c.value;	


    // Valid GlideRecord
    gr = new GlideRecord(data.table);
    if (!gr.isValid())
        return;

    // Valid sys_id
    if (!gr.get(data.sys_id))
        return;

if (input && input.action) {
        var action = input.action;
 if (action == 'delete')
gr.deleteRecord();
}

In the server script after last line add this too

})();