Thursday, 8 August 2013

Batch Apex & CSV Parsing - I am Lovin'it !!


Situation :

I have a CSV File with 3 Fields Lead ID , Yes|No Field and Operator Name.
Without Using Data Loader I need my Data Entry Team to Upload this File  and Leads get updates with these values in the respective fields.



Solution :

Perhaps , Data Loader is the Mirror Cracking Solution. But you cannot get all the happiness at once.

Making the Data Entry team familiar with Data Loader and expecting that they will do correct is like expecting a lion not to eat you all because you are a Vegetarian.

So , Clear Cut My Process was turned up to Batch Apex CSV Parsing . Yes ! You Heard It Correct Batch Apex CSV Parsing .

So , I went to my Manager with this serving on Plate...

I will create a Visualforce Page with a Browse Button.
When a CSV File with Correct Format is Uploaded from there.It will call an controller which will call the Batch Process . The Batch will call a class which will insert records from the values which it gets from another class. Make Sense ?

His words "Sounds Delicious !! "

So , I started upon it .. Here are the ingredients

1.  Page -- BatchUpdateLeads

/* This will be the Visualforce Page with the Browse Button. /*

2. ctrlUpdateallrecFromCSV

/* This will be the Extension which will be called from the Page. /*

3. BatchProcessor

/* Called from the Extension, This is the Actual Batch Process. /*

4. BatchSObjectFeeder

/* This will be an Input for the Batch Apex in its Start Method. /*

5. ctrlAdddataFromCSV

/* This will be the called from the Batch Apex. "InsertValues"  Method will be called. Also , This is the class responsible for Saving the Records in Salesforce. /*
6. clsAdddataFromCSV

/* This is the Class called from ctrlAdddataFromCSV , The main process of this is to parse the excel sheet  . /*



Get the Code from Here ..
Disclaimer : This is a chain of classes as I respect OOP Concepts.


Hope it works well for you all !

Happy Batch Update !! :-)


Friday, 19 July 2013

The Story of 'ASC' & 'DESC' in SOQL


Recently , I was trying to implement the masterpiece of JavaScript & Jeff Douglas > Dynamic Search Functionality which was getting parameters and searching the records in real time.

I was doing well but then I stuck at a issue pointed by my MIS head stating that Sorting doesn't work for the Null records and I was asked to fix this so that whenever we sort on the basis of a column , the records with null values should not come over first , they should be placed in last.

I was sorting on the basics of Registration Number but I was not getting all the null records in first. It was killing me and my time.

I was using database.query to give get the records sorted when we click on label Registration Number:-

SELECT  Id, Reg_Num FROM Courses__c order by Reg_Num DESC.




So I carved out a path with the the best friend of all developers "Google".

Keyword : NULLS LAST

SELECT  Id, Reg_Num FROM Courses__c order by Reg_Num DESC NULLS LAST


Here is what I got the explanation...


The Default Sorting of NULLs is DBMS dependent. Some of them sort at the end and some at the beginning. Salesforce does it in beginning. So, only way to ensure this is to use NULL FIRST/LAST if the DBMS supports it.

In standard SQL (and most modern DBMS like Oracle, PostgreSQL, DB2, Firebird, Apache Derby, HSQLDB and H2) you can specify keyword NULLS LAST or NULLS FIRST.

The usage of isnull() or other functions is a workaround for the missing support for NULLS.

So , I wrote this down so that I could save someone's life and important time which he would spend while consulting the best buddy "Google".

Hope this writing helps you in the thing which you were looking for.

Happy Sorting !