Create cancel button in demand form

sakila
Tera Contributor

Given: Demand record is created.

When: Demand team accesses the demand record

Then: Demand team should be able to mark Demand as cancel using Cancel Ul action button

AND

A pop-up should appear with Justification text box as manadatory field

AND

the pop-up content should be updated in the worknotes

Notes for Developers: Create Cancel Ul action button to cancel the Demand, on-clicck of which create one pop-up under which create

follwing fields:

1. Justification(mandatory field): A text field

2. Create 2 button as:

a. Submit

b. Close any one pls help 

 

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @sakila  - There are a number of things I'd do differently here, but you can improve your solution by moving your cancelDemand function into the UI action (in which case you don't need the script include at all).

 

UI Page [Name = cancel_demand]

HTML

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:ui_form>
        <p>Please add comments to close the demand:</p>
        <textarea id="justification_notes" rows="5" cols="50"></textarea>
        <div style="text-align: right; margin-top: 10px;">
            <g:dialog_buttons_ok_cancel ok_style_class="btn btn-primary" ok_text="${gs.getMessage('Submit')}" ok_type="button" cancel_type="button" />
        </div>
    </g:ui_form>
</j:jelly>

Client script

function actionOK() {
	g_form.checkMandatory = false; // Ignore mandatory fields
	g_form.setValue('work_notes', gel('justification_notes').value.trim());
	gsftSubmit(null, g_form.getFormElement(), 'cancel_demand');
}

 

UI Action [Action name = cancel_demand]

Script

function showModal(){
	modal = new GlideModal('cancel_demand');
	modal.setTitle('Confirm Action');
	modal.setWidth('400');
	modal.render();
}

if (typeof window == "undefined") {
	cancelDemand();
}

function cancelDemand() {
	var table = current.getTableName();
	var cdv = current.getClassDisplayValue();
	current.state = '11'; // Cancelled
	gs.addInfoMessage(cdv + ' <a href="/' + table + '.do?sys_id=' + current.sys_id + '">' + current.number + '</a> has been cancelled.');
	current.update();
	action.setRedirectURL(current);
}

 

You’ll notice I’ve set the state to 11 rather than 7. By default, 7 corresponds to "Rejected" and you may want to distinguish between "Cancelled" and "Rejected". Keep in mind that state 7 is mapped to a demand stage in dmn_stage_config, and in the flow formatter it will appear as "Deferred" unless you update the records in sys_process_flow, so the choice matters. In my view, distinguishing "Cancelled" from "Rejected" is worth the small extra effort to keep the meanings clear.

 

Additionally, you'll want to set the appropriate conditions on the UI action. For example:

!current.isNewRecord() && gs.hasRole(DemandUtil.getDemandRole('manager',current.getTableName())) && (current.state != 9 && current.state != 11) && current.project.nil()

 

Finally, don't forget to update the UI Page so the justification field is mandatory per your requirements.

View solution in original post

5 REPLIES 5

sakila
Tera Contributor

any one help here . i written code ui action 

function cancelDemand() {
    var dialogBox = new GlideDialogWindow('close_demand');
    dialogBox.setTitle('Close Comments');
    dialogBox.setPreference('sys_id', g_form.getUniqueValue());
    dialogBox.render();
       
}
and ui page html part 
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:ui_form>
        <p>Please add comments to close the demand:</p>

        <textarea id="justification_notes" rows="5" cols="50"></textarea>

        <div style="text-align: right; margin-top: 10px;">
            <g:dialog_buttons_ok_cancel ok="return closeDemand()" cancel="return closeDialog()" />
        </div>
    </g:ui_form>
</j:jelly>
and client script part 
function closeDemand() {

    var getComments = gel('justification_notes').value;
   var getDemandID = g_form.getUniqueValue();
 

    // alert('commnets ' + getComments);
    // alert('value ' + getDemandID);

    var gr = new GlideAjax('CancelDemandProcessor');
    gr.addParam('sysparm_name', 'cancelDemand');
    gr.addParam('sysparm_comments', getComments);
    gr.addParam('sysparm_demandID', getDemandID);
    gr.getXMLAnswer(responseValue);
    GlideDialogWindow.get().destroy();
    // action.setRedirectURL(current);
    return true;
}

