how to add incident to incident list from widget

akshay parekar1
Tera Contributor

Hello, last step is remaining for my one portal task, please help to achieve this

Earlier i have created a widget on my portal which is showing list of opened incidents by current logged in user.

Also have added one 'NEW' button which will redirect to another widget. This second widget has User ID and short description fields. User Id is getting populated with current user's user name and short description i will put something.There is 'Submit' button also in second widget . when this  2 fields are filled and i click on submit button , this record will get updated in incident list as this incident is opened by current logged in user. As soon as i click on submit button of second widget , i must be able to see this record added to list of my first widget also.

Please tell me where i have to make changes

First widget (getting list of opened incidents by current user)-

Server script-

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
    
data.incident=[];
    
    var gr= new GlideRecord('incident');
    gr.addQuery('opened_by',gs.getUserID());
    gr.query();
    while(gr.next()){
        var incident_data={};
        incident_data.number= gr.getValue('number');
        incident_data.short_description= gr.getValue('short_description');
        incident_data.priority= gr.getDisplayValue('priority');
        incident_data.state= gr.getDisplayValue('state');
        
        data.incident.push(incident_data);
    }
})();

HTML Template-

<div>
<!-- your widget template -->
  <table border ="1.5 px">
    
    <div style="text-align:right;">
<button onClick="location.href='?id=add_incident_1'">New</button>
</div>
    
    <tr> <th style="width:120px";> Incident Number </th>
      <th style="width:120px";> Short Description </th>
      <th style="width:120px";> Priority </th>
      <th style="width:120px";> State </th>
    </tr>
    
    <tr ng-repeat ="incident_data in data.incident" > <td>{{incident_data.number}} </td>
      <td>{{incident_data.short_description}} </td>
      <td>{{incident_data.priority}} </td>
      <td>{{incident_data.state}} </td>
    </tr>
  </table>
</div>

Second widget( to add record in incident table)-

server script-

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
    data.loggedinuser=gs.getUserDisplayName();
})();

Html-

<div>
<!-- your widget template -->
<h1>
  Please fill the form:
  <br> <br>   
  User ID: 
<input type="text" ng-model="data.loggedinuser" size="50">
<br> <br>  
Short
Description:
<input type="text"  size="100"> 
   <br> <br>   
   <div style="text-align:left;">
<button onClick="location.href='?id=task_page'">Submit</button>
</div>
  </h1>
</div>

1 ACCEPTED SOLUTION

@akshay parekar sure mark this answer correct if it helped you and close the thread so that it helps other users with same query 

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@akshay parekar 

Would you mind closing your earlier question by marking appropriate response as correct?

How to populate duration type of field's value on form

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

yes i will close it right now marking it correct

akshay parekar1
Tera Contributor

I have closed it, please help me to solve this  one

Mohith Devatte
Tera Sage
Tera Sage

hello @akshay parekar ,

so you can do this using an ng-click function and then call a client function and then call server from client to insert an incident in the incident table which will automatically show the record in incident table 

<div>
<!-- your widget template -->
<h1>
  Please fill the form:
  <br> <br>   
  User ID: 
<input type="text" ng-model="data.loggedinuser"  size="50">
<br> <br>  
Short
Description:
<input type="text"  size="100" ng-model="entereddescription"> 
   <br> <br>   
   <div style="text-align:left;" >
<button ng-click="c.createIncident(entereddescription)">Submit</button>
</div>
  </h1>
</div>

Client controller:

api.controller=function() {
var c = this;
c.createIncident=function(desc)
{
c.data.description=desc;
c.server.update();
window.location.href='?id=task_page';
}
};

Server script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
    data.loggedinuser=gs.getUserDisplayName();
If(input.description)
{
 var gr= new GlideRecord('incident');
gr.initialize();
g.caller_id=gs.getUserID();
gr.priority="your_priority_value";
gr.short_description= input.description;
gr.state="your_state_value";
gr.insert();
}
})();

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU