spUtil error message not working

cfrazer
Giga Contributor

Hi guys,

I am working with the Service Catalog and I have added a text box for users on the portal to add rejection comments. I want these comments to be mandatory, however, when I try to add the error message for the rejection comments it does not recognize it and the user is able to reject the order without comments. How can I get the error message to pop-up?

Here is my code:

Body HTML template

<textarea ng-model="c.data.comment" style="color: grey; width: 100%; margin-top: .5em;" placeholder="Rejection Comments" class="form-control" rows="5"></textarea>

Client Controller

function ($scope, $window, spUIActionsExecuter, spUtil) {
	var c = this;
c.action = function(state) {

         if ((c.data.comment ==   undefined || c.data.comment ==   '' )&& state == 'rejected')
						{

                            spUtil.addErrorMessage("Rejection comments cannot be empty.");
return false;

         }

         c.data.op = state;

         c.data.state = state;

         c.server.update();


};

Thank you!

1 ACCEPTED SOLUTION

Rahul Jain11
Kilo Guru

Change your  ng-click="c.action('approved')" to ng-click="action('approved')", do same fro reject.

Hope this time it helps !!!

Thanks

 

View solution in original post

7 REPLIES 7

Sure, here is all of my html:

<div ng-if="c.data.isValid" class="panel panel-{{::c.options.color}} b">
  <div class="panel-heading">
    <h4 class="panel-title" ng-if="c.data.isMine && (c.data.state == 'requested')">${This {{c.data.label}} requires your approval}</h4>
    <h4 class="panel-title" ng-if="!c.data.isMine && (c.data.state == 'requested')">${This {{c.data.label}} requires approval <span ng-if="c.data.approver"> by {{c.data.approver}}}</span></h4>
    <h4 class="panel-title" ng-if="c.data.state == 'approved'">${Approved} <sn-time-ago timestamp="::c.data.sys_updated_on" /></h4>
    <h4 class="panel-title" ng-if="c.data.state == 'rejected'">${Rejected} <sn-time-ago timestamp="::c.data.sys_updated_on" /></h4>
  </div>  
  <div class="panel-body">
    <form ng-submit="$event.preventDefault()" class="form-horizontal">
      <div ng-if="c.data.fields.length > 0">
        <div ng-repeat="field in c.data.fields" class="m-b-xs" ng-if="field.value">
          <label class="m-n">{{field.label}}</label>
          <span ng-switch="field.type">
            <div ng-switch-when="glide_date_time" title="{{field.display_value}}"><sn-time-ago timestamp="::field.value" /></div>
            <div ng-switch-default >{{field.display_value}}</div>
          </span>
        </div>
      </div>
      <div ng-if="c.data.isMine && (c.data.state == 'requested')" class="question">
        <button type="button" name="approve" class="btn btn-success btn-question" ng-click="c.action('approved')">${Approve}</button>
        <div class="spacer"></div>
        <button type="button" name="reject" class="btn btn-default btn-question" ng-click="c.action('rejected')">${Reject}</button>
      <textarea ng-model="c.data.comment" style="color: grey; width: 100%; margin-top: .5em;" placeholder="Rejection Comments" class="form-control" rows="5"></textarea>
      </div>
    </form>
  </div>  
</div>

 

Also, here is my entire client controller if you need it:

function ($scope, $window, spUIActionsExecuter, spUtil) {
 var c = this;
$scope.action = function(state) {
 if ((c.data.comment == undefined || c.data.comment == '' )&& state == 'rejected') {
 spUtil.addErrorMessage("Rejection comments cannot be empty.");
 return false;
 }
 c.data.op = state;
 c.data.state = state;
 c.server.update();
 };
	
	var ESIGNATURE = {
		"approved": "cbfe2911472678680cx13a5554ee4904d",
		"rejected": "580f7111472678680cx13a5554ee4904b"
	};
	
	spUtil.recordWatch($scope, "sysapproval_approver", "state=requested^sys_id="+ c.data.sys_id);


	c.action = function(state) {
		if(c.data.esignature.e_sig_required) {
			var requestParams = {
				username: c.data.esignature.username,
				userSysId: c.data.esignature.userSysId
			};
			spUIActionsExecuter.executeFormAction(ESIGNATURE[state], "sysapproval_approver" , c.data.sys_id, [] , "", requestParams).then(function(response) {
				});
		} else {
				c.data.op = state;
				c.data.state = state;
				c.server.update();
		}
	}
}

 

I'm newer to ServiceNow/coding, how do I add the alert in the if block to see if control is passing if block or not?? Thanks!

Rahul Jain11
Kilo Guru

Change your  ng-click="c.action('approved')" to ng-click="action('approved')", do same fro reject.

Hope this time it helps !!!

Thanks

 

That fixed it! You are awesome, thank you!!!