Issue with set Value interface API in Freshservice

Hello Team,

I am using interface API to set the value to a field, but once the value is set, the success block is not getting executed.

 client.interface.trigger("setValue", {
            id: id,
            value: value
        }).then(function (data) {
            **// once the value is set, success block is not getting executed**
            console.log("data in set value", data);
        }, function (error) {
          **// In error block, the error is coming as "undefined"**
            console.log("error in ser value", error);
        });

Can you please look into it and let me know the reason causing this issue?

Even though it is not coming in success block, the value is getting set.

Regards,
Akhil S Kulkarni

Akhil,

Can you let me know when is this problem occuring? Or during which action?

Interface API can be used in multiple pages such as Contact Details, Contact List and so on. Can you point me to specific part of documentation may be? I can use this help to reproduce the issue to further debug.

Hi @Akhil_Kulkarni,

Just checking if it is still the case. Could you also share the code snippet that caused this scenario? We will be able to zero in on the error

Hello Hemchandra,

I am using below code in my project.

I am passing id and value from other functions and I can see the value is set to particular custom field but, in success block I am not getting anything.

The error is coming as undefined in error block.

Please let me know if I am doing something wrong here.

function setValues(client, id, value, dummyText, finalObject) {
        client.interface.trigger("setValue", {
            id: id,
            value: value
        }).then(function (data) {
            console.log("data in set value function", data);
            if (dummyText === "firstValue") {
                intermediatoryFunction(client, finalObject, "secondValue");
            } else {
                clickUpdateButton(client);
            }
        }, function (error) {
            console.log("error in set value function", error);
            if (dummyText === "firstValue" && error === undefined) {
                intermediatoryFunction(client, finalObject, "secondValue");
            } else {
                clickUpdateButton(client);
            }
        });
    }

hey @Akhil_Kulkarni, can you post your server function?
you might be missing the proper return. Here is an example

$db.set(args.id, { "value": args.value }).then (
function(data) {
  // success operation
  // "data" value is { "Created" : true }
  console.log(data)
  return renderData(null, data)
},
function(error) {
  // failure operation
  console.log(error)
  return renderData(null, error)

});
1 Like

Hello Omar,

Please find the below server side code.

var moment = require('moment');
var momentbusinesstime = require('moment-business-time');
var base64 = require('base-64');
exports = {
  calledFunctionFromFrontEnd: function (dataFromAPP) {
    getSelectedBusinessHours(dataFromAPP);
  }
}

function getSelectedBusinessHours(dataFromAPP) {
  console.log("in getSelectedBusinessHours function");
  var freshserviceURL = dataFromAPP.obj.iparamsData.url;
  var apiKey = dataFromAPP.iparams.api;
  var selectedBusinessHourID = dataFromAPP.obj.iparamsData.selectedBusinessHour;
  var url = `https://${freshserviceURL}/api/v2/business_hours/${selectedBusinessHourID}`;
  var headers = {
    "Authorization": "Basic " + base64.encode(apiKey),
    "Content-Type": "application/json"
  };
  var options = {
    headers: headers
  };
  $request.get(url, options).then(function (data) {
    try {
      var weekDaysObj = {};
      var result = JSON.parse(data.response).business_hours.service_desk_hours;
      var listOfHolidays = JSON.parse(data.response).business_hours.list_of_holidays;
      var finalHolidayList = [];
      var currentYear = (new Date).getFullYear();
      for (let i = 0; i < listOfHolidays.length; i++) {
        var splitDate = listOfHolidays[i].holiday_date.split("-");
        var finalDate = currentYear + "-" + splitDate[2] + "-" + splitDate[3];
        finalHolidayList.push(finalDate);
      }
      var weekDays = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
      for (let j = 0; j < weekDays.length; j++) {
        if (weekDays[j] in result) {
          weekDaysObj[`${j}`] = [`${result[weekDays[j]].beginning_of_workday}`, `${result[weekDays[j]].end_of_workday}`];
        } else {
          weekDaysObj[`${j}`] = null;
        }
      }
      findBusinessDays(dataFromAPP, weekDaysObj, finalHolidayList);
    } catch (error) {
      console.log(error);
    }
  }, function (error) {
    console.log(error);
  })
}

