Sunday, 24 March 2013

Mobile Application Development On Force.com Touch Platform-Part 1

MobilePhone and Tablets  are  very important aspect of life and the need for Mobile Applications are rising continuously. Salesforce force.com platform we all know is a very strong application development platform where variety of applications can be build in short span .

Along with the web based application customers frequently look for equivalent mobile applications that can enhance there business .A common example is the need of a mobile application for a field service agent to locate the customer location and resolve the case  or an application for a sales rep to maintain and calculate his inventories or generate quotes .

Out-Of-Box Mobile Application Provided By Salesforce and there advantages and disadvantages :

1)Salesforce Touch:

Salesforce Touch is an excellent application the salesforce provides for free.The User record has a checkbox to identify the user as Touch User .

Where it Lacks:

1)Presently this application runs well on IOS platform and one can download from App exchange .No support for Android yet .(Cross platform support is not available as on the date i wrote this article)
2)No support for Offline .
3)Reports and dashboards are not available .
4)There are list of objects that are not supported yet.


2)Salesforce Mobile :

This is an excellent add on and available straight out of box for free.There are little manual configuration steps involved to get it up and running.

The above guide from salesforce gives indepth knowledge on configuraion and its usage .

Currently there is support for Blackberry,IOS ,Android ,Windows,Etc (Please refer salesforce guide to explore more).


Features:

1)Support for offline available

2)Supports cross platform


Where it Lacks:


1)Salesforce has stopped adding additional features to this .(As per winter 13 guide )
2)Running on Ipad and tablets not compatible(One has to switch the Ipad to Iphone compatibility mode to run this properly )
3)Limited support for dashboards and Reports .


The above small limitations opens the door for need for developers and for developers its vital to learn the Salesforce SDK .SalesforceSDK is free source and we will discuss in detail in my next post.

Differences Between Salesforce Touch and Touch Platform

There are confusions frequently around this .

Salesforce Touch:This refers to touch application provided by Salesforce.com .This can be obtained for free from app store 

Salesforce Touch Platform:This refers to the platform salesforce allows for the developers to develop the mobile application using force.com components like apex,visualforce and Salesforce SDK .I will further elaborate more on my next post.

Hope you enjoyed Reading!!Feel free to leave a comment below for suggestions ,critics and feedback! 


Thursday, 21 February 2013

Salesforce and QR server integration for QR code generation in Salesforce

QR codes are very helpful in emails or business cards and often we get requirements in mobile application or a mail send to lead from salesforce to contain these QR code .

There has been lot of talks and debates on usability of this technology .Infact google has deprecated the API. Nevertheless some customers still prefer this technology and we often find QR codes in business cards ,front page of books ,Quotes and so on.

So this blog post demonstrates how to form the QR code in salesforce with the help of the qrserver

Please make sure to read the terms and conditions of the qrserver.com before using the QR code API for commercial  purpose .

There is already a good blog post from one of the MVP on this and hence it was very simple task for me to read and follow.Here is the link to the blog and it was nicely written.
http://www.tgerm.com/2012/11/salesforce-qr-code-visualforce-contact.html

The above blog used google chart API but since google have deprecated this API ,i had to look for the other websites providing me the API.

There were couple of good websites .

1)http://qrserver.com/api/documentation/create-qr-code/
2)http://www.sparqcode.com/static/QRGenAPI

In this blog i will be describing how to use formula field to generate the QR code using the qrserver.com API .The procedure remains same as we use with google chart API as demonstrated in the blog.

My assumptions are i have employee custom object and i have a formula field named QRCode and remote site settings has been configured for the http://qrserver.com/ .

so here is the formula using IMAGE function of the salesforce on how to bring the QR code from the qrserver.com

IMAGE('http://api.qrserver.com/v1/create-qr-code/?data='+ Name ,'hello',50,50)

Make sure to specify URL in small caps in image tag.For the readability purpose i have made all caps and in actual those are in small caps


data field consists of the content that will be displayed once the QR code is scanned by the reader.

Please note how the variable Name is used .Actually its  a merge field used from the formula editor and hence for each record this is dynamically populated .

50-Height of the image in pixel

