Skip to content

Commit

Permalink
update salesforce helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eashaw committed May 20, 2024
1 parent ab3f111 commit 0e6f309
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
115 changes: 115 additions & 0 deletions api/helpers/salesforce/create-lead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module.exports = {


friendlyName: 'Create lead',// FUTURE: Retire this in favor of createTask()


description: 'Create a Lead record in Salesforce representing some kind of action Fleet needs to take for someone, whether based on a signal from their behavior or their explicit request.',


inputs: {

salesforceAccountId: {
type: 'string',
required: true,
description: 'The ID of the Account record that was found or updated by the updateOrCreateContactAndAccount helper.'
},
salesforceContactId: {
type: 'string',
required: true
},
leadDescription: {
type: 'string',
description: 'A description of what this lead is about; e.g. a contact form message, or the size of t-shirt being requested.'
},
leadSource: {
type: 'string',
required: true,
isIn: [
'Website - Contact forms',
'Website - Sign up',
'Website - Waitlist',
'Website - swag request',
],
},
primaryBuyingSituation: { type: 'string', isin: ['eo-it', 'eo-security', 'mdm', 'vm'] },
numberOfHosts: { type: 'number' },
},


exits: {

success: {
extendedDescription: 'Note that this deliberately has no return value.',
},

},


fn: async function ({salesforceAccountId, salesforceContactId, leadDescription, leadSource, primaryBuyingSituation, numberOfHosts}) {
require('assert')(sails.config.custom.salesforceIntegrationUsername);
require('assert')(sails.config.custom.salesforceIntegrationPasskey);
let jsforce = require('jsforce');

// login to Salesforce
let salesforceConnection = new jsforce.Connection({
loginUrl : 'https://fleetdm.my.salesforce.com'
});
await salesforceConnection.login(sails.config.custom.salesforceIntegrationUsername, sails.config.custom.salesforceIntegrationPasskey);

// Get the Contact record.
let contactRecord = await sails.helpers.flow.build(async ()=>{
return await salesforceConnection.sobject('Contact')
.retrieve(salesforceContactId);
}).intercept((err)=>{
return new Error(`When attempting to create a new Lead record using an existing Contact record (ID: ${salesforceContactId}), an error occurred when retreiving the specified record. Error: ${err}`);
});

// Get the Account record.
let accountRecord = await sails.helpers.flow.build(async ()=>{
return await salesforceConnection.sobject('Account')
.retrieve(salesforceAccountId);
}).intercept((err)=>{
return new Error(`When attempting to create a Lead record using an exisitng Account record (ID: ${salesforceAccountId}), An error occured when retreiving the specified record. Error: ${err}`);
});

let primaryBuyingSituationValuesByCodename = {
'vm': 'Vulnerability management',
'mdm': 'Device management (MDM)',
'eo-it': 'Endpoint operations - IT',
'eo-security': 'Endpoint operations - Security',
};

// If numberOfHosts or primaryBuyingSituationToSet was provided, set that value on the new Lead, otherwise fallback to the value on the contact record. (If it has one)
// Note: If these were not provided and a retreived contact record does not have this information, these values will be set to 'null' and are safe to pass into the sobject('Lead').create method below.
let numberOfHostsToSet = numberOfHosts ? numberOfHosts : contactRecord.of_hosts__c;
let primaryBuyingSituationToSet = primaryBuyingSituation ? primaryBuyingSituationValuesByCodename[primaryBuyingSituation] : contactRecord.Primary_buying_situation__c;

// Create the new Lead record.
await sails.helpers.flow.build(async ()=>{
return await salesforceConnection.sobject('Lead')
.create({
// Information from inputs:
Description: leadDescription,
LeadSource: leadSource,
Account__c: salesforceAccountId,// eslint-disable-line camelcase
Contact_associated_by_website__c: salesforceContactId,// eslint-disable-line camelcase
// Information from contact record:
FirstName: contactRecord.FirstName,
LastName: contactRecord.LastName,
Email: contactRecord.Email,
Website: contactRecord.Website,
of_hosts__c: numberOfHostsToSet,// eslint-disable-line camelcase
Primary_buying_scenario__c: primaryBuyingSituationToSet,// eslint-disable-line camelcase
LinkedIn_profile__c: contactRecord.LinkedIn_profile__c,// eslint-disable-line camelcase
// Information from the account record:
OwnerId: accountRecord.OwnerId
});
}).intercept((err)=>{
return new Error(`Could not create new Lead record. Error: ${err}`);
});
}


};

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module.exports = {


friendlyName: 'Update or create contact and account and create lead',


description: 'Updates or creates a contact and account in Salesforce, then uses the IDs of the created records to create a Lead record.',

extendedDescription: 'This is a wrapper for the update-or-create-contact-and-account and create-lead helpers used to run both of them in the background with timers.setImmediate().',

inputs: {

// Find by…
emailAddress: { type: 'string' },
linkedinUrl: { type: 'string' },

// Set…
firstName: { type: 'string', required: true },
lastName: { type: 'string', required: true },
organization: { type: 'string' },
primaryBuyingSituation: { type: 'string' },
psychologicalStage: {
type: 'string',
isIn: [
'1 - Unaware',
'2 - Aware',
'3 - Intrigued',
'4 - Has use case',
'5 - Personally confident',
'6 - Has team buy-in'
]
},
// For new leads.
leadDescription: {
type: 'string',
description: 'A description of what this lead is about; e.g. a contact form message, or the size of t-shirt being requested.'
},
leadSource: {
type: 'string',
required: true,
isIn: [
'Website - Contact forms',
'Website - Sign up',
'Website - Waitlist',
'Website - swag request',
],
},
numberOfHosts: { type: 'number' },
},

exits: {

success: {
extendedDescription: 'Note that this deliberately has no return value.',
},

},



fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage, leadSource, leadDescription, numberOfHosts}) {

let recordIds = await sails.helpers.salesforce.updateOrCreateContactAndAccount.with({
emailAddress,
firstName,
lastName,
organization,
linkedinUrl,
primaryBuyingSituation,
psychologicalStage,
});

await sails.helpers.salesforce.createLead.with({
salesforceContactId: recordIds.salesforceContactId,
salesforceAccountId: recordIds.salesforceAccountId,
leadDescription,
leadSource,
numberOfHosts,
});

return;
}


};

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ module.exports = {


fn: async function ({emailAddress, linkedinUrl, firstName, lastName, organization, primaryBuyingSituation, psychologicalStage}) {

require('assert')(sails.config.custom.salesforceIntegrationUsername);
require('assert')(sails.config.custom.salesforceIntegrationPasskey);
require('assert')(sails.config.custom.iqSecret);
Expand Down Expand Up @@ -87,13 +86,14 @@ module.exports = {
// 'LinkedIn_company_URL__c': enrichmentData.employer.linkedinCompanyPageUrl // TODO: if this information is not present on an existing account, nothing will be returned.
});
// console.log(existingAccountRecord);
if(existingAccountRecord && existingAccountRecord.OwnerId !== '0054x00000735wDAAQ') {
// If we found an exisitng account and it is not owned by the integrations admin or a disabled user, we'll use assign the new contact to the account owner.
if(existingAccountRecord && !['0054x00000735wDAAQ', '0054x0000086wsQAAQ'].includes(existingAccountRecord.OwnerId)) {
// Store the ID of the Account record we found.
salesforceAccountId = existingAccountRecord.Id;
salesforceAccountOwnerId = existingAccountRecord.OwnerId;
// console.log('exising account found!', salesforceAccountId);
} else {
// If we didn't find an existing record, or found one onwned by the integrations admin, we'll round robin it between the AE's Salesforce users.
// If we didn't find an existing record, or found one onwned by the integrations admin or a disabled user, we'll round robin it between the AE's Salesforce users.
let roundRobinUsers = await salesforceConnection.sobject('User')
.find({
AE_Round_robin__c: true,// eslint-disable-line camelcase
Expand Down

0 comments on commit 0e6f309

Please sign in to comment.