Calling Rest API using JavaScript function in vbcs

We can call rest API configured in service connection using RestHelper

The function getLineItems() can be called using action chain.

define(["vb/helpers/rest"], (Rest) => {
  "use strict";

  class PageModule {
    getLineItems() {
      let endpointurl = "buinessObjects/getalls_Country";
      let queryString = "id is not null";

      return new Promise(function (resolve, reject) {
        var ep = Rest.get(endpointurl);
        ep.parameters({
          q: queryString,
          onlyData: true,
          limit: 50,
          orderBy: "id:asc",
        });
        ep.fetch().then(function (result) {
          if (result.response.ok) {
            resolve(result.body.items);
          } else {
            resolve([]);
          }
        });
      });
    }
  }

  return PageModule;
});

We can call the rest API configured in the service connection using Rest Helper in APP UI as well

We have to additional pass extensionId – site_MyExtension. ( id of the APP UI extension.

You can find the extension id from ->

  • Open workspace, click on extension name in left hand side
  • Go to settings and you will find extension id.
define(["vb/helpers/rest"], (Rest) => {
  "use strict";

  class PageModule {
    getLineItems() {
      let extensionId = "site_MyExtension"
    
      let endpointurl = "site_MyExtension:saasRestApi/getalls_Country";
      let queryString = "id is not null";
 
      return new Promise(function (resolve, reject) {
        var ep = Rest.get(endpointurl, { extensionId: "site_MyExtension" } );
        ep.parameters({
          q: queryString,
          onlyData: true,
          limit: 50,
          orderBy: "id:asc",
        });
        ep.fetch().then(function (result) {
          if (result.response.ok) {
            resolve(result.body.items);
          } else {
            resolve([]);
          }
        });
      });
    }
  }

  return PageModule;
});

About the Author

khalil

Hi, I'm Khalil, a passionate front-end developer with over 8 years of experience in web development. I specialize in building interactive, user-friendly websites using technologies like JavaScript, HTML, CSS, and Java.

I've had the opportunity to work on a variety of projects across different industries, helping businesses create modern and responsive web applications.

Outside of coding, I enjoy exploring the latest trends in design and tech, as well as sharing knowledge with others. I'm always looking for new challenges to grow my skills and make a positive impact in the tech community

You may also like these