Object.extendsObject

Tim71
Kilo Explorer

I want to extend the sn_ws.RESTMessageV2 object to create a subclass but am having trouble getting the syntax correct.

 

This object sn_ws.RESTMessageV2  instantiates either as empty object  or populates via a table if passed two parameters.

Do I need to recreate the initialize function as part of the prototype definition or can i just extend the functionality with new calls?

How does one call the parent function from within the child prototype?

super() is not supported...

Nothing I have tried so far seems to work...everything involving calling the parent function methods just comes up undefined.

var ib_message = Class.create();

ib_message.prototype = Object.extendsObject(sn_ws.RESTMessageV2, {

       foo: function() {

               gs.print('foo b');

       },

       type: 'ib_message'

 

});

 

var end = "https://xxx.xxx.xx.xx";

var record_type="record:a";

var max_records_per_query = 50;

var max_queries = 2;

var fields="extattrs";

var r = new ib_message('Network_Sync', 'get_ib_records');

    r.setStringParameter("rest_endpoint",end);

    r.setStringParameter("record_type", record_type);

    r.setQueryParameter("_paging", 1);

    r.setQueryParameter("_return_as_object", 1);

    r.setQueryParameter("_max_results", max_records_per_query);

    r.setQueryParameter("_return_fields%2B",fields);

    r.setEndpoint(end);

    gs.print(r.getEndpoint());

    var resp = r.execute();

    var httpResponseStatus = resp.getStatusCode();

      

    gs.print("http response status_code: " + httpResponseStatus); 

 

4 REPLIES 4

Jace Benson
Mega Sage

I'd suggest editing your post by updating the title to get more people to look at it.

Make a good to-the-point title for the question or as stackoverflow says, "Write a title that summarizes the specific problem". This helps because it lets those who can help, know they can without going into the post. You might be thinking, I can't sum it up it's too complex. No problem, try to think how you'd ask a busy colleague, and put that into the subject. If you're still having trouble, write the title last after all the details are fresh.

  • Good: Set Value form variable in the request form to Table field
  • Good: How to disable attachment confirmations?
  • Good: How can I set an override reference qualifier to a group
  • Bad: NetApp
  • Bad: Photo Masking
  • Bad: Not Available for Module script

https://stackoverflow.com/help/how-to-ask

Jon Barnes
Kilo Sage

I may be wrong about this, but I believe you cannot extend a Java object to a javascript class as you are trying to do, and I am fairly certain that sn_ws.RestMessageV2 is a java object.

Jon Barnes
Kilo Sage

I may be wrong, but I don't think you can extend a java object to a javascript class using extendsObject. and since sn_ws.RestMessageV2 appears to be a java object (like GlideRecord), I don't think you can extend it in that way. But I do think you can add functions to it using prototype. See this blog from Steve Bell for how he extended the GlideRecord object to add a couple new functions to it.

https://community.servicenow.com/community?id=community_blog&sys_id=b96c2ea1dbd0dbc01dcaf3231f9619b9&view_source=searchResult

BillMartin
Mega Sage

Hi @Tim71 ,

 

I have created this easy to follow example where you can inherit from a class using Object.extendsObject() function. just replace the code with a REST-Message function.

 

You can find more details here: Master Good Software Architecture with Object.extendsObject() | A Guide for ServiceNow Developers