Can I use GlideRecord in a Service Portal client script

stryker129
Mega Guru

This is a part of this question:

So I have a custom form in a widget (Service Portal page).

By submitting I'd like to store all the data to my table.

Firebug says that gr.insert() is not defined:

$('#submit').click(function(){

              var gr1 = new GlideRecord('x_11234_notes_customers');

              gr1.initialize();

              gr1.name = 'first to do item';    

              gr1.list_of_services = 'learn about GlideRecord';    

              gr1.insert();

      });

Where am I wrong here?

1 ACCEPTED SOLUTION

Hi Ivan,



What it's doing now is running your insert when the page loads. When you run the update the data object gets passed back to the server as the input object, so try this in your server side script:



(function() {


      /* populate the 'data' object */


      /* e.g., data.table = $sp.getValue('table'); */



  if (input) {


      var gr = new GlideRecord('x_1111_customers');


      gr.initialize();


      gr.name = input.name;


      gr.list_of_services = input.list_of_services;


      gr.insert();


  }


 


})();


View solution in original post

8 REPLIES 8

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You'll need to put the GlideRecord update in the server script part of the widget, and then call that from the client side. This article describes how you can use this.server.get() or this.server.update().



Widget client script


HTML:


<button type="button" id="submit" ng-click="c.submitCustomerForm();" name="submit" class="btn btn-primary pull-left">Submit Form</button>


Client Script:


function() {


  /* widget controller */


  var c = this;


     


      $('ul.panel-tabs a').click(function (e) {


      e.preventDefault();


      $(this).tab('show');


});


     


      c.submitCustomerForm = function(){


              c.data.name = 'first to do item';


              c.data.list_of_services = 'learn about GlideRecord';


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


                      console.log("response from Server");


                      c.data.name = "";


                      c.data.list_of_services = "";


              })


      };


}



Server Script:


(function() {


  /* populate the 'data' object */


  /* e.g., data.table = $sp.getValue('table'); */



              var gr = new GlideRecord('x_1111_customers');


              gr.initialize();


              gr.name = data.name;    


              gr.list_of_services = data.list_of_services;    


              gr.insert();


})();



It creates a new record in 'x_1111_customers' table, but 'name' and 'list_of_services' fields are empty.


Why?


Hi Ivan,



What it's doing now is running your insert when the page loads. When you run the update the data object gets passed back to the server as the input object, so try this in your server side script:



(function() {


      /* populate the 'data' object */


      /* e.g., data.table = $sp.getValue('table'); */



  if (input) {


      var gr = new GlideRecord('x_1111_customers');


      gr.initialize();


      gr.name = input.name;


      gr.list_of_services = input.list_of_services;


      gr.insert();


  }


 


})();


Thanks a lot!


I've tried it before but with


gr.name = data.name;


instead of using


gr.name = input.name;



Thanks again.