let accountNameToDelete = ""; //triggered when adding a location $("#btn-map-location").on("click", function () { showLoader(); let customerNumber = $("#customerNumber").val(); if (customerNumber) { //get the account details from account table based on the account number entered apiService.getItems( "accounts?$select=accountid,name,address1_composite,accountnumber&$filter=accountnumber eq '" + customerNumber + "'" ) .then((data) => { if (data.value.length > 0) { let accountData = { "ricnt_Account@odata.bind": `/accounts(${data.value[0].accountid})`, "ricnt_Contact@odata.bind": `/contacts(${$("#contact-id").val()})` }; //checks if the account is already added apiService.getItems( "ricnt_contactaccountmappings?$select=ricnt_contactaccountmappingid,ricnt_Account,ricnt_Contact&$expand=ricnt_Account($select=accountnumber),ricnt_Contact($select=contactid)&$filter=ricnt_Account/accountnumber eq '" + data.value[0].accountnumber + "' and ricnt_Contact/contactid eq '" + $("#contact-id").val() + "'" ) .then((data) => { if (data.value.length == 0) { //add the account to contact account mapping table apiService.postItem("ricnt_contactaccountmappings", accountData) .then((res, status, xhr) => { hideLoader(); showSuccessToast("Account added successfully."); populateAccountTable(); $("#customerNumber").val(""); populateAccountDropdown(); }) .catch((error) => { hideLoader(); showErrorToast("Failed to create order."); console.error("Error creating order:", error); }); } else { hideLoader(); showErrorToast("Account is already added."); } }) .catch((error) => { hideLoader(); showErrorToast("Failed to get account details."); console.error("Failed to get account details", error); }); } else { hideLoader(); showErrorToast("Invalid account number."); } }) .catch((error) => { hideLoader(); showErrorToast("Failed to get account details."); console.error("Failed to get account details", error); }); } else { showErrorToast("Customer number is required."); hideLoader(); } }); // triggered when clicking on Add/Remove button $("#add-remove-location").on("click", function () { populateAccountTable(); }); function populateAccountTable() { let userAccountDetails = ""; showLoader(); //get account details of the current user apiService.getItems( "ricnt_contactaccountmappings?$select=ricnt_contactaccountmappingid,ricnt_Account,ricnt_Contact&$expand=ricnt_Account($select=accountnumber,name,address1_composite),ricnt_Contact($select=contactid)&$filter=ricnt_Contact/contactid eq '" + $("#contact-id").val() + "'" ) .then((data) => { if (data.value.length > 0) { data.value.forEach(element => { userAccountDetails += ` ${element.ricnt_Account.name} ${element.ricnt_Account.accountnumber} ${element.ricnt_Account.address1_composite ?? "-"} `; }); } //poulate account table $("#added-accounts").empty(); $("#added-accounts").append(userAccountDetails); hideLoader(); }) .catch((error) => { hideLoader(); showErrorToast("Failed to get account details."); console.error("Failed to get account details", error); }); } function deleteAccountConfirm(id, accountnumber) { $("#account-name-delete").text($("#" + id).data("value")); $("#txt-account-to-delete").val(id); $("#txt-account-number-to-delete").val(accountnumber); accountNameToDelete = $("#"+id).data("value"); } function deleteAccount() { showLoader(); apiService.deleteItem("ricnt_contactaccountmappings", $("#txt-account-to-delete").val()) .then((data) => { showSuccessToast("Account deleted successfully."); populateAccountTable(); $('input[type="checkbox"][value="' + $("#txt-account-number-to-delete").val() + '"]').parent().hide(); let accountNumberToDelete = $("#txt-account-number-to-delete").val(); let accountLabelToDelete = accountNameToDelete + " - " + accountNumberToDelete; selectedAccountLabels = selectedAccountLabels.filter(item => item != accountLabelToDelete); selectedAccounts = selectedAccounts.filter(item => item != accountNumberToDelete); sessionStorage.setItem('selectedAccounts', JSON.stringify(selectedAccounts)); sessionStorage.setItem('selectedAccountLabels', JSON.stringify(selectedAccountLabels)); //updateLocationDropdownButton(); location.reload(); }) .catch((error) => { hideLoader(); showErrorToast("Failed to delete account."); console.error("Failed to delete account", error); }) }