Service Portal--Store the record to custom table when user submit form through portal

Mad3
Tera Expert

Hello Community!

Thanks in advance,

 

I Need to store the records into the custom table when submit the form on "Submit" button function.

I have created a form using custom widget to take the details from user to fetch same details to the custom table fields.

Using this I need to save the records to the table.

I am unable to get the details and send to the table to create a record and save.

I have written this code, I am unable to find my mistakes please correct and provide correct script.

 

HTML:

<div >
<form class="My Form">

<div class="form-group">
<label for="name">Name</label>
<sn-record-picker field="'Name'" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
</div>

<div class="form-group">
<label for="name">Approver</label>
<sn-record-picker field="'Name'" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>

</div>

<div class="form-group">
<label for="number_of_days">Number Of Days</label>
<input type="text" class="form-control" id="number_of_days" name="number_of_days">
</div>

<div class="form-group">
<label for="from_date">From Date</label>
<input type="date" class="from_date" id="from_date" name="from_date">
</div>

<div class="form-group">
<label for="to_date">To Date</label>
<input type="date" class="to_date" id="to_date" name="to_date">
</div>

<div class="form-group">
<label for="type_of_leave">Type Of Leave</label>
<select id="type_of_leave" name="type_of_leave">
<option value="none">--None--</option>
<option value="casual_leave">Casual Leave</option>
<option value="sick_leave">Sick Leave</option>
<option value="vacation_leave">vacation Leave</option>
</select>
</div>
<br><br>
<div class="form-group">
<label for="state">State</label>
<select id="state" name="state">
<option value="open">Open</option>
<option value="inprogress">In-Progress</option>
<option value="approved">Approved</option>
<option value="reject">Reject</option>
</select>
</div>
<label for="reason">Reason</label>
<textarea name="reason"></textarea> <br><br>
<label for="reject_comments">Reject Comments</label>
<textarea name="reject_comments"></textarea><br><br>

 

<button ng-click="c.submit()" class="btn btn-primary">Submit</button>

</form>
</div>

 

Client Script:

api.controller = function($scope) {
/* widget controller */
var c = this;
c.submit = function() {
// alert('form subtted successfully');
c.data.name = c.name;
c.data.approver = c.approver;
c.data.number_of_days = c.number_of_days;
c.data.from_date = c.from_date;
c.data.to_date = c.to_date;
c.data.type_of_leave = c.type_of_leave;
c.data.state = c.state;
c.data.reject_comments = c.reject_comments;
c.server.update();
};
};

 

Server Script:

 

(function($sp, input, data, options, gs) {


if (input) {
var gr = new GlideRecord('custom_table');
gr.initialize();
gr.setValue('name', input.name);
gr.setValue('approver', input.approver);
gr.setValue('number_of_days', input.number_of_days);
gr.setValue('from_date', input.from_date);
gr.setValue('to_date', input.to_date);
gr.setValue('type_of_leave', input.type_of_leave);
gr.setValue('state', input.state);
gr.setValue('reject_comments', input.reject_comments);

 

gr.insert();

}
})($sp, input, data, options, gs);

 

Custom_Table_Form_Fields:

 

Custom_Table_Form_Fields.PNG

 

2 REPLIES 2

Sai Shravan
Mega Sage

Hi @Mad3 ,

It seems like your code should work, but there are a couple of minor issues.

  1. The field names in your client script don't match the names in your HTML. For example, in your HTML, the name field has a name of "name" and in your client script, you are trying to set the value of "c.name", which is undefined. You need to update your client script to match the names in your HTML.

  2. The syntax for setting the field values in the server script is incorrect. You need to use the dot notation to set the field values. For example, instead of "gr.setValue('name', input.name)", you should use "gr.name = input.name".

Here is the updated client script:

api.controller = function($scope) {
  /* widget controller */
  var c = this;
  c.submit = function() {
    c.data.Name = c.Name;
    c.data.Approver = c.Approver;
    c.data.Number_of_Days = c.Number_of_Days;
    c.data.From_Date = c.From_Date;
    c.data.To_Date = c.To_Date;
    c.data.Type_of_Leave = c.Type_of_Leave;
    c.data.State = c.State;
    c.data.Reject_Comments = c.Reject_Comments;
    c.server.update();
  };
};

 

Here is the updated server script:

(function($sp, input, data, options, gs) {
  if (input) {
    var gr = new GlideRecord('custom_table');
    gr.initialize();
    gr.Name = input.Name;
    gr.Approver = input.Approver;
    gr.Number_of_Days = input.Number_of_Days;
    gr.From_Date = input.From_Date;
    gr.To_Date = input.To_Date;
    gr.Type_of_Leave = input.Type_of_Leave;
    gr.State = input.State;
    gr.Reject_Comments = input.Reject_Comments;
    gr.insert();
  }
})($sp, input, data, options, gs);

I hope this helps!

Regards,
Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan ,

Thanks for the reply!

 I tried with provided script , couldn't save the record in a table. As I am new to portal stuff Can you check the code what and where I am missing.