Contact List Export and Download

I've viewed many of the other forum posts and not having any success. If I click the 'Export' button on the contact list, I am able to use my code to to get the contact list download. But, if I don't click on that button, it will error out. Below is the code I currently have. Something to note, if I change the first outboundApi.getOutboundContactlist(contactListId) to post, I get the error that the "File wasn't available on the site."

function downloadList() {
console.log("Download button clicked.");
const contactListId = $('#contactLists').find(':selected').val();
console.log("Selected contact list ID:", contactListId);

		 // Check if a contact list has been selected
		 if (!contactListId || contactListId === "") {
			$('#upload-result').html($('<div>').addClass('alert alert-danger').text('Please select a Contact List before downloading.'));
			return; // Exit the function early
		}

		// Show a loading indicator
		$('#upload-result').html($('<div>').addClass('text-center').text('Preparing Download...')); 

		outboundApi.getOutboundContactlistExport(contactListId) 
			.then((response) => {
				const downloadUri = response.uri;

				outboundApi.getOutboundContactlist(contactListId)
					.then((contactList) => {
						const link = document.createElement('a');
						link.href = downloadUri;
						link.download = contactList.name + '.csv'; 
						link.style.visibility = 'hidden';
						document.body.appendChild(link);
						link.click(); 
						document.body.removeChild(link); 

						// Clear the loading indicator
						$('#upload-result').html(''); 
					})
					.catch((err) => {
						console.error('Error getting contact list name:', err);
						$('#upload-result').html($('<div>').addClass('alert alert-danger').text('Download failed. Error getting contact list name.'));
					});
			})
			.catch((err) => { 
				console.error('Error getting download URI:', err);
				$('#upload-result').html($('<div>').addClass('alert alert-danger').text('Download failed. No download URI exists.'));
			}); 
	}

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