Manager for Salesforce Simple Tasks – Part I – Models and Interface
In this blog post series we are going to cover how we can connect to Salesforce to perform simple tasks. This first blog post will explain the models needed to accomplish theses tasks and the interface that we are going to implement which describes a little bit what you will be able to do with the manager. So without further due, lets begin.
First, we will create the models for the manager actions, starting for a Lead which will allow you to retrieve and create leads in Salesforce. Any field with a __c post fix is a custom field that must be added to Salesforce, we use Newtonsoft Json library to specify how the columns are named in Salesforce
public class Lead
{
[JsonProperty("Id")] public string Id { get; set; }
[JsonProperty("City")] public string City { get; set; }
[JsonProperty("Company")] public string Company { get; set; }
[JsonProperty("Country")] public string Country { get; set; }
[JsonProperty("Email")] public string Email { get; set; }
[JsonProperty("Engagement_Score__c")] public long EngagementScoreC { get; set; }
[JsonProperty("FirstName")] public string FirstName { get; set; }
[JsonProperty("Industry")] public string Industry { get; set; }
[JsonProperty("LastName")] public string LastName { get; set; }
[JsonProperty("LeadSource")] public string LeadSource { get; set; }
[JsonProperty("MiddleName")] public string MiddleName { get; set; }
[JsonProperty("MobilePhone")] public object MobilePhone { get; set; }
[JsonProperty("NumberOfEmployees")] public long NumberOfEmployees { get; set; }
[JsonProperty("OwnerId")] public string OwnerId { get; set; }
[JsonProperty("Phone")] public string Phone { get; set; }
[JsonProperty("PostalCode")] public string PostalCode { get; set; }
[JsonProperty("Profile_Category__c")] public string ProfileCategoryC { get; set; }
[JsonProperty("Rating")] public string Rating { get; set; }
[JsonProperty("Salutation")] public string Salutation { get; set; }
[JsonProperty("State")] public string State { get; set; }
[JsonProperty("Status")] public string Status { get; set; }
[JsonProperty("Street")] public string Street { get; set; }
[JsonProperty("Suffix")] public string Suffix { get; set; }
[JsonProperty("Title")] public string Title { get; set; }
[JsonProperty("Website")] public string Website { get; set; }
}
Once a lead is created, it can be transformed to a Contact, so that is the second class we will describe
public class Contact
{
[JsonProperty("Id")] public string Id { get; set; }
[JsonProperty("AccountId")] public string AccountId { get; set; }
[JsonProperty("Department")] public string Department { get; set; }
[JsonProperty("Email")] public string Email { get; set; }
[JsonProperty("Fax")] public string Fax { get; set; }
[JsonProperty("FirstName")] public string FirstName { get; set; }
[JsonProperty("LastName")] public string LastName { get; set; }
[JsonProperty("MailingCity")] public string MailingCity { get; set; }
[JsonProperty("MailingCountry")] public string MailingCountry { get; set; }
[JsonProperty("MailingPostalCode")] public string MailingPostalCode { get; set; }
[JsonProperty("MailingState")] public string MailingState { get; set; }
[JsonProperty("MailingStreet")] public string MailingStreet { get; set; }
[JsonProperty("MiddleName")] public string MiddleName { get; set; }
[JsonProperty("MobilePhone")] public string MobilePhone { get; set; }
[JsonProperty("Phone")] public string Phone { get; set; }
[JsonProperty("ReportsToId")] public string ReportsToId { get; set; }
[JsonProperty("Salutation")] public string Salutation { get; set; }
[JsonProperty("Suffix")] public string Suffix { get; set; }
[JsonProperty("Title")] public string Title { get; set; }
[JsonProperty("Engagement_Score__c")] public float EngagementScore { get; set; }
}
Next, we will create the Account model which will be used for later create opportunities
public class Account
{
[JsonProperty("Id")] public string Id { get; set; }
[JsonProperty("BillingCity")] public string BillingCity { get; set; }
[JsonProperty("BillingCountry")] public string BillingCountry { get; set; }
[JsonProperty("BillingPostalCode")] public string BillingPostalCode { get; set; }
[JsonProperty("BillingState")] public string BillingState { get; set; }
[JsonProperty("BillingStreet")] public string BillingStreet { get; set; }
[JsonProperty("Description")] public string Description { get; set; }
[JsonProperty("Industry")] public string Industry { get; set; }
[JsonProperty("Name")] public string Name { get; set; }
[JsonProperty("NumberOfEmployees")] public int NumberOfEmployees { get; set; }
[JsonProperty("ParentId")] public string ParentId { get; set; }
[JsonProperty("Phone")] public string Phone { get; set; }
[JsonProperty("ShippingCity")] public string ShippingCity { get; set; }
[JsonProperty("ShippingCountry")] public string ShippingCountry { get; set; }
[JsonProperty("ShippingPostalCode")] public string ShippingPostalCode { get; set; }
[JsonProperty("ShippingState")] public string ShippingState { get; set; }
[JsonProperty("ShippingStreet")] public string ShippingStreet { get; set; }
[JsonProperty("Type")] public string Type { get; set; }
[JsonProperty("Website")] public string Website { get; set; }
}
Then we will create the Opportunity model itself which is assigned to an Account
public class Opportunity
{
[JsonProperty("AccountId")] public string AccountId { get; set; }
[JsonProperty("Amount")] public string Amount { get; set; }
[JsonProperty("CampaignId")] public string CampaignId { get; set; }
[JsonProperty("CloseDate")] public string CloseDate { get; set; }
[JsonProperty("Description")] public string Description { get; set; }
[JsonProperty("LeadSource")] public string LeadSource { get; set; }
[JsonProperty("Name")] public string Name { get; set; }
[JsonProperty("NextStep")] public string NextStep { get; set; }
[JsonProperty("Probability")] public int Probability { get; set; }
[JsonProperty("StageName")] public string StageName { get; set; }
[JsonProperty("Type")] public string Type { get; set; }
}
We will also add, the task model which allows you to create tasks for a contact or lead that later can be handle by a business representative
public class Task
{
public string Id { get; set; }
public string WhoId { get; set; }
public string Subject { get; set; }
public string Status { get; set; }
public string Priority { get; set; }
public string ReminderDateTime { get; set; }
public string OwnerId { get; set; }
}
Finally, we will add some mixed models that will be used for different purposes. First, the lead or contact model which will check if an specific entity is a lead or contact
public class LeadOrContact
{
public Lead Lead { get; set; }
public Contact Contact { get; set; }
public bool Exist => Lead != null || Contact != null;
public bool IsLead => Lead != null;
public bool IsContact => Contact != null;
}
The lead status class will return in which stage the lead is.
public class LeadStatus
{
public string MasterLabel { get; set; }
public int SortOrder { get; set; }
}
The new account contact data model, will create a new lead or contact depending of the entity status with only the basic data
public class NewAccountContactData
{
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string Company { get; set; }
public string EngagementScore { get; set; }
}
And the last model which will allow you to assign a contact to an opportunity
public class OpportunityContactRole
{
[JsonProperty("ContactId")] public string ContactId { get; set; }
[JsonProperty("OpportunityId")] public string OpportunityId { get; set; }
}
Now, the interface for the manager is as follows
public interface ISalesforceManager
{
void Authenticate(IConfiguration configuration);
Task<string> CreateLeadAsync(NewAccountContactData accountData, int engagementScore);
Task<string> CreateObjectAsync(string objectName, object data);
Task<LeadOrContact> IsLeadOrContactAsync(string email);
Task<Account> GetAccountByNameAsync(string name);
Task<IList<LeadStatus>> GetAllLeadStatusAsync();
Task<Contact> GetContactByEmailAsync(string email);
Task<Contact> GetContactByLeadAndAccountAsync(Lead lead, Account account);
Task<Contact> GetContactByEmailAndAccountIdAsync(string email, string accountId);
Task<Account> GetAccountByEmailAndNameAsync(string email, string firstName, string lastName);
Task<Lead> GetLeadByEmailAsync(string email);
Task<LeadStatus> GetLeadStatusAsync(int position);
Task<bool> UpdateObjectAsync(string objectName, string objectId, object data);
Task UpdateSalesforceContactAsync(Contact sfContact, int? engagementScore = null);
Task<bool> UpdateUserData(string email, int engagementScore, string name = null, string lastname = null, bool transform = false, string phoneNumber = null);
Task<bool> VerifyUserOrCreateIt(string email, string name, string lastName);
Task<bool> CreateTaskAsync(string email);
Task<string> CreateOpportunityAsync(string email, string description);
Task<bool> AssignOpportunityToContact(string opportunityId, string contactId);
Task<bool> TransformLeadToContact(string leadId, Lead lead);
Task<bool> UpdateSalesforceDataAsync(string email, int? engagementScore = null, int? statusIndex = null,
NewAccountContactData accountData = null);
}
The manager will be able to authenticate with Salesforce, create and update leads, contacts, opportunities and tasks. We are using the library Force.com to implement these functionality in a dot net core solution. And that is it. You now have a basic structure that can interact with Salesforce. If you have any questions or suggestions please let me know in the comments. I hope this can help someone and as always keep learning !!!
2 COMMENTS