function findBusinessDays(dataFromAPP, weekDaysObj, finalHolidayList) {
  console.log("In findBusinessDays function");
  var mainObj = {};
  var flag;
  var timeStampFieldValPresent = dataFromAPP.obj.timeStampFieldValuePresent;
  if (timeStampFieldValPresent === null || timeStampFieldValPresent === undefined) {
    var ticketCreatedDate = dataFromAPP.obj.ticketCreatedAt;
  } else {
    flag = "previous updated date";
    let splittimeStampFieldValPresent = timeStampFieldValPresent.split(",");
    let requiredTextFieldValue = splittimeStampFieldValPresent[splittimeStampFieldValPresent.length - 2];
    var previousUpdatedDate = ((requiredTextFieldValue.split(":updated on:")[1]).split(":time used:"))[0];
    var ticketCreatedDate = previousUpdatedDate.split(" Time: ")[0] + "T" + previousUpdatedDate.split(" Time: ")[1];
    console.log("ticketCreatedDate == >> ", ticketCreatedDate);
  }
  var ticketUpdatedDate = dataFromAPP.obj.ticketUpdatedAt;
  console.log("ticketUpdatedDate .. ", ticketUpdatedDate);
  moment.updateLocale('en', {
    workinghours: weekDaysObj
  });
  if (finalHolidayList.length > 0) {
    moment.updateLocale('en', {
      holidays: finalHolidayList
    });
  }
  var days = ["0", "1", "2", "3", "4", "5", "6"];
  for (let i = 0; i < days.length; i++) {
    if (days[i] in weekDaysObj && weekDaysObj[days[i]] !== null) {
      var a = weekDaysObj[days[i]][1].split(":")[1] - weekDaysObj[days[i]][0].split(":")[1];
      var b = weekDaysObj[days[i]][1].split(":")[0] - weekDaysObj[days[i]][0].split(":")[0];
      if (a >= 30) {
        b = b + 1;
      } else {
        b = b;
      }
      var businessHoursSet = b;
      break;
    }
  }
  var splitDate = ticketUpdatedDate.split("T");
  var splitYear = splitDate[1].split("+");
  var modifiedticketUpdatedDate = `${splitDate[0]} ${splitYear[0]}`;
  if (flag === "previous updated date") {
    let modifiedPreviousTicketUpdatedDate = (ticketCreatedDate.split("T"));
    let modifiedPreviousTicketUpdatedTime = modifiedPreviousTicketUpdatedDate[1].split("+");
    var from_time = `${modifiedPreviousTicketUpdatedDate[0]} ${modifiedPreviousTicketUpdatedTime[0]}`;
  } else {
    var from_time = moment(ticketCreatedDate).format('YYYY-MM-DD HH:mm:ss', true);
  }
  var to_time = modifiedticketUpdatedDate;
  console.log("from_time == >> ", from_time);
  console.log("to_time == >> ", to_time);
  var hoursDifference = moment(to_time).workingDiff(moment(from_time), 'hours', true);
  var daysDifference = moment(to_time).workingDiff(moment(from_time), 'days', true);
  var minuteDifference = moment(to_time).workingDiff(moment(from_time), 'minute', true);
  var monthsDifference = moment(to_time).workingDiff(moment(from_time), 'months', true);
  mainObj["Hrs"] = hoursDifference;
  mainObj["Days"] = daysDifference;
  mainObj["Mins"] = minuteDifference;
  mainObj["Mons"] = monthsDifference;
  mainObj["businessHoursSet"] = businessHoursSet;
  mainObj["dataFromAPP"] = dataFromAPP;
  console.log("mainObj == >> ", mainObj);
  **renderData(null, mainObj);**
}

Please let me know if I am doing something wrong here

Regards,
Akhil S Kulkarni

@Akhil_Kulkarni my apologies, I misread the original client function. it appears you are calling #inteface.trigger on setValue. please show setValue

@Akhil_Kulkarni, I went through all the details that you have shared.

For the custom field, the name should be of the form cf_your_custom_field_name . For example, if your custom field name is “Agent Number”, then your setValue call should be: client.interface.trigger(“setValue”, { id: “cf_agent_number”, value:“121333”});

In your case does the id follow the cf_your_custom_field convention?

Hello Saif,

I am taking it dynamically (providing a dropdown in settings page and using ticket fields API to get all text fields).

So, field ID is coming correctly.

Please let me know if you need any more info.

Regards,
Akhil S Kulkarni

@Akhil_Kulkarni Can you quickly share value of id ? The same dynamic value that you fetch from settings page.

Hello Saif,

Please find the ID used -> cf_reopen_text

Regards,
Akhil S Kulkarni

It’s a text field.

Regards,
Akhil S Kulkarni

@Akhil_Kulkarni, I tried reproducing the issue by taking up a sample code and seeing if the success block is getting executed. The sample app worked fine as expected. I will drop an direct message in your forum’s inbox in order to not to expose your code.