In this blog, I am going to explain how to use Salesforce UI API.Salesforce has the number of APIs to build the native and mobile apps. Salesforce UI allows you to build native mobile apps and custom web apps. The Great advantage of the Salesforce UI API, you get data and metadata in a single response. You don’t have to worry about layouts, picklists, field-level security, or sharing.
Step 1: Create Connected App
Create a salesforce connected app as shown below. In this post, i am using named credential to authenticate the web service call. Go to setup –> create apps–> create a new connected app as shown below and save it to get consumer key and secret key. In this step use any URL as callback URL. later after confirmed the auth provides we will replace the callback URL from auth provider callback URL
After saving the connected app you will get consumer key and consumer secret which we will use to configure the Auth provides.
Step 2: Auth Provider
Now we need to create an auth provide as shown below. Go to setup –> administration –> Security control -> create a new Auth provider as shown below .
Replace the connected App callback URL with Auth provide callback URL
Step 3: Named credentials
Create a named credentials as shown below for authenticating Salesforce REST API.
Code :
public class UIExamples { public string accId{get;set;} public Map<String, String> uiDisplayList {get;set ; } public String accName { get; set; } public List<selectOption> getAccountValues() { List<selectOption> options = new List<selectOption>(); for(Account a : [Select Id , Name from Account Limit 10 ]){ options.add(new selectOption(a.Id,a.Name)); } return options; } public UIExamples(){ uiDisplayList = new Map<String, String>(); } public void parseRecord(){ Http h = new Http(); HttpRequest httpReq = new HttpRequest(); httpReq.setMethod('GET'); httpReq.setEndpoint('callout:REST_API1/services/data/v41.0/ui-api/record-ui/'+accName); HttpResponse res = h.send(httpReq); Map<String, Object> root = (Map<String, Object>)JSON.deserializeUntyped(res.getBody()); Map<String, Object> records = (Map<String, Object>)root.get('records'); Map<String, Object> records1 = (Map<String, Object>)records.get(accName); Map<String, Object> records2 = (Map<String, Object>)records1.get('fields'); for(String keys : records2.keySet()){ Map<String, Object> sttt = (Map<String, Object>)records2.get(keys) ; if((String)sttt.values()[0]!=null){ uiDisplayList.put( keys ,(String)sttt.values()[0]) ; } } } public class UIDisplay { public String label {get;set;} public String value {get;set;} public UIDisplay(String label , String value){ this.label = label ; this.value = value; } } }
Visualforce page
Create a visualforce page that shows the data and metadata from based on selected account.
<apex:page controller="UIExamples" sidebar="false" showHeader="true" lightningStylesheets="true"> <apex:slds /> <apex:form > <div align="center"> <b> Select Account to see the details .</b> <apex:selectList size="1" value="{!accName}" > <apex:actionSupport event="onchange" action="{!parseRecord}"/> <apex:selectOptions value="{!AccountValues}"> </apex:selectOptions> </apex:selectList> </div> <br/> <div align="center"> <apex:pageBlock> <apex:pageBlockTable value="{!uiDisplayList}" var="dirKey" columns="2" styleClass="styleHeader" > <apex:column value="{!dirKey}" headerValue="Label" /> <apex:column value="{!uiDisplayList[dirKey]}" headerValue="Value"/> </apex:pageBlockTable> </apex:pageBlock> </div> </apex:form> </apex:page>