Installed serverless app is only working when tested locally

Hello everyone,
On Freshdesk, I am developing a serverless app which fills a ticket field on ticket creation. When I test it on local (http://localhost:10001/web/test), it works by filling the custom field
But when I install the app on my helpdesk and create a ticket (whether from agent interface or portal), it does not fill the field.
What should I do?
Thank you

Hi @Andonirina

Can you share your code snippet so that I can understand the problem better and check where the error is? In local, the events are just simulated by user intervention. So I would like to check the code snippet for further debugging.

Thanks & Regards,
Mughela Chandresh

Of course
What I am doing is when someone is creating a ticket, the app fill the “code client numerique” ticket field. I get this information from the company the requester is in
When simulating ticket creation on local test , the app works and fill the field but when I upload the app to freshdesk and create ticket, it does not work anymore

This is the event in manifest.json :

"product": {
    "freshdesk": {
      "events": {
        "onTicketCreate": {
          "handler": "onTicketCreateHandler"
        }
      },

This is server.js :

var request = require('request');
var handler = require('./lib/handle-response');
var Buffer = require('buffer').Buffer;
var LZUTF8 = require('lzutf8');
var content,contact_det,comp_code_client;

exports = {
  events: [
    { event: 'onTicketCreate', callback: 'onTicketCreateHandler' }
  ],
  onTicketCreateHandler:async function (args) {
	  console.log("onTicketCreateHandler");
    content=await fetchData(args);
		if(content==null){
      console.log("No Requester ID Type Entered");
    }
    else{
      console.log("Requester ID is..",content);
			contact_det=await fetchContact(args,content);
			if(contact_det==null){
				console.log("No company details entered in contacts");			
			}
			else{
				comp_code_client=await fetchcode_client(args,contact_det);
				if(comp_code_client==null){
          console.log("4");
					console.log("No code_client entered for this Company");	
				}
				else{
          console.log("4");
					await add_code_client(args,comp_code_client);
				}
			}
    }
  }
};

async function fetchData(args){ 
  console.log("Fetch data")
  // returns the ticket requester id
  return new Promise(function(resolve,reject){
    console.log("ici");
    var  new_tid=args['data']['ticket']['id'];
    console.log("this is new ticket id",new_tid); 	  
    $request.get(`${getDomain(args)}/api/v2/tickets/${new_tid}`, {
      headers: {
        'Authorization': 'Bearer <%= encode(iparam.api_key) %>' // Here, access_token is passed safely which is a secure installation parameter     
      },
      isOAuth: true,
      json: {}
    }).then(function (data) {
      if(data!=''){  
        console.log('EEEEEEEEEEEEEEEEEE',data,data.response.requester_id);
        resolve(data.response.requester_id);
      }
      else {
        console.log("No ticket")
        reject(err);
      }
    })
  },function (err) {
    console.log('Error occured while trying to fetch ticket details' , JSON.stringify(err));
  });
}

var contacts=[];
async function fetchContact(args,req_id){ //Fetch the contact who created the ticket
  // returns the ticket requester company ID
  return new Promise(function(resolve,reject){
    console.log("Inside fetchContact",req_id); 	 
	  $request.get(`${getDomain(args)}/api/v2/contacts/${req_id}`, {
      headers: {
        'Authorization': 'Bearer <%= encode(iparam.api_key) %>' // Here, access_token is passed safely which is a secure installation parameter     
        },
      isOAuth: true,
      json: {}
    }).then(function (data) {
      console.log("data",data);
      if(data!=''){
        console.log('fcon',data.response.company_id);
        resolve(data.response.company_id);
      }
      else {
        console.log("data err",err);
        reject(err);
      }
    })
  },function (err) {
    console.log('Error occured while trying to fetch ticket details' , JSON.stringify(err));
  });  
}
  
async function fetchcode_client(args,company_id){ // Fetch code client numerique
  return new Promise(function(resolve,reject){
    console.log("Inside Fetch code_client function"); 
    $request.get(`${getDomain(args)}/api/v2/companies/${company_id}`, {
      headers: {
        'Authorization': 'Bearer <%= encode(iparam.api_key) %>' // Here, access_token is passed safely which is a secure installation parameter     
      },
      isOAuth: true,
      json: {}
    }).then(function (data) {
		  if(data!=''){  
		    console.log('COMPANY data...................',data);
		    console.log('fcon',data.response.custom_fields.code_client_numerique);
        resolve(data.response.custom_fields.code_client_numerique);
      }
      else {
        reject(err);
      }
    })      
  },function (err) {
    console.log('Error occured while trying to fetch ticket details' , JSON.stringify(err));
  });
}

async function add_code_client(args,comp_code_client){
  console.log("Inside Update function");
  var new_id=args['data']['ticket']['id'];
  return new Promise(function(resolve,reject){
    let updated_details={
      "custom_fields":{
			  "cf_code_client_numerique":comp_code_client
      }
    };
    var op={
      method:"PUT",
      url:`${getDomain(args)}/api/v2/tickets/${new_id}`,
      headers:{
        "Authorization":getToken(args),
        'content-Type':'application/json'
      },
      body:updated_details,
      json:true
    };
	  console.log("op",op);
    request(op,(err,res,body)=>{
      if(body!='')
      {
        console.log(body);
        resolve(body);
      }
      else {
        reject(err);
      }
    });
  })
}  


function getToken(args){
  return Buffer.from(args['iparams'].api_key+':x').toString('base64');
}
function getDomain(args){
  return `https://${args['iparams'].FreshdeskDomain}.freshdesk.com`;
}

The full manifest.json file if needed :

{
  "platform-version": "2.2",
  "dependencies": {
    "request": "2.88.2",
    "buffer": "5.6.0",
    "lzutf8": "0.5.5"
  },
  "product": {
    "freshdesk": {
      "events": {
        "onTicketCreate": {
          "handler": "onTicketCreateHandler"
        }
      },
      "location": {
        "new_email_requester_info": {
          "url": "template.html",
          "icon": "icon.svg"
        },
        "new_ticket_requester_info": {
          "url": "template.html",
          "icon": "logo.svg"
        }
      }
    }
  },
  "whitelisted-domains": [
    "https://*.freshdesk.com"
  ],
  "engines": {
    "node": "14.18.1",
    "fdk": "8.4.0"
  }
}

Hi @Andonirina

Is onTicketCreateHandler triggered and could you see ‘onTicketCreateHandler’ log in the serverless logs?

Yes I can see it when I am doing local tests with ticket creation simulation .
But how can I view logs from real ticket creation via Freshdesk?

You can view the serverless logs by following the below steps

  1. navigate to gallery
  2. go to installed apps
  3. click on the settings icon near the app
  4. click on the logs option from the dropdown
  5. give the time range and logs will be displayed

Please feel free to ping if you have further queries.

Regards,
Mughela Chnadresh

I skimmed through your code snippet and following can be the reasons for not populating the custom ticket field ‘code client numerique’.

  1. The ticket requester might not have an associated company
  2. If the ticket requester has an associated company, then that company does not have a custom customer field ‘code client numerique’
  3. if the company has a code client numerique custom customer field then the value of that field might be empty
  4. if all the above is achieved then the custom ticket field ‘code client numerique’ will be auto-filled.

Note : There are 2 types of fields in freshdesk. 1. Ticket fields 2. Customer fields. The custom fields in the companies api will return the custom fields in the customer fields and NOT the ticket fields

Please check the serverless logs for debugging further.

Let me know for further assist

Thanks & Regards,
Mughela Chandresh

I checked the logs and there is nothing
image

So the event is not even triggered. How is it that it does work on testing environment but not after installation? Thanks

Hi @Andonirina

If you don’t mind, Can I get occasional agent access to your account with admin access for further debugging?

Of course. If you give me your email address, I can give you access

mughela.chandresh@freshworks.com

Thanks! I added you as account admin
The app name is Code Client

I did not see this message
The field I wanted to fill is a ticket field. But the value I have to give is a customer field which I take from the requester’s company.
The “code client” field I have to take is a company requester field. But there is a ticket field name also “code client”.
Also, the app works on local testing with the same contact as the one I use on freshdesk for real testing

Thanks for the access. I have created a test ticket in your helpdesk and logs are displaying now. As I have suspected earlier in step 3, Your code is checking for ‘code client numerique’ field in the customer fields. Since that field is not present, control exits with ‘No code_client entered for this Company’ error and ‘code client numerique’ ticket field is not populated.
To populate the field, test it by the following steps

  1. Please add the code client numerique field in the customer fields. go to admin settings → customer fields → company → add ‘code client numerique’ field
  2. Take any one requester and associate company to that requester
  3. go to that company and fill the code client numerique field by editing that company details
  4. finally create a ticket and value will be populated

Thanks and Regards,
Mughela Chandresh

The code is not checking on ‘code client numerique’ on the customer but on company the customer belongs in (image below)

If the log says that ‘No code_client entered for this Company’, it is because the company of the customer you used to create the ticket does not have the field ‘code client numerique’ filled, as it is not mandatory

However, I did not do any other change but now the feature is working. Thank you for your help!

1 Like

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