Extract nested attribute value from JSON String using Apex

Salesforce offers two main ways to extract values from a JSON string
1) JSON.deserialize() This approach requires you to have a wrapper class/other valid entity to deserialize the data to.
2) JSON.createParser(jsonString) This approach requires you to loop through a bunch of confusing parser.nextToken() calls

In this article I am introducing an easy to use utility to extract inner attribute values from any JSON string. This is leveraging JSON.createParser(). But once you have this utility, you don't have to go through the parser.nextToken() confusion.

Example of use
Utils.extractAttributeFromJson('{"data":{"user":{"organization":{"id":"123"}}}}', 'data.user.organization.id');


Please note that it will be more performant to use JSON.createParser() directly if you have multiple attributes to extract from the JSON.

How to schedule a class to run once at a specific time from apex

Salesforce gives out of the box ability to schedule apex classes with some limited options to control frequency under setup => apex classes => "Schedule Apex" button. But this approach limits you to run apex classes only at the start of the hour.

If you have a need to run a scheduled apex class at a specific time, you can use custom CRON expressions gives a more flexible way to do that.


Note that nextRun can be any datetime value in this case. Here we generate a cron expression from the datetime provided. If you want to read more about cron expressions refer this blog.

How to scan barcode values and send to Salesforce

Here we are covering how we can scan barcode values and execute some actions in Salesforce using the scanned data. This is achieved using a mobile app "Barcode To Cloud". It is available for iOS and Android as FREE to get started.

This app allows you to scan different barcode formats (QR Code, Code 128, Code 39, Data Matrix, Codebar, PDF417, EAN13, ISBN etc) and send data to Salesforce. You can configure the app to do differnt activities in Salesforce when a barcode or QR code is scanned.

Below actions can be configured in the app using the data obtained from scanning.

  • Create a record in Salesforce with data from barcode
  • Find a record in Salesforce matching barcode data and display in the app
  • Find a record with barcode data and update it
  • Find a record with barcode data and delete it
  • For complex operations including records from multiple objects, create a record in Salesforce using barcode data and use a trigger/process builder to execute additional operations


Short Demo Video

Full Demo Video

This video shows a detailed walkthrough of the app covering below items.

  1. Connecting to Salesforce - watch
  2. Receive shipments at warehouse with notes - watch
  3. Receive shipment and scan continuously - watch
  4. Importing configuration - watch
  5. Retrieve account details when account number barcode is scanned and allow users to update details - watch
  6. Track attendance at an event using barcode in ticket - watch
  7. Simple demo - Post scanned barcode to chatter as the scanning user - watch


Demo Video (Old Version)

This video shows a detailed walkthrough of the app covering below items.

1) Connecting to Salesforce

2) Updating Checked In At field on lead record when lead's email is scanned using the app

3) Displaying lead record details by scanning email QR code

If you are looking for a free appexchange app to generate barcode, please check out Barcode and QR Code Generator.


Use cases

Barcode generation and scanning can be used to improve a lot of business processes. Some examples are,

  • Generate product barcodes and stick it as labels to products so that you can scan using mobile app and add products to opportunity
  • Generate QR code corresponding to your event registration custom object field(Id or registration number). Send QR code in email confirmation to the users. Later when users arrive at the event you can scan reservations using the mobile app
  • Stick a QR code in your outgoing shipments and incoming shipments. Then scan it and send to Salesforce records using the mobile app so that you can track shipments at different stages of shipping

Export/Import Configuration

This configuration driven app allows you to export the configurations you created and share with your team members. Your team members can later import these configurations and the app will be ready to use without any additional configuration


Need fully branded mobile app for your business?

This is a generic mobile app designed to support use cases of all types of users. So features in the app are generic. If you need a fully custom branded mobile app that interacts with your Salesforce environment and designed spcifically for your business requirements, please contact me at albertsfo1@gmail.com

Display barcode in Salesforce record detail view as a button

Here we cover an easy way to display QR code in Salesforce classic using a free appexchange app Barcode and QR Code Generator.

This app allows you to generate barcode in Salesforce without depending on any external services. In below step by step guide, we are assuming that you have installed the app already. If not please go to Barcode and QR Code Generator and install.


