The number of visits to your website by your potential client in a given timeframe can be used to set a quality rating for such a client. Leadfeeder does use this rating in the Leadfeeder web application. However, if you want to set this up in your Salesforce, you might have to follow this workflow created by Nadia Rehman.
Please execute this code first in a sandbox environment or test it in a non-production environment prior to pushing it online. We also recommend that you ask your Salesforce administrator/developer to help you with this setup.
In order to set up a quality rating in your Salesforce you will need three things:
1) Standard account rating field in your Salesforce (or a custom field if you already use the standard rating field for something else),
2) Integration between Leadfeeder and Salesforce, latest Leadfeeder Salesforce package installed and receiving visits from Leadfeeder in Salesforce, and
3) Set up the apex code and Flow in Salesforce so that it runs daily right before midnight so that it is easy to count the quality rating.
This article assumes that you have already sections 1) and 2) working, so we will focus on section 3) now.
a) Open the developer console:
b) Create a CalculateAccountRating.apxc Class and include the following code:
public class CalculateAccountRating {
@InvocableMethod(label='CalculateAccountRating' description='This will Calculate Account Rating baased on website visits' )
public static Void CalculteTodaysVisits() {
//Get all Acc ID's
List <Account> AllRelatedAccounts = New List <Account> ([SELECT ID, Rating FROM Account]);
//Get all Visits
List <leadfeederapp__WebsiteVisit__c> AllWebVisits = New list <leadfeederapp__WebsiteVisit__c> ([SELECT Id, leadfeederapp__Account__c, Name FROM leadfeederapp__WebsiteVisit__c] );
Date TodayDate = Date.today();
System.debug('Date is : ' + TodayDate);
//Bulk DML Account Rating
List<Account> Accs = New List <Account> ();
//Map to show number of Related Visits
Map <ID, Integer> AccRelatedWVsMap = New Map <ID, Integer>();
//Temporary variable to hold name
String WVName;
Date WVNameDate;
//Create a list to hold all the dates
List <Date> FullDates = New List <Date>();
//Loop over the Accounts
For (Account Acc : AllRelatedAccounts){
//Clear Related Visits List
FullDates.clear();
System.debug('This should be be clear : ' + FullDates );
//Loop over the Related Visits
For(leadfeederapp__WebsiteVisit__c WV : AllWebVisits ){
If(Acc.id == WV.leadfeederapp__Account__c ){
//Split the name to leave the dates only visits and add the dates to a list
WVName = WV.Name.Split('on').get(1);
WVName = WVName.Split('at').get(0);
System.debug('This should be date from name field = ' + WVName);
//Format the date from the site visit name to a date
WVNameDate = date.valueOf(WVName);
System.debug('Value of formatted name date is :' +WVNameDate);
//If the date is the same dates today then add to a list
If (WVNameDate == Date.today()){
FullDates.add(date.valueOf(WVName));
}
}
}
System.debug('This is the size of Dates Variable : ' + FullDates.size() );
//If clause to update the Rating field on Account
//See the size of the account visits (amount of visits)
If(FullDates == null || FullDates.Size() <= 3){
Acc.Rating = 'Cold';
}
Else If(FullDates != null && FullDates.Size() <= 8){
Acc.Rating = 'Warm';
}
Else If(FullDates != null && FullDates.Size() >= 9){
Acc.Rating = 'Hot';
}
Accs.add(Acc);
}
Update Accs;
}
}
NOTE: If you are using a Salesforce custom field, please exchange the word 'Rating' with your custom field's API name.
c) Next create a test Class CalculateAccountRatingTest.apxc and include the following code:
@IsTest public class CalculateAccountRatingTest{
@IsTest public static Void CalculateColdRating(){
String StrDate = Datetime.now().format('yyyy-MM-dd');
Account Acc = New Account() ;
Acc.Name = 'Test101010';
Insert Acc;
leadfeederapp__WebsiteVisit__c WV = New leadfeederapp__WebsiteVisit__c();
WV.Name = 'Visited on' + StrDate + 'at 00:00:00';
System.debug('Name is : ' + WV.Name);
WV.leadfeederapp__ExternalID__c = '1010101';
WV.leadfeederapp__Account__c = Acc.id;
Insert WV;
CalculateAccountRating.CalculteTodaysVisits();
Acc = [SELECT ID, Rating FROM Account WHERE ID = : ACC.Id Limit 1];
System.assertEquals('Cold', Acc.Rating);
System.debug('Rating should be cold: ' +Acc.Rating);
}
@IsTest public static Void CalculateColdRating2(){
List <Account> Accs = New List <Account>();
Account Acc = New Account() ;
Acc.Name = 'Test101010';
Acc.Rating = 'Hot';
Insert Acc;
CalculateAccountRating.CalculteTodaysVisits();
Acc = [SELECT ID, Rating FROM Account WHERE ID = : ACC.Id Limit 1];
System.assertEquals('Cold', Acc.Rating);
System.debug('Rating should be cold: ' +Acc.Rating);
}
@IsTest public static Void CalculateWarmRating(){
Account Acc = New Account() ;
Acc.Name = 'Test101010';
Insert Acc;
String StrDate = Datetime.now().format('yyyy-MM-dd');
List <leadfeederapp__WebsiteVisit__c> WVList = New List <leadfeederapp__WebsiteVisit__c>();
Integer Counter = 0;
Do{
leadfeederapp__WebsiteVisit__c WV = New leadfeederapp__WebsiteVisit__c();
WV.Name = 'Visited on' + StrDate + 'at 00:00:00' +Counter.format();
System.debug('Name is : ' + WV.Name);
WV.leadfeederapp__ExternalID__c = '1010101' + Counter.format();
WV.leadfeederapp__Account__c = Acc.id;
WVList.add(WV);
Counter++;
} While (Counter < 7);
System.debug('This many records in the list : ' + WVList.size());
Insert WVList;
CalculateAccountRating.CalculteTodaysVisits();
Acc = [SELECT ID, Rating FROM Account WHERE ID = : ACC.Id Limit 1];
System.assertEquals('Warm', Acc.Rating);
System.debug('Rating should be Warm: ' +Acc.Rating);
}
@IsTest public static Void CalculateHotRating(){
Account Acc = New Account() ;
Acc.Name = 'Test101010';
Insert Acc;
String StrDate = Datetime.now().format('yyyy-MM-dd');
List <leadfeederapp__WebsiteVisit__c> WVList = New List <leadfeederapp__WebsiteVisit__c>();
Integer Counter = 0;
Do{
leadfeederapp__WebsiteVisit__c WV = New leadfeederapp__WebsiteVisit__c();
WV.Name = 'Visited on' + StrDate + 'at 00:00:00' +Counter.format();
System.debug('Name is : ' + WV.Name);
WV.leadfeederapp__ExternalID__c = '1010101' + Counter.format();
WV.leadfeederapp__Account__c = Acc.id;
WVList.add(WV);
Counter++;
} While (Counter < 10);
System.debug('This many records in the list : ' + WVList.size());
Insert WVList;
CalculateAccountRating.CalculteTodaysVisits();
Acc = [SELECT ID, Rating FROM Account WHERE ID = : ACC.Id Limit 1];
System.assertEquals('Hot', Acc.Rating);
System.debug('Rating should be Warm: ' +Acc.Rating);
}
}
NOTE: If you are using a Salesforce custom field, please exchange the word 'Rating' with your custom field's API name.
d) In your Salesforce, click "Setup", find "Process Automation" and then open "Flows"
Click "New Flow" and then in the newly open window choose "Scheduled-Triggered Flow" and click create.
Set schedule - use yesterday's date, time 23:45 and "daily" frequency and click "Done".
e) Click plus sign, and choose "Action". In the New Action find the "apex" code you created, name it and label it.
f) Save the flow under a label and name, and then activate to make it run.
g) Run the test and check what is your code coverage. You will need at least 75% for production.
h) Wait for the visits to roll in and see if your client account is Cold, Warm, or Hot :-)
The account rating will be updated daily based on this logic (you can change the logic in the above code under b) depending on your own liking):
Cold <= 3 visits
Warm <= 8 visits
Hot >= 9 visits
NOTE: Please remember that Leadfeeder visits will be sent to your Salesforce daily in the morning for the previous day and then this flow will run, ie. visits for January 1st will be sent to your Salesforce on January 2nd and you will see the quality rating on January 3rd for visits on January 1st.