-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
} | ||
|
||
|
||
}; | ||
|
86 changes: 86 additions & 0 deletions
86
api/helpers/salesforce/update-or-create-contact-and-account-and-create-lead.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters