Post on 01-Jul-2015
description
Taking Apex and Visualforce Above and Beyond
Jay Jayakumaran
Yahoo! Inc.
Senior Developer
Jay JayakumaranSenior Developer
Agenda – Building synchronous data loader using Apex and Visualforce
– Bulk edit using Apex and Visualforce
– Grand Children Design Pattern
– Building custom field track history using Apex and Visualforce
Building Synchronous data loader using Apex and Visualforce
Building synchronous data loader using Apex and Visualforce
• Why?– Give Standard Users insert, update, and deletion rights without extending them Admin rights
such as API
or Modify All Data.
– Existing desktop based tools require user training or users to rely on the admin for all data loads.
– The functionality is not available out of the box https://success.salesforce.com/ideaview?id=08730000000Bre6AAC
• What has Yahoo! done?– We have customized Data import tools to bulk insert/update 10,000 records in real time
synchronously.
Highlights of the Visualforce data loader Step by step wizard process for Data import
A Standard help template for the sObject to load the data
UI Preview of the csv to be imported/updated
Customized data import logic which parses a generic csv file and inserts Parent, Children, and Grandchildren in one single transaction
Advanced success and error records display with RollBack or finish options
Sending success and error records as csv files to email
Code can be reused if necessary by other teams with minimal changes
Visualforce data loader – Screen shots
STEP 1: Choose file
STEP 2: Preview file
STEP 3: View Success and Error records with
Rollback/Finish
Inserting Parent, Children, and Grand Children in one single transaction
Data Import tool(VF Page & Apex class)
Template sObject csv
Parent sObject Child sObject Grand Children sObject
Create Success and error files; and
send email
Update/Delete Operation
Data Import tool (Identifies records using record name)
sObject csv
Create Success and error files; and send email
Code walkthrough
Convert csv blob to String
Use DescribeFieldResult to get the API names of the field labels
Parse the String to form a collection of sObjects
Use Database methods to do insert or update so that we can handle success and error records
Demo - Building Synchronous data loader using Apex and Visualforce
Bulk Edit using Apex and Visualforce
Bulk Edit using Apex and Visualforce Why ?
Out of the Box list views in salesforce provides bulk edit in UI
Users have to create first list view of their choice or clone list views
User training required on how to customize list views and filter
Highlights Visualforce page for bulk edit
Real time filters and display of records meeting the criteria
Has all the features of the standard bulk edit
Error handling (Triggers and validation rules)
Demo - Bulk Edit using Apex and Visualforce
Grandchildren UI pattern
Grandchildren Pattern (User Navigation)– In Standard SFDC, drilling down on the Grand Children records from the parent record requires 3 pages.
– Accommodate the CRUD of children and Grand Children records in just one page
– Used generally in the Parent record Detail/View page
– Achieved through Radio Button related lists or Expand Collapse pattern– DEMO
Parent
Child
Grand Child
Custom Field Track History
Field Track History using Apex and Visualforce
Salesforce.com Field Track History limitations
Can track only 20 fields per sObject (need to contact SFDC support to increase)
Cannot record the value changes on MultiSelect Picklist and long text area fields
Cannot do data migration on Standard History tables
(We have to create custom history tables; data migrate legacy audit trail into custom history tables; Combine both the standard history data and custom history data into one List on UI).
Field Track History using Apex and Visualforce – Cont.
Create after insert, after update triggers on sObject
Track the value changes using Trigger.old and Trigger.New
Insert the history records into custom history tables
Sample code: // Save ContentLicense History for Multi-select picklist fields
//writing such code for every field on each sObject is not efficient and is repetitive consuming code lines
if(Trigger.isUpdate) {
List<ContentLicenseLegacyHistory__c> rhInsList = new List<ContentLicenseLegacyHistory__c> ();
for (ContentLicense__c rOld : Trigger.old) {
ContentLicense__c rNew = Trigger.newMap.get(rOld.Id)
if (rOld.Device__c != rNew.Device__c) {
rhinsList.add(new ContentLicenseLegacyHistory__c(parentid__c = rOld.id,field__c = 'Device__c',oldValue__c = rOld.Device__c, newValue__c = rNew.Device__c, CreatedDate__c = rNew.LastModifiedDate, CreatedById__c = rNew.CreatedById));
}
if (rOld.UserGeo__c != rNew.UserGeo__c) {
rhinsList.add(new ContentLicenseLegacyHistory__c(parentid__c = rOld.id,field__c = 'UserGeo__c',oldValue__c = rOld.UserGeo__c, newValue__c = rNew.UserGeo__c, CreatedDate__c = rNew.LastModifiedDate, CreatedById__c = rNew.CreatedById));
}
insert rhInsList;
Custom Field Track history - architectureApp. User Interface: (Visualforce, JavaScript, CSS, Apex class, and Config Tables)
Trigger and Generic Apex class framework: (Apex Trigger, Apex class, and Custom History tables)
Generic History UI Component ( Visualforce, Apex class)
Config Tables to store the sObject names and its fields to be
tracked
GUI config for sObject and fields to be tracked
Apex Trigger framework (after insert, update)
Generic Apex class FrameworkcreateHistory(sObjectName, FieldName, OldObjectCopy,
newObjectCopy)
History records
Insert field track history records with field old and new values into custom
history tables
Generic UI ComponentLoads the history records into standard pagelayouts
or sObject Visualforce Pages
Q&A