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...'); console.log('No token found. Initiating OAuth2 flow...');
// For command-line scripts where a browser can be opened: // For command-line scripts where a browser can be opened:
await sfdc.initToken((loginUrl) => { await sfdc.initToken((loginUrl) => {
console.log(`Please open this URL to authenticate: ${loginUrl}`); console.log(`Please open this URL to authenticate: ${ loginUrl }`);
}); });
} else { } else {
console.log('Token loaded successfully.'); console.log('Token loaded successfully.');
@@ -93,6 +93,8 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
conditions.push(`Name='${ escapeSoql(rawName) }'`); 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 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); const nameResult = await sfdc.query(nameSoql);
@@ -107,24 +109,21 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
console.log('DRY RUN: document not created'); console.log('DRY RUN: document not created');
} }
else { 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', { const newDocument = await sfdc.create('Documento__c', {
Account__c: `${ matchAccount.Id }`, Account__c: matchAccount.Id,
Name: 'Attestato di Formazione', Name: 'Attestato di Formazione',
Data_Caricamento__c: `${ nowIsoString }`, Data_Caricamento__c: nowIsoString,
Tipo_Documento__c: "Dossier di Tirocinio", Tipo_Documento__c: 'Dossier di Tirocinio',
Stato__c: 'Valido' Stato__c: 'Valido'
}); });
// this is the query that is needed to extract the Id of the client and assign it // ContentVersion linked directly to Documento__c
// to FirstPublishLocationId field const newContentDoc = await sfdc.create('ContentVersion', {
const soql2 = `SELECT Account__c FROM Documento__c WHERE Id='${ newDocument.id }'`; Title: `Attestato di Formazione - ${ matchAccount.Name } -`,
const docQuery = await sfdc.query(soql2); PathOnClient: `Attestato di Formazione_${ matchAccount.Name }.pdf`,
contentVersion = await sfdc.create('ContentVersion', { VersionData: base64Pdf,
Title: `${ matchAccount.Name }_Document.pdf`, FirstPublishLocationId: newDocument.id
PathOnClient: `${ matchAccount.Name }_Document.pdf`,
VersionData: base64Pdf, // Must be Base64 string
FirstPublishLocationId: `${ docQuery.records[0].Account__c }`
}); });
} }
} }