Example - Displaying QR code using custom field in custom object

Here we will go through an example of displaying QR code using data from a custom field. Here for demo purpose I have created a custom object with name Demo_Record__c. This object has a custom field with API name Unique_Field__c. This is just one example. Same approach will work for any other field in any custom/standard object in Salesforce.


1) Create a barcode config record for the custom object and field combination

Go to "Barcode Configs" tab that comes with the app and create a new record. Give it a name without spaces or special characters. I am giving it a name DemoUniqueFieldConfig. Specify Demo_Record__c as object name and Unique_Field__c as field name. Then in order to display QR code, I am selecting "QR Code" from "Barcode type" picklist/dropdown field. You can leave other fields with default values. Then save the record. Please check below screenshot,

2) Create a custom button to display QR Code in "Demo Record" page layout

Navigate to "Setup => Objects => Demo Record". Scroll down to "Buttons, Links, and Actions" section and create a new button.

Generally to display barcode in a new widow you can just redirect user to below URL pattern /apex/BarQR__BarcodeGeneratorPage?configName=YOUR_CONFIG_RECORD_NAME_HERE&recordId=RECORD_ID_HERE

We are doing the same here in the button. We are using the configuration record name DemoUniqueFieldConfig from Step 1 and passing recordId dynamically using {!Demo_Record__c.Id}.

Save your button code. At the end you can add the button to page layout under "Setup => Objects => Demo Record => Page Layouts => Edit". Check below screenshot.

Click save and navigate to a record details page. You will be able to see "QR Code" button in the page. When you click on the button, QR code will be displayed in a popup window using data from the field you configured.

Result

Example from Salesforce classic view.

Since our "QR Code" button is a custom button that redirects the user to a page with barcode, it will be available in lightning experience also. But in lightning Barcode/QR code will be more easily accessible if you add it through app builder page. Checkout example of adding barcode component directly in lightning app builder page


Use cases

Barcode generation and scanning can be used to improve a lot of business processes. Some examples are,

  • Generate product barcodes and stick labels to products so that you can scan and add products to opportunity
  • Generate QR code corresponding to your event registration custom object field(Id or registration number) and email users, so that you can scan in participants at the time of the event
  • Stick a QR code in your outgoing shipments and incoming shipments and then scan it into Salesforce records so that you can track shippings end to end

Displaying barcode in Salesforce lightning experience natively

Here we cover an easy way to display barcode in lightning experience using a free appexchange app Barcode and QR Code Generator.

This app allows you to generate barcode in Salesforce without depending on any external services. In below example, we are assuming that you have the app installed already. If not please go to Barcode and QR Code Generator and install.


Example - Displaying barcode using email field in lead

Here we will go through an example of displaying barcode using data from email field in lead records. This is just one example. Same approach will work for any other field in any custom/standard object in Salesforce.


1) Create a barcode config record for lead email field

Go to "Barcode Configs" tab that comes with the app and create a new record. Give it a name without spaces or special characters. Specify lead as object name and email as field name. You can leave other fields with default values. Then save the record. Please check below screenshot,

2) Add Barcode component to lead flexipage and activate

Once you have created a barcode config record, it is just matter of editing lead flexipage and adding "Barcode and QR Code Generator" component to the page.

First go to a lead record details page. Click Gear Icon and "Edit Page".

Drop "Barcode and QR Code Generator" component from left sidebar to the place you need. It will ask for a configName. Give the name of the record you created from step 1.

Click "Save" button on top right and finally click "Activation" on top right and assign the page as organization default (or based on your app or profile preferences).


Result

If you followed the steps correctly, you should see barcode when you go to any lead record page.


Use cases

Barcode generation and scanning can be used to improve a lot of business processes. Some examples are,

  • Generate product barcodes and stick labels to products so that you can scan and add products to opportunity
  • Generate QR code corresponding to your event registration custom object field(Id or registration number) and email users, so that you can scan in participants at the time of the event
  • Stick a QR code in your outgoing shipments and incoming shipments and then scan it into Salesforce records so that you can track shippings end to end
  • Need to scan the barcode values to Salesforce as well? Check out the article here