multiple accounts (with the same client's name) problem solved
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
const ExcelJS = require('exceljs');
|
||||
const { PDFParse } = require('pdf-parse');
|
||||
const { PDFDocument } = require('pdf-lib');
|
||||
const readline = require('node:readline/promises');
|
||||
const { stdin: input, stdout: output } = require('node:process');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
@@ -22,7 +20,7 @@ const authConfig = {
|
||||
|
||||
async function initializeSalesforceConnection() {
|
||||
// sfdc.setProductionBaseUrl(); // or sfdc.setSandboxBaseUrl();
|
||||
sfdc.setSandboxBaseUrl(); // Example: using sandbox
|
||||
sfdc.setProductionBaseUrl(); // Example: using sandbox
|
||||
sfdc.setTokenFile(authConfig.sfdcTokenFile);
|
||||
sfdc.setClientId(authConfig.sfdcClientId);
|
||||
sfdc.setClientSecret(authConfig.sfdcClientSecret);
|
||||
@@ -54,7 +52,6 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
let parser;
|
||||
try {
|
||||
let contentVersion;
|
||||
const rl = readline.createInterface({ input, output });
|
||||
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]);
|
||||
@@ -63,11 +60,17 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
try {
|
||||
const result = await parser.getText();
|
||||
let conditions = [];
|
||||
let matchAccount = null;
|
||||
|
||||
const userID = result.text.match(/Codice fiscale:\s*([A-Za-z0-9]+)/i);
|
||||
const codiceFiscale = userID ? userID[1].trim() : null;
|
||||
if (codiceFiscale) {
|
||||
conditions.push(`CF__c='${ codiceFiscale }'`);
|
||||
const cfSoql = `SELECT Id, Name, CF__c FROM Account WHERE CF__c='${codiceFiscale }' ORDER BY CreatedDate ASC LIMIT 1`;
|
||||
const cfResult = await sfdc.query(cfSoql);
|
||||
|
||||
if (cfResult && cfResult.records && cfResult.records.length > 0) {
|
||||
matchAccount = cfResult.records[0];
|
||||
}
|
||||
}
|
||||
|
||||
// since the name and lastname of the user are reversed in some files, we need to put it in order
|
||||
@@ -86,29 +89,30 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
conditions.push(`Name='${ escapeSoql(reversedName) }'`);
|
||||
}
|
||||
}
|
||||
conditions.push(`Name='${ escapeSoql(rawName) }'`);
|
||||
else {
|
||||
conditions.push(`Name='${ escapeSoql(rawName) }'`);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (nameResult && nameResult.records && nameResult.records.length > 0) {
|
||||
matchAccount = nameResult.records[0];
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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];
|
||||
if (matchAccount) { // if we find something
|
||||
console.log('--- Match found! ---');
|
||||
|
||||
if (isDryRun) {
|
||||
console.log('DRY RUN: non creato documento');
|
||||
}
|
||||
else {
|
||||
const title = await rl.question('Choose a title for the document: ');
|
||||
const nowIsoString = new Date().toISOString(); // current timestamp in UTC
|
||||
const newDocument = await sfdc.create('Documento__c', {
|
||||
Account__c: `${ result_soql.records[0].Id }`,
|
||||
Name: `${ title }`,
|
||||
Data_Caricamento__c: nowIsoString,
|
||||
Tipo_Documento__c: "Stage doc",
|
||||
Account__c: `${ matchAccount.Id }`,
|
||||
Name: 'Attestato di Formazione',
|
||||
Data_Caricamento__c: `${ nowIsoString }`,
|
||||
Tipo_Documento__c: "Dossier di Tirocinio",
|
||||
Stato__c: 'Valido'
|
||||
});
|
||||
|
||||
@@ -116,10 +120,9 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
// 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`,
|
||||
Title: `${ matchAccount.Name }_Document.pdf`,
|
||||
PathOnClient: `${ matchAccount.Name }_Document.pdf`,
|
||||
VersionData: base64Pdf, // Must be Base64 string
|
||||
FirstPublishLocationId: `${ docQuery.records[0].Account__c }`
|
||||
});
|
||||
@@ -128,6 +131,8 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
else {
|
||||
console.log('Account non trovato su Salesforce!');
|
||||
}
|
||||
console.log(`Name: ${ matchAccount.Name }`);
|
||||
console.log(`Name: ${ matchAccount.Id }`);
|
||||
}
|
||||
finally {
|
||||
if (parser) {
|
||||
@@ -137,7 +142,6 @@ async function attachPdfToAccount(sfdc, tempFiles, isDryRun = false) {
|
||||
}
|
||||
console.log(`PDFs successfully attached!`);
|
||||
console.log('Dry run completed!');
|
||||
rl.close();
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error uploading PDF: `, error);
|
||||
|
||||
Reference in New Issue
Block a user