Bootstrapping the Force.com PHP Toolkit – Part 3

Img80_phpIn the last tutorial, we learnt how to enhance the Force.com PHP Toolkit by building a Contact Detail page and doing CRUD operations on the user interface for Contact records. In this post, we’ll look at how to import and export Contact records into a CSV file.

I added a ‘Export to CSV’ option on the Contact listview page as follows.

export

Clicking this will export all contact records into a CSV file.

Screen Shot 2016-06-04 at 1.36.32 PM

There are a couple of ways to do it – A) Fetch data from Salesforce using the SOAP API, store it in memory and dump it a CSV file B) Since we’re already showing the records on the user interface, just read it from there and dump it to a CSV file

For simplicity, I’m demonstrating the approach B) mentioned above.

Screen Shot 2016-06-04 at 1.38.52 PM

We read the data from the table and store it each row (delimited by new line) and each column (delimited by ,) as shown above.

Screen Shot 2016-06-04 at 1.41.00 PM

We then set the mime type and push all the the data to the CSV file as shown above.  For import, the user can chose any CSV file via this file chooser option.

Screen Shot 2016-06-04 at 1.42.03 PM

And selecting a file will insert all records into the Salesforce backend and show them in the list view as well. If there are any errors, it will show them in an alert.

We first try to read the file and validate the headers to determine if the file is valid or not.

Screen Shot 2016-06-04 at 1.45.09 PM

Once we’ve validated the headers, we read each row and each cell and then try to create a record for it as shown below.

Screen Shot 2016-06-04 at 1.47.30 PM.png

If there’s an error, we shown an alert. If all records are imported successfully, the list view is refreshed to show the newly added records as well.

This wraps up the import/ export capability that we added to the Force.com PHP Toolkit. The updated source code is available here.

I’ll polish up the code hopefully in the next week or so and publish it to my Github repo as well.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s