Managing your SFDX Data with Bulk Apex
SfDX and Record Types
In my previous article on Importing a complex data structure in Salesforce DX, I touched on the fact that Salesforce DX data tree import does not support the assignment of record types. I mentioned in my youtube video that you can address this with Apex and using patterns in the data. So that's what this article is about. We're going to use the same test data and schema, and build bulk apex jobs to change the record types.Experience Records
In the Resume, we break up Experiences into 3 record types: Professional, Educational, and Professional Development. The record types determine how they are displayed on the Resume pages. As you can see below, when we initially import the data, the record types are not correct and thus the resume is not properly formatted. Since Salesforce DX does not allow us to assign record types during the import, when we have large data volumes, this is not sustainable. So we need to look for patterns in the data to determine record types.There are 3 key fields that are used in the Page Layouts for each record type that can help dictate what Record Type to assign to a record.
- Job_Title__c - This field is only available on the Professional page layout and is only used for Jobs.
- Degree_Earned__c - This field is only available on the Educational page layout and is used to hold the degree earned.
- Course_Name__c - This field is only available on the Professional Development page layout as it only used for recording the course name.
Writing an Apex Bulk Class
NOTE: This code is now part of Resume Builder app on GithubWhile we don't have a massive amount of Test Data, I went with Bulk apex to make this easy. You could use Queueable apex and string the processes together to run other apex classes to update other object records.
Below is the code:
public class UpdateExperienceRTs implements Database.Batchable<sObject>, Database.Stateful { public Integer recordsProcessed = 0; public Database.QueryLocator start(Database.BatchableContext bc){ return Database.getQueryLocator( 'SELECT Id, Resume__c, RecordTypeId, Course_Name__c, Job_Title__c, Degree_Earned__c FROM Experience__c ORDER BY Resume__c' ); } public void execute(Database.BatchableContext bc, List<Experience__c> scope){ Id RecordTypeIdEdu = Schema.SObjectType.Experience__c.getRecordTypeInfosByName().get('Educational').getRecordTypeId(); Id RecordTypeIdPrDv = Schema.SObjectType.Experience__c.getRecordTypeInfosByName().get('Professional Development').getRecordTypeId(); Id RecordTypeIdProf = Schema.SObjectType.Experience__c.getRecordTypeInfosByName().get('Professional').getRecordTypeId(); List <Experience__c> experiences = new List<Experience__c>(); for (Experience__c experience : scope){ if(experience.Degree_Earned__c != null){ experience.RecordTypeId = RecordTypeIdEdu; experiences.add(experience); recordsProcessed = recordsProcessed + 1; } else if (experience.Course_Name__c != null){ experience.RecordTypeId = RecordTypeIdPrDv; experiences.add(experience); recordsProcessed = recordsProcessed + 1; } else{ experience.RecordTypeId = RecordTypeIdProf; experiences.add(experience); recordsProcessed = recordsProcessed + 1; } } update experiences; } public void finish(Database.BatchableContext bc){ System.debug(recordsProcessed + ' Experience records processed.'); } }
- In our Start method, we query All Experience records, and include the 3 fields we identified to determine the record type. We sort the records by the Resume "Master" record. This is important because if we had multiple Resumes, we could run into record lock issues if the apex attempted to process 2 experience records belonging to the same Resume.
- In our Execute method, we pass a list of all the experience records queried.
- We then use the getRecordTypeInfosByName method to get the Id for the 3 Record Types that we need to assign. We also define a list to store the record updates.
- We then create a for loop to process all the records in the list. In the loop, we use If-else statements to assign Education record type to records with the Degree_Earned__c field populated, or assign a Professional Development record type if the Course_Name__c is populated, otherwise assign the Professional record type.
- Once the For loop is complete, we then update all the experience records.
- In our Finish method, we just send a debug message to state the number of records that were processed.
Executing the Bulk Apex
With our Apex class defined, we now have to execute the class. This will update the records in a bulk process.UpdateExperienceRTs batchExperiences = new UpdateExperienceRTs();
Id batchId = Database.executeBatch(batchExperiences);
Summary
With complex data models, look at Page Layouts, Validation Rules, and any logic that is used to help determine a record type. You can then build apex to assign the Record Types.We used apex to modify record types instead of other methods because this is only required for DX data imports and don't want this unecessarily firing off during normal data use.
This comment has been removed by the author.
ReplyDeleteThe representation of this article is actually superb. I think this is a genuinely beneficial and instructive article for everyone, I appreciate this kind of writing. Thankful to you for sharing an article like this.Vietnam Export Data
ReplyDeleteBest Vietnam Trade data provider online in very cheap prices
ReplyDelete