'Android To Case' : Android App to log Salesforce Case

Saturday, May 21, 2011 by Aslam - The Alexendra
Hi All,
Most of you may know "Web To Case" functionality of Salesforce. I have used the same concept and reuse that idea to make simple Android App, which works exactly same as "Web to case", so i called this app as "Android To Case".

Here is some screen shot how it will look like:

1) Initial Screen:



2) After Submit below screen will come:




The code and app is simple. Here is the main code as below:

package com.androidsfdc.androidtocase;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidToCase extends Activity {
/** Called when the activity is first created. */
private static final String ORGID = "00D###0###0###G";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn
= (Button) findViewById(R.id.submit);

btn.setOnClickListener(
new View.OnClickListener() {

public void onClick(View v) {
try {
EditText name
= (EditText) findViewById(R.id.contactName);
EditText email
= (EditText) findViewById(R.id.email);
EditText phone
= (EditText) findViewById(R.id.phone);
EditText subject
= (EditText) findViewById(R.id.subject);
EditText description
= (EditText) findViewById(R.id.description);
StringBuffer params
= new StringBuffer();
params.append(
"orgid=" + URLEncoder.encode(ORGID, "UTF-8"));
params.append(
"&name="
+ URLEncoder.encode(name.getText().toString(),
"UTF-8"));
params.append(
"&email="
+ URLEncoder.encode(email.getText().toString(),
"UTF-8"));
params.append(
"&phone="
+ URLEncoder.encode(phone.getText().toString(),
"UTF-8"));
params.append(
"&subject="
+ URLEncoder.encode(subject.getText().toString(),
"UTF-8"));
params.append(
"&description="
+ URLEncoder.encode(description.getText()
.toString(),
"UTF-8"));
String output
= excutePost(
"https://www.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8",
params.toString());
name.setText(
"");
email.setText(
"");
phone.setText(
"");
subject.setText(
"");
description.setText(
"");
Toast.makeText(getBaseContext(),
"Case is successfully Created!!!",
Toast.LENGTH_LONG).show();

}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
}

public static String excutePost(String targetURL, String urlParameters) {
URL url;
HttpsURLConnection connection
= null;
try {
// Create connection
url = new URL(targetURL);
connection
= (HttpsURLConnection) url.openConnection();
connection.setRequestMethod(
"POST");
connection.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");

connection.setRequestProperty(
"Content-Length",
"" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty(
"Content-Language", "en-US");

connection.setUseCaches(
false);
connection.setDoInput(
true);
connection.setDoOutput(
true);

// Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd
= new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response
= new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append(
'\r');
}
rd.close();
return response.toString();

}
catch (Exception e) {

e.printStackTrace();
return null;

}
finally {

if (connection != null) {
connection.disconnect();
}
}
}
}


You only need to change "ORGID" with your organization id in the above code. And it will work for your developer/production org.

This application will be useful for such companies who want their customers to log cases directly from their Android mobiles natively. They can simply setup this application and ask their customers to install the app on their mobiles. And this is ready to use.

You can download the complete code from here:
http://www.aslambari.com/downloads/AndroidToCase.zip

Hope you like this app :) . Give me feedback what do you think.

Thanks
Aslam Bari

8 comments:

Anonymous said...

wowwwwwwwwww gr8 solution to use android for salesforce :-)

you are rock star.

akhilesh

Anonymous said...

Excellent Idea to use salesforce on Android.

Motyar said...

Hey good start..
keep it up.

:)

Venkata Narasimha Rao Vutla said...

Good work bayya

Abdul Vahid said...

Great Work Aslam :)

Mr.Pradip Kumbhar said...

Thanks a lot for this great solution!!!

Mr.Pradip Kumbhar said...

Can you please tell me whether we can fetch and update existing cases from android using some API as we have created new case in SFDC???

samsung said...

very nice work on it nice

Post a Comment