50-Width of the image in pixel.

'hello '-string that will be displayed in case image fails to renders 



The QR image can be easily scanned with QR readers.There are number of readers available for mobile and the desktop.

I downloaded a QR code reader for my desktop from the URL below and works great .


From the screen the reader automatically scans the QR code and decodes the Text in it .

A small hint incase you need this image on the visualforce page here is the code to help

Hope the above information is helpful for people looking for sample approach on QR code generation in salesforce.





                                        

Saturday, 2 February 2013

Problem with WSS or WSP headers in WSDL while using WSDL-APEX

WSDL to APEX is really a good feature to Support SOAP API callouts to the external system in force.com.

Although this feature provides us the stub classes for us to make callout usually its not simple to parse the WSDL to the apex classes .Reason being this out-of-box feature has numerous limitation and as a technical architect or an integration specialist must be aware of these limitations

Here is the link to the document where the limitations are documented in sfdc

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex.htm

I came across a situation of late where i was able to parse the WSDL and generate the API classes still i was not able to make callout with exception saying authentication failed .

For authentication its usually best to use HTTP headers and even WSDL to apex does support addition of HTTP headers .To understand this again the above link is very helpful.

My problem was more weird i had authentication headers in my WSDL and it had tags using WSS and WSP.

What are actually WSP and WSS?

https://wiki.servicenow.com/index.php?title=SOAP_Web_Service
http://docs.oracle.com/cd/E23943_01/web.1111/e13750/sec_assert.htm

I went to the link above and read the document of oracle and its a security protocol for webservices .To deep dig above two links are great.

Coming to my problem again

1)If salesforce WSDL>apex dont support then i should get error while parsing (WSDL parsed out with no issues )
2)How can i make changes to my generated classes if i need to add and make this work?

Thanks to stackexchange.I quickly posted and tweeted to sandeep,Patt and other evangelism at salesforce who can help me

http://salesforce.stackexchange.com/questions/7587/forming-soap-header-through-apex-class-if-the-wsdl-provided-has-no-information-r

Thankfully Sandeep Bhanot replied saying WSDL>APEX don't support this yet .

So we have only one alternative left is to make a HTTP callout instead by dynamically constructing the XML with SOAP headers .

The above is only an alternative and must be adapted when no other choice left .

Best approach is still look whether the server programming at the other end can be adjusted to use simple HTTP authentication then having authentication in SOAP header.

Hope this helps !Enjoy Integrating with salesforce!


Saturday, 26 January 2013

Salesforce System Error: 1148877694-2467 (681892207) (681892207) when using JSON generator class in apex

Today i encountered a very interesting error when i was using JSON generator .The error said FATAL_ERROR System.UnexpectedException: Salesforce System Error: 1148877694-2467 (681892207) (681892207).

Looking into the error my first thing was to run through my code again and again .I referred the following documents on JSON generator class 



The code in my eyes look pretty simple and here is the exact snippet that was throwing the exception

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeNumberField('abc', 1.21);
gen.writeStringField('def', 'xyz');
String pretty = gen.getAsString();//This statement use at the end only .It ends the JSON 
System.debug('STRING JSON'+pretty);
gen.writeStartObject();
gen.writeStringField('def', 'xyz');
gen.writeEndObject();

After the line 6 (where i am printing the log )i have the following statement in my debug log 13:24:03:048 FATAL_ERROR System.UnexpectedException: Salesforce System Error: 1148877694-2467 (681892207) (681892207)

I decided to read the document again and discovered that the when i use the method getAsString method of JSON generator class the apex automatically closes the JSON and hence attempting to start writing the Object after closing generates an error .

Though this is simple issue but the error message is not caught properly in the salesforce backend .

So the conclusion is always using getAsString of JSON generator class should be practiced .

Never use it even for the debug statements.

This is one of the rare cases where error message is misleading.Hope this helps for people trying to google and find why the error is occuring when using JSON generator.

Just commmenting Line 5 and line 6 will resolve the issue.
Happy Coding !

Tuesday, 22 January 2013

JSON.serialize in APEX after launch of API 27.0(Spring 13)


