22
Taking Apex and Visualforce Above and Beyond Jay Jayakumaran Yahoo! Inc. Senior Developer

Taking Apex and Visualforce Above and Beyond

Embed Size (px)

DESCRIPTION

At Yahoo, our Salesforce developers are thinking 'above and beyond' to create innovative solutions with Apex and Visualforce. Join us as we discuss patterns for deep clone, mass and bulk edit, and walk through the data import wizard we built to allow our sales team to synchronously modify 10,000 records at a time.

Citation preview

Page 1: Taking Apex and Visualforce Above and Beyond

Taking Apex and Visualforce Above and Beyond

Jay Jayakumaran

Yahoo! Inc.

Senior Developer

Page 2: Taking Apex and Visualforce Above and Beyond

Jay JayakumaranSenior Developer

Page 3: Taking Apex and Visualforce Above and Beyond

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

Page 4: Taking Apex and Visualforce Above and Beyond

Building Synchronous data loader using Apex and Visualforce

Page 5: Taking Apex and Visualforce Above and Beyond

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.

Page 6: Taking Apex and Visualforce Above and Beyond

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

Page 7: Taking Apex and Visualforce Above and Beyond

Visualforce data loader – Screen shots

STEP 1: Choose file

STEP 2: Preview file

STEP 3: View Success and Error records with

Rollback/Finish

Page 8: Taking Apex and Visualforce Above and Beyond

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

Page 9: Taking Apex and Visualforce Above and Beyond

Update/Delete Operation

Data Import tool (Identifies records using record name)

sObject csv

Create Success and error files; and send email

Page 10: Taking Apex and Visualforce Above and Beyond

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

Page 11: Taking Apex and Visualforce Above and Beyond

Demo - Building Synchronous data loader using Apex and Visualforce

Page 12: Taking Apex and Visualforce Above and Beyond

Bulk Edit using Apex and Visualforce

Page 13: Taking Apex and Visualforce Above and Beyond

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)

Page 14: Taking Apex and Visualforce Above and Beyond

Demo - Bulk Edit using Apex and Visualforce

Page 15: Taking Apex and Visualforce Above and Beyond

Grandchildren UI pattern

Page 16: Taking Apex and Visualforce Above and Beyond

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

Page 17: Taking Apex and Visualforce Above and Beyond

Custom Field Track History

Page 18: Taking Apex and Visualforce Above and Beyond

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).

Page 19: Taking Apex and Visualforce Above and Beyond

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;

Page 20: Taking Apex and Visualforce Above and Beyond

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

Page 21: Taking Apex and Visualforce Above and Beyond

Q&A

Page 22: Taking Apex and Visualforce Above and Beyond