Error while substituting the templates

Hello,

I am getting an Error while substituting the templates error when attempting to make 3 separate API calls to Freshservice. I am working on updating a large client’s code from 2.2 to 2.3 and am following the migration guide provided by Freshworks, but following those steps has resulted in this error. My code calls the GET requester with
iclient.request.invokeTemplate("fdGetMethod",{context:{"path":"requester_fields"}}

Within requests.js I have defined the fdGetMethod as follows:

"fdGetMethod": {
        "schema": {
          "method": "GET",
          "host": "<%= iparam.fdurl %>",
          "path": "/api/v2/<%= context.path %>",
          "headers": {
            "Authorization": "Basic <%= encode(iparam.api_key) %>",
            "Content-Type": "application/json"
           }
        }
    }

fdurl is defined within iparams.html and is linked to the input.

I make this call for requester_fields, department_fields, and ticket_form_fields. All the calls return with “Error while substituting the templates”. From the console I can tell they are all returning a rejected promise.

The request payload is as follows:

{
    "action": "execute",
    "data": {
        "name": "fdGetMethod",
        "context": {
            "path": "/api/v2/ticket_form_fields"
        },
        "isInstall": true,
        "isOAuth": true
    },
    "isInstall": true
} 

and the response is:

{
    "status": 400,
    "headers": {},
    "response": "Error while substituting the templates",
    "errorSource": "APP",
    "attempts": 1
}

I am not sure what to try to fix this issue.

Hi @Jose_Escobar ,

It appears that you forgot to include the “protocol”: “https” parameter in your request method. Please add it as shown below and attempt again.

“fdGetMethod”: {
“schema”: {
“method”: “GET”,
“protocol”: “https”
“host”: “<%= iparam.fdurl %>”,
“path”: “/api/v2/<%= context.path %>”,
“headers”: {
“Authorization”: “Basic <%= encode(iparam.api_key) %>”,
“Content-Type”: “application/json”
}
}
}

Hi @Jose_Escobar,

Does this request method work if the host, path, and Authorization headers are hard-coded with static values?
If it works, could you replace it one by one and check which variable is causing the issue?

Please verify if the variable limitations are considered and used accordingly.

If it still fails and you can’t find, please add where this API request is made from and if the iparams are already submitted.

Hello, I tried adding this and I am still receiving the same error.

New protocol:

"fdGetMethod": {
        "schema": {
          "method": "GET",
          "protocol": "https",
          "host": "<%= iparam.fdurl %>",
          "path": "/api/v2/<%= context.path %>",
          "headers": {
            "Authorization": "Basic <%= encode(iparam.api_key) %>",
            "Content-Type": "application/json"
           }
        }
}

Hello Raviraj,

I have also tried hard coding all the values in there and I am getting the same issue. I tried with the following get request:

"fdGetMethod": {
        "schema": {
          "method": "GET",
          "protocol": "https",
          "host": "realURL.freshservice.com",
          "path": "/api/v2/requester_fields",
          "headers": {
            "Authorization": "Basic <%= encode(RealAPIKey) %>",
            "Content-Type": "application/json"
           }
        }
    },

The iparams are being filled on iparams.html on installation so they are present before the get call (the form for this input is below, the “Verify Freshservice” button is the one making the call to the API with the given input)

I am considering the variable limitations and using the context variable to provide the path in the api call and the get method as shown below. This is how it is shown in the migration guide.

 "fdGetMethod": {
        "schema": {
          "method": "GET",
          "protocol": "https",
          "host": "<%= iparam.fdurl %>",
          "path": "/api/v2/<%= context.path %>",
          "headers": {
            "Authorization": "Basic <%= encode(iparam.api_key) %>",
            "Content-Type": "application/json"
           }
        }
    },

The API request is made from the iparams.js file and is as follows:

(iclient.request.invokeTemplate("fdGetMethod",{context:{"path":"requester_fields"}}))

iclient is set on app initialization:

$(document).ready(function () {
    app.initialized().then(function (client) {
        iclient = client;

Is there anything else I can provide to help solve this issue?

Hey @Jose_Escobar,
Is this template used in the custom iparam page? Then the host and api key would have to be passed via context.

Here is a sample - GitHub - freshworks-developers/request-method-samples: Sample codes to demonstrate making API calls using Request Method that you can refer to for this use case.

var res = await client.request.invokeTemplate("iparamValidate", {
            context: {
                subdomain: domain.value,
                api_key: apiKey.value
            }
        });

Let me know if this helps.

2 Likes

This was exactly the issue. The domain and api key were being submitted in iparams.html, so they were not accessible yet during the installation process. I passed domain/api-key into the request as variables in context and it worked.

Thank you very much to everyone who helped me get through this issue!

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.