



// Initialize the embedded app ZOHO.embeddedApp.on("PageLoad", async function(data) { console.log("PageLoad data:", data); // Step 1: Get the entity (module) and entity ID (Contact record ID) if (data && data.Entity && data.EntityId) { entityModule = data.Entity; entityId = data.EntityId; console.log("Entity:", entityModule); console.log("Entity ID (CRM Contact ID):", entityId); // Create reusable ZRC instance for Zoho Desk API deskZrc = zrc.createInstance({ baseUrl: 'https://desk.zoho.com/api/v1', connection: 'desk_oauth_connection' }); await loadTickets(); } else { showError("No contact context found"); } }); ZOHO.embeddedApp.init(); |
// Step 2: Make GET contacts API call to Desk using ZRC const contactsResponse = await deskZrc.get('/contacts', { params: { limit: 100 } }); console.log("Desk Contacts Response:", contactsResponse); if (contactsResponse && contactsResponse.data) { // Parse the response data const contactsData = typeof contactsResponse.data === 'string' ? JSON.parse(contactsResponse.data) : contactsResponse.data; // Find the object where zohoCRMContact.id matches Contact record ID if (contactsData && contactsData.data && Array.isArray(contactsData.data)) { const matchingContact = contactsData.data.find(function(contact) { return contact.zohoCRMContact && contact.zohoCRMContact.id && contact.zohoCRMContact.id === entityId; }); if (matchingContact) { // Pick the id (Desk contact record ID) const deskContactId = matchingContact.id; console.log("Desk Contact ID:", deskContactId); // Proceed to fetch tickets await fetchTickets(deskContactId); } else { showEmptyState("No Desk contact found linked to this CRM contact"); } } } |
async function fetchTickets(deskContactId) { try { // Step 3: Make GET Tickets API call using ZRC const ticketsResponse = await deskZrc.get('/tickets', { params: { include: 'contacts', status: 'Open', limit: 100 } }); console.log("Tickets Response:", ticketsResponse); if (ticketsResponse && ticketsResponse.data) { const ticketsData = typeof ticketsResponse.data === 'string' ? JSON.parse(ticketsResponse.data) : ticketsResponse.data; // Filter tickets where contact matches deskContactId if (ticketsData && ticketsData.data && ticketsData.data.length > 0) { const matchingTickets = ticketsData.data.filter(function(ticket) { return ticket.contact && ticket.contact.id === deskContactId; }); if (matchingTickets.length > 0) { renderTickets(matchingTickets); } else { showEmptyState("No open tickets found for this contact"); } } else { showEmptyState("No open tickets found"); } } } catch (error) { console.error("Error fetching tickets:", error); showError("Failed to fetch tickets: " + (error.message || error.toString())); } } |




