Leveraging utility methods

Now that we have worked with callback and change functions on iparams, let us take it further to work with utility methods to enable retrival of iparam values using utils.get() and also setting the iparam value and attributes with utils.set().

Let us use the validateChange callback function from validate iparam's change event to invoke Freshdesk API and validate if the credentials are valid or not using request method by fetching the iparams values using utils.get().

config/assets/iparams.js file

async function init() {
  client = await app.initialized();
}

function domainChange(newValue) {
  //Input type validation
  let regex = /[A-Za-z]+[0-9]+\.freshdesk\.com/i;
  if(!regex.test(newValue)){
    utils.set("domain_name", { hint: "Invalid domain" })
  }
}

async function validateChange(newValue) {
  if(newValue) {
    try{
      let res = await client.request.invokeTemplate("validateAPI",{
        context: {
          domain: utils.get("domain_name"),
          apiKey: utils.get("api_key")
        }
      })
    }catch(e){
      utils.set("domain_name",{hint: "Invalid domain"})
      utils.set("api_key",{hint: "Invalid API key"})
    }
  }
}

To test this, use fdk run and update the iparam values in http://localhost:10001/custom_configs.