For users looking for display options beyond the Visualforce table, there are some custom options possible within Salesforce. Our objects and fields are completely flexible, and can be used in logic to populate custom fields in the Account or Lead object.
Populating these custom fields can be done by flow, trigger, or batch - and there are pros and cons to each option.
Due to the quantity of the data HG could be populating, we recommend the batch method. The batch method will avoid any simultaneous record update limits and some validation errors. By our assessment, is the most scalable and least disruptive, with the note that it is a scheduled operation.
Below are some examples of requested custom fields, and example batch logic that can be referenced during set up. Please note, this code is a reference and actual implementation will likely vary within your org.
Batch Logic for a Rollup Custom Field
The following code covers the use case where:
- You want to see your selected product installs for a record, populated in a single custom field and separated with a semi-colon.
global class PopulateAccountInstalledProductsBatch implements Database.Batchable<sObject>, Schedulable {
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, Name, Installed_Products__c, (select Id, HG_Insights__Product__c from HG_Insights__HGTechnographicToAccount__r) FROM Account WHERE HG_Insights__HG_Match_Status__c = \'Matched\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> records){
for (Account acc : records) {
acc.Installed_Products__c = '';
for (HG_Insights__HGTechnographic__c tech : acc.HG_Insights__HGTechnographicToAccount__r) {
acc.Installed_Products__c += !String.isEmpty(tech.HG_Insights__Product__c) ? tech.HG_Insights__Product__c + '; ' : '';
}
}
update records;
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
//schedulable method
global void execute(SchedulableContext sc) {
PopulateAccountInstalledProductsBatch batch = new PopulateAccountInstalledProductsBatch();
database.executebatch(batch);
}
}
Batch Logic for a Checkbox Custom Field
The following code covers the use case where:
- You want to create a checkbox field for a single product, where when installed it is automatically checked off.
global class PopulateAccountInstalledProductsBatch implements Database.Batchable<sObject>, Schedulable {
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, Name, Product1__c, Product2__c, Product3__c, (select Id, HG_Insights__Product__c from HG_Insights__HGTechnographicToAccount__r) FROM Account WHERE HG_Insights__HG_Match_Status__c = \'Matched\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> records){
for (Account acc : records) {
//reset values
acc.Product1__c = false;
acc.Product2__c = false;
acc.Product3__c = false;
for (HG_Insights__HGTechnographic__c tech : acc.HG_Insights__HGTechnographicToAccount__r) {
//update specific Product fields in case the product is installed
if (tech.HG_Insights__Product__c == 'Product1 Name') {
acc.Product1__c = true;
} else if (tech.HG_Insights__Product__c == 'Product2 Name') {
acc.Product2__c = true;
} else if (tech.HG_Insights__Product__c == 'Product3 Name') {
acc.Product3__c = true;
}
}
}
update records;
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
//schedulable method
global void execute(SchedulableContext sc) {
PopulateAccountInstalledProductsBatch batch = new PopulateAccountInstalledProductsBatch();
database.executebatch(batch);
}
}
Running a Batch out of the Scheduled Time
To manually run the batch:
- Open developer console
- Open Anonymous Window by pressing cmd+E/ctrl+E
- Run below piece of code (this will run the batch only once and it will start once you hit Execute):
Database.executeBatch(new PopulateAccountInstalledProductsBatch());
Comments
0 comments
Please sign in to leave a comment.