From 406d519a64efd8d0fe5f8ec8da5531e5ae964c84 Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Thu, 30 Jul 2026 11:56:40 +0200 Subject: [PATCH] improvements in file.js and minor change in README --- README.md | 2 +- file.js | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a216074..d840d11 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,4 @@ Before running this project, ensure you have: ## sfdc2.js -This projects uses the ```sfdc2.js```library which facilitates interaction with the Salesforce API. \ No newline at end of file +This projects uses the ```sfdc2.js``` library which facilitates interaction with the Salesforce API. \ No newline at end of file diff --git a/file.js b/file.js index 4fca466..6fce148 100644 --- a/file.js +++ b/file.js @@ -52,9 +52,9 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { let parser; try { let contentVersion; - for (let i = 0; i < tempFiles.length; i++) { + //for (let i = 0; i < tempFiles.length; i++) { // 1. Read the local PDF file and convert it to Base64 - const pdfBuffer = fs.readFileSync(tempFiles[i]); + const pdfBuffer = fs.readFileSync(tempFiles[0]); const base64Pdf = pdfBuffer.toString('base64'); parser = new PDFParse({ data: pdfBuffer }); try { @@ -89,8 +89,8 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { // use the soql command to search in the database // if codice fiscale is not available, I search the user's account based on their name and lastname - const soql = `SELECT Id, Name FROM Account WHERE (${conditions.join(' OR ')}) ORDER BY CreatedDate DESC LIMIT 1`; - const result_soql = await sfdc.query(soql); + const soql1 = `SELECT Id, Name FROM Account WHERE (${conditions.join(' OR ')}) ORDER BY CreatedDate DESC LIMIT 1`; + const result_soql = await sfdc.query(soql1); if (result_soql && result_soql.records && result_soql.records.length > 0) { // if we find something let matchFound = result_soql.records[0]; @@ -100,27 +100,26 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { console.log('DRY RUN: non creato documento'); } else { + const nowIsoString = new Date().toISOString(); // current timestamp in UTC + const newDocument = await sfdc.create('Documento__c', { + Account__c: `${ result_soql.records[0].Id }`, + Name: `Attestato - Sicurezza e Salute sul Lavoro -`, + Data_Caricamento__c: nowIsoString, + Tipo_Documento__c: "Stage doc", + 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); + console.log(`Doc Id: ${ newDocument.id }`); contentVersion = await sfdc.create('ContentVersion', { Title: `${ result_soql.records[0].Name }_Document.pdf`, PathOnClient: `${ result_soql.records[0].Name }_Document.pdf`, VersionData: base64Pdf, // Must be Base64 string - FirstPublishLocationId: `${ result_soql.records[0].Id }`// Automatically creates the link to the Account! + FirstPublishLocationId: `${ docQuery.records[0].Account__c }` }); - - //const accountName = result_soql.records[0].Name; - const nowIsoString = new Date().toISOString(); // current timestamp in UTC - const todayDate = new Date().toISOString().split('T')[0]; - const newDocument = await sfdc.create('Documento__c', { - Account__c: `${ result_soql.records[0].Id }`, - //Name: `Attestato - ${ accountName } - ${ todayDate }`, - Name: `Attestato - Sicurezza e Salute sul Lavoro -`, - Data_Caricamento__c: nowIsoString, - Tipo_Documento__c: "Dossier di Tirocinio", - Stato__c: 'Valido' - }); - - //console.log(`Name: ${ result_soql.records[0].Name }`); - //console.log(`Id: ${ result_soql.records[0].Id }`); } } else { @@ -132,7 +131,7 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) { await parser.destroy(); } } - } + //} console.log(`PDFs successfully attached!`); console.log('Dry run completed!')