+ Start a Discussion
bujjibujji 

Batch Apex Job

Hi Guys,

 

My requirement is like every day i have to run an apex class in such a way that,

I have to fetch all the updated contacts from the salesforce and i have to put in a file in specific path.

So that datawarehousing guys can access.

 

Will it possible???? than how????

 

Can anybody solve my problem.(if possible send me the code)

 

it is very urgent please.

 

Thanks In advance.

Bujji.

ritika@developerforceritika@developerforce

Hi,

 

These are the things you can do -

 

1. Write a batch class to pick up Contacts by ModifiedDate , in the last 24 hours

2. Apex runs on the cloud, hence files cannot be written on a local system. However, you can either write into a Document object, or as an Attachment to another object.

 

If these work for you, let me know, I can help with the code to write Document / Attachment (as xml, csv etc)

bujjibujji

Thanks Rithika,

 

It will work really fine for me. But what the thing is these updated contacts has to access by the other

i.e., datawarehouse guys, so is there any possibility to give them this data on daily basis.

 

Can u tell me how to do it.

 

Thanks,

Bujji

ritika@developerforceritika@developerforce

Hi,

 

Where is the information suppose to be stored ?

 

- As a document within Salesforce ? 

- As a document within Salesforce, and send the document link ot attachment in email to different people ?

- Information need to be sent to some other server ? - You need to user web services API for this. You can schedule ApexD Data Loader CLI to run every night, to transfer the information to other server. Or there are other data managemen tools like CastIron performing these tasks.

 

Pleae clarify what and where is the output expected ? Datawarehousing people is an abstract term.

 

Regards,

Ritika

bujjibujji

Hi,

 

Actually the updated contacts on the daily basis should be stored in a database by other people.

So i have to give this data to him, so how can we do this.

 

My gmail id is " bajidssk@gmail.com " can u please me please sent invite so that we can discuss.

B'coz it is urgent requirement.

 

Thanks,

Bujji

ritika@developerforceritika@developerforce

Hello,

 

There are two things (in my knowledge) that you can do

 

1. Apex Data Loader Command Line Interface - By this, you can schedule daily jobs to extract data from salesforce and save the same. But the server needs to be running all the time (this is a desktop application, not cloud)

http://wiki.developerforce.com/page/Using_Data_Loader_from_the_command_line

 

2. Write an Apex Batch class and save the data in a document CSV file. - You can create a new file, or replace the existing one. But this will have limits to the amount of data you can write.

 

Please analyse the aboev methods for your purpose.

 

Regards,

Ritika

 

bujjibujji

Hi Rithika,

 

Finally we are in the right track, the 2nd approach is suitable for my situation. i.e.,

 

Write an Apex Batch class and save the data in a document CSV file. - You can create a new file, or replace the existing one. But this will have limits to the amount of data you can write.

 

Can u tell me how to do it. If possible please send me the code step by step so that it will be helpful.

 

Thanks,

Bujji

ritika@developerforceritika@developerforce

Hi,

 

As youa re aware there may be limits to the amount of data you can process using this approac, I'lls till go ahead with a sample here

 

A.      In the execute of the Batch -

 

Id folderId ; // Populate this Id with the Folder Id you want the document to be saved in

 

List<Contact> cons = (List<Contact>)scope;

String txtToInsert = 'Id, Name, Email, Birthdate' + '\n';

for(Contact con : cons){

    txtToInsert += con.Id + ',' + '\"' + con.Name +  '\"' +',' + con.Email + ',' + con.Birthdate + '\n';

}

txtToInsert = txtToInsert.substring(0, txtToInsert.length() -1 );
txtToInsert = txtToInsert.replaceAll('null', '');

 

Document doc = new Document (Name = 'contacts_Updated_'+DateTime.now().format() + '.csv');

doc.Description = ''; // Insert description

doc.FolderId = folderId;

doc.Body = Blob.valueOf(txtToInsert);

insert doc;

 

 

B.    Make sure the query for contacts (in start method of Batch class) contain all the fields you want to write in the CSV

C.    Make sure you query only records modified in like last 24 hours

D.    Make sure to give corrcet access to the Folder to writing document into it

 

Hope this helps

 

bujjibujji

Hi,

 

you said "folder id" it will store in the our local hard disk only write.

And can u please send me the complete code to my mail, b'coz i am new to apex coding.

Please could help me.

My email id is bajidssk@gmail.com

 

Thanks,

Bujji

ritika@developerforceritika@developerforce

Hi,

 

You're mistaken here. Apex cannot interact with your local system, its a technology that runs on the cloud. So there is a standard 'Document' object in Salesforce, where information could be written.

There has to be a folder defined in order to create the Document.

 

Please try the above code in a Batch Apex and let me know if there are any issues,.

 

 

bujjibujji

Hi,

 

Ok i got it. The file has to be stored in "document" object. But somebody has to access this update records.

How it can be done can suggest me any good procedure.

 

And i have created a visualforce page and i have some command links. Now my task is whenever i clicke

this command link a new window has to be openend with specified url. How can we do it.

 

Thanks,

Bujji

ritika@developerforceritika@developerforce

Hi,

 

The documents can be accessed by any of your users, provided they have the correct permissions for that folder. You can create a new user for the data warehousing profile and give them access to those documents you create.

 

For command link use "target" attribute with value "_blank" to open in a new window/tab.

 

bujjibujji

Hi,

 

you are not understanding, the updated data has to used by the outside users they should not use salesforce for that.

I got the code how to get the updated data and i put in a list.

 

Now i have to give this data to the external system, can tell how to do it.

wheather i should have to expose that class as webservice or what.

 

Please help me out.

 

Thanks,

Bujji

Jerun JoseJerun Jose

Hi,

 

Batch apex can only prepare the updated data from the Contacts object into some format in your Salesforce organisation. To make the data visible outside salesforce, you will have to use other means. Using webservices also has limitations on the size of data that can be transported.

 

From what I can see, command line data loader seems to be the best approach in your case. It can be scheduled every day to pull the updated contacts from your organisation and write into the external server. But as Ritika mentioned, Apex Data Loader is a client application. It needs a physical machine to run on which translates to additional costing and maintenance.

 

If you are certain that the size of the daily export will not be greater than 3MB, then you can use webservices.

 

Thanks,

 

Jerun Jose

bujjibujji

Hi Jose,

 

Thank you for your approach, Can u please tell me the script or code how to run dataloader from command line.

 

Thanks,

Bujji

Jerun JoseJerun Jose

Hi,

 

Sorry for the late response. Did not have network over the weekend.

 

Anyway, configuring the data loader to work with databases from the command line is not exactly a one step process.

 

I would recommend that you go thru these pages:

wiki.apexdevnet.com/index.php/Using_Data_Loader_from_the_command_line

and

http://www.developerforce.com/media/Cheatsheet_Setting_Up_Automated_Data_Loader_9_0.pdf

 

It mainly involves setting up two xml files: process-conf and database-conf

 

Thanks,

 

Jeurn Jose