changes in attachPdfToAccount() function

This commit is contained in:
2026-07-31 16:15:37 +02:00
parent 2e6cf6a53c
commit 566e766d5c
+13 -14
View File
@@ -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
});
}
}