From 566e766d5cd1626924063aa2577601f601e6fa7f Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Fri, 31 Jul 2026 16:15:37 +0200 Subject: [PATCH] changes in attachPdfToAccount() function --- file.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/file.js b/file.js index fcfb243..0bea12a 100644 --- a/file.js +++ b/file.js @@ -30,7 +30,7 @@ async function initializeSalesforceConnection() { console.log('No token found. Initiating OAuth2 flow...'); // For command-line scripts where a browser can be opened: await sfdc.initToken((loginUrl) => { - console.log(`Please open this URL to authenticate: ${loginUrl}`); + console.log(`Please open this URL to authenticate: ${ loginUrl }`); }); } else { console.log('Token loaded successfully.'); @@ -93,6 +93,8 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { conditions.push(`Name='${ escapeSoql(rawName) }'`); } + // in this query, we prioritize the accounts that have the CF (e.g. if a client has two accounts and in one of them + // they don't have the CF but on the other one they do, we consider the latter) const nameSoql = `SELECT Id, Name, CF__c FROM Account WHERE (${conditions.join(' OR ')}) ORDER BY CF__c DESC NULLS LAST, CreatedDate ASC LIMIT 1`; const nameResult = await sfdc.query(nameSoql); @@ -107,24 +109,21 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { console.log('DRY RUN: document not created'); } else { - const nowIsoString = new Date().toISOString(); // current timestamp in UTC + const nowIsoString = new Date().toISOString(); // current timestamp in UTC const newDocument = await sfdc.create('Documento__c', { - Account__c: `${ matchAccount.Id }`, + Account__c: matchAccount.Id, Name: 'Attestato di Formazione', - Data_Caricamento__c: `${ nowIsoString }`, - Tipo_Documento__c: "Dossier di Tirocinio", + Data_Caricamento__c: nowIsoString, + Tipo_Documento__c: 'Dossier di Tirocinio', Stato__c: 'Valido' }); - // this is the query that is needed to extract the Id of the client and assign it - // to FirstPublishLocationId field - const soql2 = `SELECT Account__c FROM Documento__c WHERE Id='${ newDocument.id }'`; - const docQuery = await sfdc.query(soql2); - contentVersion = await sfdc.create('ContentVersion', { - Title: `${ matchAccount.Name }_Document.pdf`, - PathOnClient: `${ matchAccount.Name }_Document.pdf`, - VersionData: base64Pdf, // Must be Base64 string - FirstPublishLocationId: `${ docQuery.records[0].Account__c }` + // ContentVersion linked directly to Documento__c + const newContentDoc = await sfdc.create('ContentVersion', { + Title: `Attestato di Formazione - ${ matchAccount.Name } -`, + PathOnClient: `Attestato di Formazione_${ matchAccount.Name }.pdf`, + VersionData: base64Pdf, + FirstPublishLocationId: newDocument.id }); } }