The CreatorCon Call for Content is officially open! Get started here.

Comments field mandatory in service portal

vaibhavdesai
Kilo Expert

Hello Experts,

I am quite new to service portal and trying to achieve one scenario. When Incident is in resolved state "Reopen Incident" button/widget should be visible and on click of that comments should be mandatory.

I am able to create widget and add required actions in it. But, I am not able to understand how to make comments mandatory.

Below is the code.. Help is much appreciated!!

HTML

----------------------------------------

<div class="panel panel-default">

<div class="panel-heading">Actions</div>

<div class="panel-body">

<button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction('reopen')">Reopen Incident</button>

</div>

</div>

----------------------------------------

Client Script

----------------------------------------

function() {

  var c = this;

  c.uiAction = function(action) {

  c.data.action = action;

  c.server.update().then(function() {

  c.data.action = undefined;

  })

  }

}

----------------------------------------

Server Script

----------------------------------------

(function() {

  // Get table & sys_id

  data.table = input.table || $sp.getParameter("table");

  data.sys_id = input.sys_id || $sp.getParameter("sys_id");

  // 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;

  var onBehalfOf = gr.getValue('u_on_behalf_of');

  var caller = gr.getValue('caller_id');

  var currentUser = gs.getUserID();

  //var comments = gs.getValue('comments');

//var comments = gr.getValue('comments');

  //var comments = $sp.getValue('comments');

gs.addInfoMessage('comments '+comments);

  if(data.table == 'incident'){

  if (action == 'reopen') {

  if(onBehalfOf == currentUser || caller == currentUser){

  if(comments != ""){

    gr.setValue('comments',comments);

  gr.setValue('state', -1);

  gr.update();

  }

  else{

//Making Comments mandatory code

  gs.addErrorMessage('Adding information in Additional Comments is mandatory')

  }

  }

  }

  } else {

}

  }

})();

----------------------------------------

Regards,

Vaibhav Desai

1 ACCEPTED SOLUTION

Hi Vaibhav,



From your picture, it seems you are defining the "comments" field in one widget, and trying to get its value from the "reopen incident" widget. You cannot retrieve fields from another widget directly, but you need to communicate your widgets somehow.



Take a look at How to communicate between widgets in Service Portal



A simple solution for your case would be to use the ng-change attribute in the text field of the comments field in the first widget, such that it broadcast the value. For example:


HTML:


<input type="text" ng-model="c.data.comments" ng-change="c.broadcastComment()"/>


client script:


function() {


  var c = this;


  c.broadcastComment = function(){


      $rootScope.$broadcast('comments', c.data.comments);


  }


}



Then, in the reopen incident widget, you can listen the event it is propagated when the comments change, by just adding


function() {


  var c = this;


  /* your existing code here */



  $rootScope.$on('comments', function(event,data) {


          c.data.comments = data;


  });


}



This would insert the comments coming from the other widget into the data, such that in the server side, you can get the comments value by just using


var comments = input.comments;



Hope it helps.


Ginés.


View solution in original post

12 REPLIES 12

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Vaibhav



Quick question.


Can you show me the look and feel of the page where you are trying to add this widget ?



Cheers


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Vaibhav



Ok in this situation you are using 2 different widgets and you want to make the field on another widget to be mandatory when you click the button.


Usually you can make different widgets to communicate using an event broadcast. You can move info between widget.



Please have a look to this article.


https://serviceportal.io/using-events-communicate-widgets/



Not sure if there is another way but in this case you are using an OOB widget that is not tailored to communicate or react to the new one.


if you have a look on your instance the widget's script has nothing that can work with events (receive or broadcast)



I hope this will help you..and in case it does please mark it


Hi Vaibhav,



From your picture, it seems you are defining the "comments" field in one widget, and trying to get its value from the "reopen incident" widget. You cannot retrieve fields from another widget directly, but you need to communicate your widgets somehow.



Take a look at How to communicate between widgets in Service Portal



A simple solution for your case would be to use the ng-change attribute in the text field of the comments field in the first widget, such that it broadcast the value. For example:


HTML:


<input type="text" ng-model="c.data.comments" ng-change="c.broadcastComment()"/>


client script:


function() {


  var c = this;


  c.broadcastComment = function(){


      $rootScope.$broadcast('comments', c.data.comments);


  }


}



Then, in the reopen incident widget, you can listen the event it is propagated when the comments change, by just adding


function() {


  var c = this;


  /* your existing code here */



  $rootScope.$on('comments', function(event,data) {


          c.data.comments = data;


  });


}



This would insert the comments coming from the other widget into the data, such that in the server side, you can get the comments value by just using


var comments = input.comments;



Hope it helps.


Ginés.