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

Hi Vaibhav,



It cannot see that you set the c.data.state value anywhere in the server side, therefore it will not be available in the client either. You need to set the field data in the server so it is available in the client.


For example, if in the server you set


data.myVar = "123";


in the client/HTML, you can access c.data.myVar and will contain the value "123".


In your case, you just need to add this line after getting the GlideRecord, but outside the "if (input ...)" checking


data.state = gr.getValue('state');



I mean, your server code should look like


(function() {


...


// Valid sys_id


  if (!gr.get(data.sys_id))


  return;



data.state = gr.getValue('state');



if (input && input.action) {


...


  }


})();


Thanks Gines!! It was helpful...


ashi7
Kilo Contributor

hi,



Can anyone please help me. when i am entering some comments also the error message is coming.