Update contact and convert report to xml

Can I update the contact page with data from an external api call in my custom app which is placed in the ticket page?

And is it possible to covert the report from CSV to XML format?

Hi @Esther_Itolima ,

Greetings!

Could you please provide more information about the two cases you mentioned? Updating a contact from a custom app is possible using contact APIs.

If I am mistaken, kindly correct me. It would be great if you could share some additional details.

Thank you.

So my custom app is calling an external api. The custom app is placed on the ticket page.

What I have working now is that I can update the ticket details with the data from the external api.

Now, my client wants to update the contact details of a particular ticket.

Secondly for the report, my client wants to be able to export the report in xml format too.

Sure @Esther_Itolima ,

As mentioned earlier, you can update the contact details of the ticket from your custom app.

However, could you please let me know which report you are trying to download and from where?

Thank you.

Hello Anish,

It is an analytic report. Had a session with the support team and they provided information about the report.

How do I go about doing the contact updates.

I have used the same procedure with the ticket update but is not working .

Hi @Esther_Itolima ,

Greetings!

Could you please share the snippet of what you are trying to do so I can help you out?

Thank you.

let client;
let ticketId;
let contactId;

init();

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

async function getTicketDetails() {
	try {
		const data = await client.data.get("ticket");
		ticketId = data.ticket.id;
	} catch (error) {
		console.log(error);
	}
}

async function getContactData() {
	try {
		const data = await client.data.get("contact");
		contactId = data.contact.id;
	} catch (error) {
		console.log(error);
	}
}

async function searchAccountDetails() {
	// Get the account number from the input field
	let accountNumber = document.getElementById("accountNumber").value;

	let payload = enc(accountNumber);

	if (!isValidAccountNumber(accountNumber)) {
		document.getElementById("invalidAccountImage").style.display = "none";
		document.getElementById("accountDetails").style.display = "none";
		document.getElementById("accountNumberError").innerHTML =
			"Account number must be 10 digits.";
		return;
	} else {
		document.getElementById("accountNumberError").innerHTML = ""; // Clear the error message
	}

	try {
		const response = await fetch(
			`baseUrl/api/Account/GetAccountDetails`,
			{
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					agentID: "crm",
				},
				body: JSON.stringify(payload),
			}
		);


		// Parse the response JSON
		const data = await response.text();
		const decryptResponse = JSON.parse(dec(data));
		// Handle the API response
		if (decryptResponse && decryptResponse.responseCode === "00") {
			sessionStorage.setItem("details", JSON.stringify(decryptResponse));
			// Display account details
			displayAccountDetails(decryptResponse);
			if (decryptResponse.status === 5) {
				document.getElementById("updateButton").disabled = true;
			} else {
				document.getElementById("updateButton").disabled = false;
				updateTicketDetails(decryptResponse);
				updateContactDetails(decryptResponse);
				// window.location.reload();
			}
			document.getElementById("invalidAccountImage").style.display = "none";
		} else {
			// No account details found
			document.getElementById("accountDetails").style.display = "none";
			document.getElementById("invalidAccountImage").style.display = "block";
		}
	} catch (error) {
		// Handle errors
		document.getElementById("accountDetails").style.display = "none";
		document.getElementById("invalidAccountImage").style.display = "block";
	}
}


async function updateContactDetails(details) {
	const accountNumber = parseInt(details?.shortAccount);

	let myHeaders = new Headers();
	myHeaders.append("Content-Type", "application/json");
	myHeaders.append("Authorization", "*********");
	myHeaders.append("Cookie", "_x_m=x_c; _x_w=euc");

	let payload = JSON.stringify({
		name: details?.accountName,
		email: details?.email,
		phone: details?.phoneNumber,
		gender: details?.gender,
		account_number: accountNumber,
	});

	let requestOptions = {
		method: "PUT",
		headers: myHeaders,
		body: payload,
	};

	fetch(
		`url/api/v2/contacts/${contactId}`,
		requestOptions
	)
		.then((response) => {
			// Check if response is successful before calling getTicketDetails
			if (response.ok) {
				
				return response.text();
				
			} else {
				throw new Error("Update details failed");
			}
		})

		.catch((error) => console.log("error", error));
}

Here is my code snippet and the response I get

Hello @Anish ,

Please is there anything that I can do about this?

Hi @Esther_Itolima,

Greetings!

It seems that you have been using the fetch method to make API calls. However, please note that a 400 error status code occurs if you request an invalid body.

I hope you are using the latest platform version 2.3 as mentioned previously. In this version, you cannot make API calls directly.

Instead, you need to configure the requests within the requests.json file located in the config folder, and also update the configuration in the manifest.json. Kindly check the link below for further reference:

https://developers.freshworks.com/docs/app-sdk/v2.3/freshdesk/advanced-interfaces/request-method/

I hope this helps.

Thank you.

HI @Esther_Itolima ,

Just a follow-up to see if the above solution worked for you. Could you kindly provide an update on the same?

Thank you!

Hi @Esther_Itolima ,

Greetings!

I hope the article above was helpful. If not, please let me know.

Thank you :blush: