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



Gines is right. There is just one thing to consider..



In order to have exactly what Gines suggested you need to clone the OOB widget and work on the copy.


This can be absolutely a good solution to your problem.



Otherwise you can rework the copy of the original widget to show the new button (Reopen) and also the old one.



Anyway i would suggest to careful evaluate the implications...


Custom widgets are absolutely allowed but after the creation you're the owner of the new one....



Cheers


Hi, r0b0_d3vil & ginesdoleratormo



Thanks.!! Will give it a try and post my code if it works.



Regards,
Vaibhav Desai


OOB widget "Ticket Conversations" is not accessible but there is clone widget button.. So, if i clone this.. I have to add this new cloned widget on my Form Right?



By broadcasting comments I will be able to access the content.. But,, How to make field mandatory?? I am still not able to figure out a way to do that..


Can you share a sample code for that..



Regards,
Vaibhav Desai


Hi Vaibhav,



Yes, add the cloned widget to your form and include the modifications to the cloned one.



Once you can access the content of the comments field, you can make them mandatory by showing an error message if the field has not been populated. Following your code, what you can do is:


var comments = input.comments;


if(comments != null && comments != ""){


    gr.setValue('comments',comments);


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


    gr.update();


  }


  else{


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


  }


Hello ginesdoleratormo,



I added one text area over the "reopen incident" and did my work over it..



I have one more question.. When incident state is resolved is only then my widget should be visible.. I added condition in my starting <div> tag.



<div class="panel panel-default" ng-if="c.data.state==6">



It should work right??? as i already have below function in my client script.. which will automatically fetch data from server script..



client script


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


function($scope) {


  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 = input.reason;



  gs.addInfoMessage('comments '+comments);


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


  if (action == 'reopen') {


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


  if(comments == "" || comments == undefined){


  gs.addErrorMessage('Adding information in Reopen Reason is mandatory.');


  return false;


  }


  else{


  gr.comments = comments;


  //setValue('comments',comments);


  gr.state = -1;


  //setValue('state', -1);


  gr.update();


  }


  }


  }


  } else {


// gs.addErrorMessage('Incident canbe Reopened only from Resolved State.');


}


  }



})();


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




I know i am missing something really stupid here..But, quite new to service portal..


Once again thanks for your help!!



Regards,


Vaibhav Desai