How to Import JS request module in Hosted Target

Hi All,

I have created Hosted Target, as my requirement is to send parallel call-out from APIGEE to multiple objects (Accounts, Contacts etc.,) in Salesforce. I am trying to do this using nodejs.

- I installed nodejs locally and imported request module as zip. Please refer the attached screenshotcapture.png (I have added this as I am getting error as "ReferenceError: Request is not defined" error).

- Also is the above approach using Hosted Target best way for the parallel call-out?

Any help here would be helpful as I am new to APIGEE. Looping @ylesyuk @Dino-at-Google

Solved Solved
0 7 430
1 ACCEPTED SOLUTION

my requirement is to send parallel call-out from APIGEE to multiple objects (Accounts, Contacts etc.,) in Salesforce. I am trying to do this using nodejs.

  • You can send multiple parallel requests out using a JS callout.

  • You can also use a Hosted Target, using nodejs. Either will work.

For sending requests with a JS Callout, use the callback mechanism.

  function onComplete(response, error) {
    if (response) {
      context.setVariable('example.status', response.status);
    }
    else {
      context.setVariable('example.error', 'Whoops: ' + error);
    }
  }
  var req1 = new Request();
  //req1.body = '';
  req1.url = 'https://sf.com/something/oneid/' + id ;
  req1.method = 'GET';
  //req1.headers = { 'ID-Index' : ix };
  httpClient.send(req1, onComplete);
  var req2 = new Request();
  //req2.body = '';
  req2.url = 'https://sf.com/something/oneid/' + id ;
  req2.method = 'GET';
  //req2.headers = { 'ID-Index' : ix };
  httpClient.send(req2, onComplete);

For a Hosted Target, your nodejs code is wrong. You seem to be mixing code that would work in the Javascript callout, with code that would work with the nodejs request module. That's not going to work. The Request object available in the JS callout is not the same as the request module available in nodejs. You cannot mix and match code.

You need to start from the beginning with a nodejs sample, outside of any Apigee context. Get that working first. Follow a tutorial.

Also, as regards the request module, please consider this. The request module is now in maintenance mode. If you want to write modern Javascript, using promises and async/await, you should consider alternatives to the request module.

View solution in original post

7 REPLIES 7

sidd-harth
Participant V

I would recommend you to first try to get your Nodejs code running fine in your local machine. For queries check Stackoverflow and other blogs.

https://www.npmjs.com/package/request

Then create the manifest.yaml file as required and push it to Apigee.

If it works as excepted on your local instance, then it should sure work on Apigee Hosted Target.

Hi @Siddharth Barahalikar thanks for getting back. I have tried in my local through JS command line, but got the same error.

I am attaching the error and .js file. Sorry, if the code has any problems - that's almost my first one.jscommand-line.pngtestapijs.txt

Thanks much!!


Well this is purely an Nodejs issue and you can check stackoverflow.

For the time being you could use the below simple code,

const request = require('request');
const options = {
    url: 'https://um1.salesforce.com/services/data/v37.0/query/?q=SELECT+Id+FROM+Account',
    method: 'GET',
    headers: {
     'Content-Type': 'application/json',
     'Authorization': 'Bearer token'
   }
};


request(options, (err, res, body) => {
    if (err) {
        return console.log(err);
    }
    console.log(JSON.stringify(JSON.parse(body)));

});

in package.json add below dependency and run npm install,

"dependencies": {
    "request": ""
  }

Moreover do mask your tokens and urls.

Thanks for the JS. I have tried to run your script locally, but it gives me the same error as before, attached it here.capture.png?
Do you think the problem is with my test or nodejs installation? Below is installed in my local.

C:\Program Files\nodejs>npm -version

6.4.1

Can I please ask if you are able to test the same in your local using command line.
Thanks much!!

my requirement is to send parallel call-out from APIGEE to multiple objects (Accounts, Contacts etc.,) in Salesforce. I am trying to do this using nodejs.

  • You can send multiple parallel requests out using a JS callout.

  • You can also use a Hosted Target, using nodejs. Either will work.

For sending requests with a JS Callout, use the callback mechanism.

  function onComplete(response, error) {
    if (response) {
      context.setVariable('example.status', response.status);
    }
    else {
      context.setVariable('example.error', 'Whoops: ' + error);
    }
  }
  var req1 = new Request();
  //req1.body = '';
  req1.url = 'https://sf.com/something/oneid/' + id ;
  req1.method = 'GET';
  //req1.headers = { 'ID-Index' : ix };
  httpClient.send(req1, onComplete);
  var req2 = new Request();
  //req2.body = '';
  req2.url = 'https://sf.com/something/oneid/' + id ;
  req2.method = 'GET';
  //req2.headers = { 'ID-Index' : ix };
  httpClient.send(req2, onComplete);

For a Hosted Target, your nodejs code is wrong. You seem to be mixing code that would work in the Javascript callout, with code that would work with the nodejs request module. That's not going to work. The Request object available in the JS callout is not the same as the request module available in nodejs. You cannot mix and match code.

You need to start from the beginning with a nodejs sample, outside of any Apigee context. Get that working first. Follow a tutorial.

Also, as regards the request module, please consider this. The request module is now in maintenance mode. If you want to write modern Javascript, using promises and async/await, you should consider alternatives to the request module.

Thanks @Dino-at-Google. Totally makes sense. So, I can still use a Reverse proxy, and through JS callout send parallel requests?

yes you can. Just as I described above.