improvements in file.js and minor change in README

This commit is contained in:
2026-07-30 11:56:40 +02:00
parent 1e31e5bbba
commit 406d519a64
2 changed files with 21 additions and 22 deletions
+1 -1
View File
@@ -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.
This projects uses the ```sfdc2.js``` library which facilitates interaction with the Salesforce API.
+20 -21
View File
@@ -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!')