function responseValue(response) {
    if (response) {        
        //g_form.addInfoMessage('Demand has been cancelled successfully');
        //action.setRedirectURL(current);
    }
}
//destroy the box on cancel
function closeDialog() {
    GlideDialogWindow.get().destroy();
    return false;

}
script inlcude : 
var CancelDemandProcessor = Class.create();
CancelDemandProcessor.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    cancelDemand: function() {

        try {
            gs.info('canceldemand');

            var comments = this.getParameter('sysparm_comments');
            var demandID = this.getParameter('sysparm_demandID');

            gs.info('canceldemand comments' + comments);
            gs.info('canceldemand demandID' + demandID);

            var demand = new GlideRecord('dmn_demand');
            if (demand.get(demandID)) {
                demand.state = 7;
                demand.work_notes = comments;
                demand.update();
                //action.setRedirect('/dmn_demand.do?sys_id='+ demandID +'&sysparm_view=default');

                // return true;
                return 'success';
            } else {
                //return false;
                return 'not_found';
            }
        } catch (ex) {
            gs.info('error From cancelDemand ' + ex);
            return 'error';//added newly
        }
    },



    type: 'CancelDemandProcessor'
}); what is happening if i click cancel demand after one window open i given Justification m after click okay its going another page this is issue 

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @sakila  - There are a number of things I'd do differently here, but you can improve your solution by moving your cancelDemand function into the UI action (in which case you don't need the script include at all).

 

UI Page [Name = cancel_demand]

HTML

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:ui_form>
        <p>Please add comments to close the demand:</p>
        <textarea id="justification_notes" rows="5" cols="50"></textarea>
        <div style="text-align: right; margin-top: 10px;">
            <g:dialog_buttons_ok_cancel ok_style_class="btn btn-primary" ok_text="${gs.getMessage('Submit')}" ok_type="button" cancel_type="button" />
        </div>
    </g:ui_form>
</j:jelly>

Client script

function actionOK() {
	g_form.checkMandatory = false; // Ignore mandatory fields
	g_form.setValue('work_notes', gel('justification_notes').value.trim());
	gsftSubmit(null, g_form.getFormElement(), 'cancel_demand');
}

 

UI Action [Action name = cancel_demand]

Script

function showModal(){
	modal = new GlideModal('cancel_demand');
	modal.setTitle('Confirm Action');
	modal.setWidth('400');
	modal.render();
}

if (typeof window == "undefined") {
	cancelDemand();
}

function cancelDemand() {
	var table = current.getTableName();
	var cdv = current.getClassDisplayValue();
	current.state = '11'; // Cancelled
	gs.addInfoMessage(cdv + ' <a href="/' + table + '.do?sys_id=' + current.sys_id + '">' + current.number + '</a> has been cancelled.');
	current.update();
	action.setRedirectURL(current);
}

 

You’ll notice I’ve set the state to 11 rather than 7. By default, 7 corresponds to "Rejected" and you may want to distinguish between "Cancelled" and "Rejected". Keep in mind that state 7 is mapped to a demand stage in dmn_stage_config, and in the flow formatter it will appear as "Deferred" unless you update the records in sys_process_flow, so the choice matters. In my view, distinguishing "Cancelled" from "Rejected" is worth the small extra effort to keep the meanings clear.

 

Additionally, you'll want to set the appropriate conditions on the UI action. For example:

!current.isNewRecord() && gs.hasRole(DemandUtil.getDemandRole('manager',current.getTableName())) && (current.state != 9 && current.state != 11) && current.project.nil()

 

Finally, don't forget to update the UI Page so the justification field is mandatory per your requirements.

I will try and let you know. Thanks for reply 

instead of am trying this one just i tried 

function Demand() {
//   if (confirm('Are you sure you want to cancel this record?'))
{
    var reason = prompt('Please enter a cancellation reason:');
    if (reason && reason.trim() !== '') {
      g_form.setValue('work_notes', reason.trim()); // Set Work Notes
      g_form.setValue('state', '20'); // Set to Cancelled (or appropriate state)
      g_form.save(); // Save the form and stay on the same page
    } else {
      alert('A cancellation reason is required.');
    }
  }
} can u suggestion me here , when ever creating new demand i dont want to see cancel demand button after save or submit only i need to see
 
in that ui action condition : current.state !='20'