Salesforce Awesome Json Mapper

A json deserializer App  to automate how json received by Salesfroce REST API are mapped into Salesforce objects and persisted into the database. It is simple to use and new features are been added every month. Empower your admins to create and maintain REST API Integration points. 

Case Scenario

Writting REST APIs to store data in salesforce is tedious and laborious work. Suppose the REST API receives json like the one bellow

Json Sample

The json represents an account with 3 contacts each one with a case. A tipical code to map this json to Account, Contact and Cases objects would be

The hard way

Boring isn’t ? And it gets worse as the json increases in number of nodes. Note that if we want to implement relationships (Lookups) between Account, Contacts, Case we have to save first the Account to get its Id and then assign this value into Contact.ContactId field. You do the same thing to map relationship between Contacts and Cases, saving first the Contacts to obtain their Ids and then finally setting the Case.ContactId field.

What if We could have an engine easily set by an Admin to automate all of this for us ? Presenting SFJsonMapper. Instead of the code above you would use just three lines of code in your REST API.

The easy way

Let’s analyse these three lines of code. First line loads a mapping called ‘SimpleAccountMapping’ which was preveously set by an admin using a User Interface. (We will talk about that in the next section). The SimpleAccountMapping has all the information needed by the engine to map the json to SObjects and then save them in Salesforce.

The second line actually parses the json in the string ‘jsonInput’ creating a tree graph of objects. For the json text above that would generate a tree structure with one Account as root having three Contacts as children, each one with a Case object.

The third line executes the sobject tree, upserting then in Salesforce and resolving any Lookups specified in the ‘SimpleAccountMapping’ (i.e Contact.AccontId = Account.Id). (Note that no triggers are necessary to assign fields to represent relationship between the objects. The engine does that for you)

Creating a Map

Once you install the package you will have a new application called MJsonMapper, make sure you set permissions to use this app in your salesforce box. Once you open the app, click on the JsonMaps tab to see available maps and then select on of the maps to view it. For the map “SimpleAccountMapping” described in the example above we could have the following configuration

UI Json Map

The mapping indicates that the json root should be mapped to an Account object and the json fields nEmployees, startedAt, site, name are mapped to Account fields. Note that the “name” field mapping to Account.Name has a “primary key” tag in it. This indicates that the Account object should be upserted based on the value of this field. In other words, the Account will be updated if there is an account where Account.Name equals to the value on the json field “name”, otherwise the account will be inserted.

Also note that in the map, the json field contacts is mapped to a List<Contact>. For each Contact in the list, the json fields lastName, title and email are mapped to Contact.LastName, Contact.Title, Contact.Email respectively. In the same contacts field you will find the text t inner join Contact::AccountId <= Account::Id. This specifies that the Contact::AccountId should receive the Account’s Id value where Account is the salesforce object mapped to the parent json object. For now a salesforce object mapped to a json node can only receive one field of the salesforce object mapped to the parent json node. I am planning to relax this restriction in future versions.

TODO

  • Save mappings as Custom Metadata Types.
  • Support for DateTime, for now only Number and String is supported as primitive types.
  • Auto completion (Combo Box) to select salesforce objects and fields in the Admin Map UI instead of simply writing them and hope their names are right.
  • Better error messages to be presented in the logs.
  • UI where admins can submit a json text block to test the mappings they are building.
  • Create support for pickup lists where json values can also be mapped to pickup list values using a conversion table
  • Allow a mapped Salesforce object to receive values from other Salesforce objects mapped to other nodes in the json. For now each object can only receive one field from only the Salesforce object associated with its parent json node.