Tuesday, March 13, 2018

External IDs and Http Mocks

Rest of the challenges for the "Apex Specialist" super badge were all right to handle. Relatively easier for me than challenge 1. Two key learning from my point of view:
1. In Salesforce you can use any field on the custom object to be used as an external ID. And, you can NOT set external id directly, i.e. code like MyObj__c.externalID = 'xyz'; does not work.
2. When testing a method that will make an http callout directly or indirectly, do not forget to set the mock class that implements HttpCalloutMock interface: Test.setMock(HttpCalloutMock.class, new MyCalloutMock());

Monday, March 12, 2018

Accessing Relationships with Apex

While attempting the first challenge for the Apex Specialist badge, I learned an important bit that helped me close the loop and finish the challenge successfully.

To access any related objects (look up, master-child), the API name that ends with __c is replaced by __r. For example, lets say class with name slave__c is under master__c, then an instance of the master class could access the slaves with syntax m.slave__r. Further, you can iterate through all the items with this syntax:
for(slave__c wp : m.slave__r){
    //awesome code here
}
We can do something similar for lookup relationships. Note when the relationship is not one to many (as is with most look ups for the class doing the look up), __r yields a single object rather than a list.

Sunday, March 4, 2018

Effective JSON parsing with Apex

I am relatively new to Apex, but as a Java programmer the language does not feel alien. While doing a few trailheads to get me up to speed, I came across the Apex Rest Callouts module. While I was able to pass the challenge relying on the code snippet provided (here is my initial code), I was not convinced that it is the best way to parse a JSON response. And so began my search. Sharing my experience, just in case somebody finds it useful.

I first tried the developer reference, but totally struggled to make it work for the JSON I was working with.

My second port of call was JSON2Apex. And the answer was promising here, though it did take me some time to get to it. I would recommend the following steps for hassle free JSON parsing:
Step 1: Go to JSON2Apex application, paste your JSON, name your class and check the "Create explicit parse code" option. I don't do that initially and wasted a lot of time!
You will get a zip file with a parser class and a test class. For the purpose of this post we will focus on the parser class. My parser class from JSON2Apex looked as linked.
Step 2: If you try to copy this into your developer console, the class will show an error as it has an inner class with the same name as the outer class!
//not allowed in Apex!
public class AnimalParse {
       //move the code inside inner class here
       public class AnimalParse { //get rid of inner class 
                                  //after moving the code
              //some code, to move up
       }
}
Just take the content inside the inner class with the same name and move it up so that it becomes part of the top most class. Most probably it will be some member classes represented in JSON and a constructor for the outer class (as was in my case). For reference, linking my changed AnimalParser file.
Step 3: Use this parser to handle to JSON received from the callout. As you can see in the linked code, this is much more elegant and easier to code with, with all the heavy lifting done by the parser created by SFDC.

While this is done for a simple example, I am fairly confident that this can be done for complex JSON as well. Hopefully this helps you to simplify your JSON parsing and make your integration less painful!