These days i spend lot of time on Stackexchange  for salesforce.This has been amazing site for salesforce queries related to admin or development .

Very interesting changes after API 27.0 was launched in JSON.serialize system method of salesforce .This method is used to convert the apex objects (primitive or User defined ) into the JSON string format.Quite helpful in integration if structure of JSON is pretty simple then forming apex class and using JSON.serialize helps to get the JSON body for making the POST call to the external system.

There are some changes on how this behaves after launch of the Spring 13 API.Here is the reference to the stackexchange links from where i gathered this information and thought of blogging so that it may help others.

http://salesforce.stackexchange.com/questions/6192/json-serialize-method-not-returning-null-fields

http://salesforce.stackexchange.com/questions/7493/json-serialize-is-not-working-in-winter-13

So i thought of experimenting on this with the small test code

Here is the code

The first query pulls an account that has fax ,website field with no data .In short these are null for the Account thats considered in the below example

public class dummy{

    public static void dummytest(){
    Account acc= [Select Name, id, Fax, Website from account where id ='001Z000000J0w5C'];
    system.debug('JSON OF OBJECT FROM SALESFORCE SCHEMA'+JSON.Serialize(acc));
    
    dummywrapp dmy= new dummywrapp();
    dmy.Name='Test';    
    System.debug('JSON FROM APEX CREATED OBJECT'+JSON.serialize(dmy));
    }
    
    public class dummywrapp{
    
    public string Name;
    public string Location;
    
        }

}


DEBUG LOG
_________



21:23:17:122 USER_DEBUG [5]|DEBUG|JSON OF OBJECT FROM SALESFORCE SCHEMA{"attributes":{"type":"Account","url":"/services/data/v26.0/sobjects/Account/0019000000KANkZAAX"},"Name":"Mobile Test","Id":"0019000000KANkZAAX"}


21:23:17:124 USER_DEBUG [9]|DEBUG|JSON FROM APEX CREATED OBJECT{"Name":"Test","Location":null}


The following conclusion can be drawn looking at the debug log.

 When using JSON.serialize with objects bounded to apex schema if the field has blank values the KEY and Value in output of JSON is ignored While same does not hold good for objects formed through class formation from apex. 

 Not sure if in future salesforce changes this but certainly this information is good to know when we are integrating any system that uses JSON to communicate and if JSON.serialize is the mechanism to generate the JSON.

 Play safe with JSONserialize() method !

References

http://salesforce.stackexchange.com/questions/6192/json-serialize-method-not-returning-null-fields

Saturday, 12 January 2013

TimeZone Methods of Spring 13(API 27.0 ) release of salesforce


The timezone handling in apex was not so elegant and as a developer we had difficult times converting the GMT time zone to the locale time zone in apex language
The below code snippet demonstrates the earlier technique i had to use to convert the GMT time into the local time of the user
String timeZone = [select timeZoneSidKey from User where id=:userinfo.getUserId()].timeZoneSidKey;
System.debug('TIME ZONE '+timeZone);
Datetime dateGMT=Datetime.newInstanceGmt(2013,01,12,01,12,0);//Form the GMT time and date in sfdc
String formatteddate=dateGMT.format('yyyy-MM-dd HH:mm:ss',timeZone);//format the GMT into the Local according to his TimeZone Key
System.debug('LOCAL  TIME ....'+Datetime.valueOfGmt(formatteddate));

Debug Log
02:55:43.119 (119313000)|USER_DEBUG|[2]|DEBUG|TIME ZONE America/Los_Angeles
02:55:43.119 (119662000)|VARIABLE_ASSIGNMENT|[3]|dateGMT|"2013-01-12T01:12:00.000Z"
02:55:43.120 (120138000)|USER_DEBUG|[5]|DEBUG|LOCAL  TIME ....2013-01-11 17:12:00

