Tuesday 28 January 2014

Parsing An XML Data With DOM Parser In Apex

Parsing the XML data in Apex is always pain and there are already very good blogs and links that really help us to design the strategy for parsing XML data of various complexities .

http://www.tgerm.com/2011/01/apex-xmldom-to-fast-xmldom.html

The above blog from Abhinav one of our MVP of sfdc dev commmunity is informational and i suggest everyone to read this on how to use fast XML DOM.

There is a cookbook article as well on how to effectively achieve the parsing of XML using DOM parsers

http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser

One has to know two system classes a)Document Class b)XMLNode Class
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm

Now the question then ,why I am I writing this blog when already there are so much information on web as how to do it.The reason is even after reading all the documents in net ,i struggled to parse an XML as none of the blogs speak on how to parse when we have namespace in the XML.

Here is the sample XML data that i am trying to parse
Now one thing you will observe in the below parser code that i have written ,how i use the namespaces to parse the data.Providing namespace as null will trigger null pointer exceptions

So the idea of this blogpost was just to make one aware on how to parse the XML if there are namespaces .

Hope you enjoyed the blog series .

Sunday 26 January 2014

Encrypting the XML response from External System In Custom Field Using AES 256 Algorithm

Encrypting the data via crypto classes in apex language is fun and below are the few articles links on developer force that really help us understand better.

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

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

Having read the above document one can easily be of opinion thats its too easy with few lines of code to encrypt data using AES 256 and also same can be stored in a field via an apex trigger .It is not so simple when it comes to storing this data into the custom field .

From the above code ,the problem arises when we start thinking on how to store the encrypted  blob data into the field of object .We dont have any field of data type blob in sfdc and thats were my problem  began and hence this blog for people looking for solution around how to store the blog in a Long Text Field and then how to decrypt the same .


The above code will trigger an exception as below





So now the question ,how will i be able to store the encrypted data in string format from the BLOB ?

There are couple of ways to do this ,but shortest ,the best

1)Convert BLOB data to HEX and store in field and then convert HEX back to string.There is lack of methods to do this till date but comming spring 14 release has a new method and much needed for these kind of problem.Once my org will be up with spring 14 i will try this method

Blob blobValue = EncodingUtil.convertFromHex('4A4B4C');

System.assertEquals('JKL', blobValue.toString());


Intresting but its coming in next release

2)Store the data inform of base64Encoded String and later use this and decode and then decrypt



The above code compiles and now lets decrypt and retrieve the original data

The below diagram summarizes the process


Reference:

https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-you-convert-a-Blob-to-string-1327108626373&language=en_US

Sunday 5 January 2014

MAXIMUM DEBUG LOG SIZE REACHED

*********** MAXIMUM DEBUG LOG SIZE REACHED ***********

Many Times i get frustrated when i want to log something and see the values in debug log and when i encounter an error saying MAXIMUM DEBUG LOG SIZE REACHED.

We all know the log levels in SFDC can be configured and hence setting filters as below in debug logs help to solve the problem

  • Database : NONE
  • Workflow : NONE
  • Validation : NONE
  • Callouts : NONE
  • Apex Code : ERROR
  • Apex Profiling : NONE
  • Visualforce : NONE

The above stackexchange link helps to identify how to solve the problems .

But when it comes to the TEST classes sometimes when there are too many test methods even filtering as shown above does not help much .How do we solve the problem then ?

One solution is to comment out all other test methods except the method that's giving trouble or suspected of cause of error .Hence we will be able to display logs for only the method and debug the result .


Introducing Lightning Base Components

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