<?xml version="1.0" encoding="UTF-8"?>
<WebLink xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Process_File</fullName>
    <availability>online</availability>
    <description>Process Uploaded file</description>
    <displayType>button</displayType>
    <linkType>javascript</linkType>
    <masterLabel>Process File</masterLabel>
    <openType>onClickJavaScript</openType>
    <protected>false</protected>
    <url>{!REQUIRESCRIPT(&quot;/soap/ajax/59.0/connection.js&quot;)}
{!REQUIRESCRIPT(&quot;/soap/ajax/59.0/apex.js&quot;)}

let recordId = &apos;{!Sub_Upload_Util__c.Id}&apos;
let isFileUploaded = &apos;{!Sub_Upload_Util__c.File_Uploaded__c}&apos;;
let mailingListId = &apos;{!Sub_Upload_Util__c.Mailing_ListId__c}&apos;;
let overWriteFanData = &apos;{!Sub_Upload_Util__c.OverwriteFanData__c}&apos;;

function isAnotherProcessRunning(){
  return sforce.apex.execute(&quot;SubUploadUtilProcess&quot;,&quot;isAnotherProcessRunningForTheSameML&quot;,{mlId: mailingListId});
}

function openConfirmationModal() {
  const text = &apos;You have checked the box to overwrite fan data. Do you want to proceed? \n\n If not, click &quot;Cancel&quot; then click &quot;Edit&quot; to uncheck the box.&apos;;
  if (confirm(text) == true) {
    processFile();
  } 
}

function processFile(){
  sforce.apex.execute(&quot;SubUploadUtilProcess&quot;,&quot;initiateProcess&quot;,{recordId: recordId});
	 alert(&apos;File Process Job initiated successfully.&apos;);
  location.reload();
}

if(isFileUploaded == false){
  alert(&apos;No files attached to initiate process&apos;);
} else {
  let ongoingSubUploadList = isAnotherProcessRunning();
  if(ongoingSubUploadList.length &gt; 0){
    alert(&apos;The upload &quot;&apos; + ongoingSubUploadList[0].Name + &apos;&quot; is already in progress (&apos; + ongoingSubUploadList[0].Progress__c * 2 + &apos;%) for this mailing list. Please try again in a few minutes.&apos;);
  } else if(overWriteFanData == true){
    openConfirmationModal();
  } else {
    processFile();
  }
}</url>
</WebLink>
