APEX Code Snippets

Reading inline Query data in APEX

 

JSONGenerator

This example generates a JSON string in pretty print format by using the methods of the JSONGenerator class

 

 

 

 

Salesforce to Salesforce Integration using REST Webservice

Use Case:

An organization has two salesforce org ( Sales Org and R&D Org ). Sales Org user wants to create a support request in R&D org for a Sales Org opportunity. R&D org team has special skills to work on those Support Requests. Sales Org user is willing to share some of the opportunity field data and restrict sharing access to only for the opportunities that have Support Requests. Sales Org User is also interested to know the status and owner of the Support Request. R&D Org users should be able to run reports on Case/Support Request/Opportunity(Sales Org) data

Solution:

Providing a button on Opportunity UI to create a support request. Invoking a Visualforce page and using a controller class system can call Webservice on R&D Org to create a Case. Whenever a Support Request is created checkbox is updated in opportunity object, based on the checkbox opportunity information is shared with an integration user. Salesforce lightning connect is used to integrate Sales Org & R-&-D org. Check out my blog post on lightning connect. R&D Org can call webservice on Sales Org to update Support Request Owner and Status details in Sales Org system.

 

Implementation:

Below flow gives a top view of how the integration/data flow between the systems.

 

Step #1: Authentication

Sales Org calls Webservice on R&D Org

R&D Org authenticates Sales Org call

R&D lets Sales Org create Case after authentication

Salesforce configuration details:

Configure Connected App in R&D Org

Configure Authentical Provider in Sales Org

Configure Named Credentials in Sales Org

Use Named Credentials in Webservice Callout script

Follow blog post ‘How to configure Named Credentials

 

Step #2: Creating Webservice on R&D Org

 

 

Step #3: Controller class to create call R&D Webservice from Sales Org

 

 

Step #4: VF Page to create Support Request

 

 

Step #5 : Create Support Request button configuration on Opportunity Page

go to Setup->Opportunity->Buttons to create Button

 

 

Step : #6: Add button to Layout

 

 

 

 

 

Sample @future code – ready to process when resources are available

 

When to use?
Long-running processes or external calls
When it’s not batchable

Advantages:
Asynchronous process without concurrent limit
Easier and quicker to implement as opposed to batch

Disadvantages:
Parameters passed in can be only of primitive types
Can’t chain @future methods
Difficult to access job id

Batchable code to update Contact address with Account Address

Simple code to write Batchable code in Apex

 

 

References: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_database_batchable.htm