Home > OS >  How to call a function and use it's return value?
How to call a function and use it's return value?

Time:12-07

I am not sure how I can pass params in a function, currently I want to store an access token in one function and then pass the params into another function, is there something I am missing or doing wrong?

function access_token()
{
  var token = zauthorization().access_token;

  var header = 
  {
    "Authorization":"Zoho-oauthtoken "   token
  }

  return header
}

function module(access_token)
{

   var options = 
  {
    "method":"get",
    "headers": header
  }
  
  var url = "https://recruit.zoho.com/recruit/v2/CustomModule1"
  var res = UrlFetchApp.fetch(url,options)
  return JSON.parse(res)
}

So in short I am not sure how I can pass the header into my next function, I need to have it done that way to go onto the next function, I need to create another function after this which will allow me to access data in a specific module so I can have the counted rows returned, I just need to know how to pass parameters in a new function from another function that can be used.

CodePudding user response:

It seems a bit simple what I think you are asking, but the only thing I can think is: You call a function by writing it's name and pass arguments in brackets separated by a comma if there are more.

myFunction(parameter1,parameter2,55,'This is a text string passed.')

parameter1 and 2 are variables, then a number, then a text string.

CodePudding user response:

I'm not sure but I think you can do it like this:

Since your first function returns the header

function myfunk() {
  var token = zauthorization().access_token;
  var header = { "Authorization": "Zoho-oauthtoken "   token }
  return header;
}

function module() {
  var options = { "method": "get", "headers": myfunk() };
  var url = "https://recruit.zoho.com/recruit/v2/CustomModule1"
  var res = UrlFetchApp.fetch(url, options)
  return JSON.parse(res)
}

Or more simply perhaps this:

function module() {
  var options = { "method": "get", "headers": { "Authorization": "Zoho-oauthtoken "   zauthorization().access_token } };
  var url = "https://recruit.zoho.com/recruit/v2/CustomModule1"
  var res = UrlFetchApp.fetch(url, options)
  return JSON.parse(res)
}

But I'm not familiar with what zauthorization() is so I could be totally off base here. If so sorry.

  • Related