debug Log analysis
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
Thanks to the spring 13 apex feature .No more i need query to find the TimeZoneSidKey Here is how i will structure my new code with the help of new time Zone methods
String timeZone = UserInfo.getTimeZone().getID();
System.debug('TIME ZONE'+timeZone);
Datetime dateGMT=Datetime.newInstanceGmt(2013,01,12,01,12,0);//Form the GMT time and date in sfdc
String formatteddate=dateGMT.format('yyyy-MM-dd HH:mm:ss',timeZone);//format the GMT into the Local according to his TimeZone Key
System.debug('LOCAL  TIME ....'+Datetime.valueOfGmt(formatteddate));

Debug Log
02:55:43.119 (119313000)|USER_DEBUG|[2]|DEBUG|TIME ZONE America/Los_Angeles
02:55:43.119 (119662000)|VARIABLE_ASSIGNMENT|[3]|dateGMT|"2013-01-12T01:12:00.000Z"
02:55:43.120 (120138000)|USER_DEBUG|[5]|DEBUG|LOCAL  TIME ....2013-01-11 17:12:00
debug Log analysis
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000

So we have saved a query and also a query row using the native userinfo.getTimezone() Method .

 Whats more interesting is Timezone system class in apex now is capable of providing accurate offset from GMT .(I may not need to maintain any custom setting to know the offset depending on timezone of the user)

 The key points to note are as follows:

   1)The negative or positive sign is indicated while returning the integer on   userinfo.gettimezone.getOffset(DateTime);
 2)The integer is in milliseconds .


References: 

 1)http://developer.force.com/releases/release/Spring13
 2)Realease Notes Spring 13 Pdf

Tuesday, 25 December 2012

JSON to Apex Convesion simpler now with http://json2apex.herokuapp.com

With the REST API being lot used when integrating apps developed on force.com paltform with the mobile applications or integrating force.com with external applications communicating using JSON it becomes vital to know few of open source tool we have to make our task simpler.

When we are in technology consulting world the time becomes crucial in any project we face .

There are couple of tools that i normally use whenever an integration is using REST API and the format of data communication is in the form of JSON .

In this blog i will describe two commonly used applications that makes life simpler when dealing with the JSON data.


This application is very useful to format and Validate the JSON .All you need to do is input the JSON in the text box provided and click on Validate button to Validate the JSON .

JSON Lint is a web based validator and reformatter for JSON, a lightweight data-interchange format.


This was recently developed by salesforce evangelism (SuperFell and PattPatterson) and this is really helpful .The app is on the Heroku paltform and its really helpful of you want to construct an apex class from the JSON you have .

All you need is validated JSON (validate using jsonlint.com) is inputed and just a button click will give you the generated class and also the deserialiser method .

Lets see an example 

Say i have the JSON data as shown below 
{

    "id": "https://login.salesforce.com/id/00D90000000aRkLEAU/00590000000HI32AAG",

    "issued_at": "1355574766264",

    "instance_url": "https://ap1.salesforce.com",

    "signature": "LfOtSilg0GXb8NMO2YwcFvDTjRf8Ml0+jxI3XOozmuw=",

    "access_token": "00D90000000aRkL!ARIAQLLtJXpuyCteMrXEbkbEi6qZcgUhkeaK6_.Yqrxlz8JeOn"

}

Once i input this JSON into the app i get the following result
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

 public String id;
 public String issued_at;
 public String instance_url;
 public String signature;
 public String access_token;

 
 public static JSON2Apex parse(String json) {
  return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
 }
 
 static testMethod void testParse() {
  String json = '{'+
  ''+
  '    \"id\": \"https://login.salesforce.com/id/00D90000000aRkLEAU/00590000000HI32AAG\",'+
  ''+
  '    \"issued_at\": \"1355574766264\",'+
  ''+
  '    \"instance_url\": \"https://ap1.salesforce.com\",'+
  ''+
  '    \"signature\": \"LfOtSilg0GXb8NMO2YwcFvDTjRf8Ml0+jxI3XOozmuw=\",'+
  ''+
  '    \"access_token\": \"00D90000000aRkL!ARIAQLLtJXpuyCteMrXEbkbEi6qZcgUhkeaK6_.Yqrxlz8JeOn\"'+
  ''+
  '}';
  JSON2Apex obj = parse(json);
  System.assert(obj != null);
 }
}
This is really awesome .No more time consumed to draft the apex class Happy Coding!

Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...