+ Start a Discussion
Hasini HasiniHasini Hasini 

Bit Urgent: CSV Upload using Batch Apex

Hi all,

We have the below requirement:

1. Create a Vf page and a button named 'Upload CSV' . The csv file contains lines/rows, each row contains 2 columns each of which is name of two custom objects.
For example, the csv file can contain below:

CustObj1Name, CustObj1Name2
TestName1, TestName2
Name1, Name2

2. On click of the button 'Upload CSV', a batch class should parse the csv and should upsert the two custom object records picking their respective name from csv. i.e. If we have CustObj1__c and CustObj2__c as custom objects, then these two custom object records shud be upserted by populating their name field from csv, say CustObj1__c.Obj1Name__c shud be populated with TestName1 and upsert CustObj1__c, similarly CustObj2__c.Obj2Name__c shud be populated with TestName2 and upsert CustObj2__c


Please let me know on the feasible solution for the above. If you have any sample code, please share which would be very helpful to me.



Thanks a lot!

James LoghryJames Loghry
Loading the data from a CSV file is tricky,  but sounds like you are on the right track.  You'll want a way to read a File in, then send the records to a batch process for loading the records in.  You're going to start running into View State issues though if you need to keep the records around for a preview functionality, for instance.  You'll also want to watch out for other governor limits including DML limitations along the way.  If you are still in the process of estimating the work, however, I would at least double if not triple your estimate for the work, as it will undoubtedly take longer than you would expect.
Surya KiranSurya Kiran
Hi Hasini,

By using custom iterable batch apex you can do this. I have done similar scenario to insert upto 1 lac records from visualforce page. As James said definitly it will take longer time for you, if you dont know the way you have to approach. But I can share the way I have done.

1. Read the file from visualforce page
2. Divided single file into multiple chunks by using java script and passed it to custom iterable batch apex.

If you still don't understand yet, let me know I can share sample code.
Hasini HasiniHasini Hasini
Hi James, 

Thanks for your reply. I didnt get the following:

You're going to start running into View State issues though if you need to keep the records around for a preview functionality, for instance.: What does this mean, I only want to insert records and do not want to display them in the Vf page, still I get issues? Pls correct me if I am wrong.

You'll also want to watch out for other governor limits including DML limitations along the way.: Could you please list few, if possible.


Thanks.


Hasini HasiniHasini Hasini
Hi Kiran,

Thanks for your reply.

2. Divided single file into multiple chunks by using java script and passed it to custom iterable batch apex.:  How to do this?

Please share sample code Kiran, it would be of great help for me.

Thanks.


Surya KiranSurya Kiran
Drop an email to - kirans.chintu@gmail.com. I can share the code. Chage it based on your requirement.
Raghu Rao 6Raghu Rao 6
Hi Surya,
We are implementing a similar solution where a user needs to attach a large csv file as an attachment and we need to first split the file into smaller chunks in order to bypass the DML limit of 10K and then process Apex batch execute method on these file chunks. Your above code implementation would be of tremendous help here. Have sent an email request for the same. Please do the needful.
Thanks