Generic Compliance Verification UI with a Lightning Component

The component described in this section can be placed on a Lightning Experience or Salesforce Mobile record page for any object without any further configuration. The component utilizes ComplianceService to dynamically invoke the appropriate Domain class and apply the applicable verification process.

Lightning Components will be covered in more detail in a later chapter. This component demonstrates one of the many places where components can be used to extend Lightning Experience and other aspects of the platform.

The following code shows the Apex Controller for the component:

public with sharing class ComplianceCheckerComponent { 
 
@AuraEnabled 
public static List<String> verify(Id recordId) { 
  try { 
    // Verify the given record for compliance 
    ComplianceService.verify(newSet<Id> { recordId }); 
    // Success, all good! 
    Return null; 
  } catch (Exception e) { 
     // Report message as normal via apex:pageMessages 
     List<String> messages = new List<String> { e.getMessage() }; 
     // Display additional compliance failure messages? 
     If(e instanceofComplianceService.ComplianceException) { 
       ComplianceService.ComplianceExceptionce =
(ComplianceService.ComplianceException) e; for(ComplianceService.VerifyResultverifyResult :
ce.failures) { messages.add(String.format('{0} ({1})', new List<String> { verifyResult.failureReason, verifyResult.complianceCode })); } } return messages; } }

The following code shows the component markup, JavaScript controller, and helper—ComplianceChecker.cmp:

<aura:component implements="force:hasRecordId,flexipage:availableForRecordHome" controller="ComplianceCheckerComponent" access="global"> 
   <aura:dependency resource="markup://force:editRecord"
type="EVENT" /> <aura:attribute name="category" type="String"
default="success" /> <aura:attribute name="messages" type="String[]"/> <aura:handler name="init" value="{!this}"
action="{!c.onInit}"/>
<aura:handler event="force:refreshView"
action="{!c.onRefreshView}"/> <div class="{!'slds-box slds-theme-' + v.category}"> <aura:iteration items="{!v.messages}"var="message"> <p><ui:outputText value="{!message}"/></p> </aura:iteration> </div> </aura:component>

The preceding code handles the force:refreshView event sent by the Lightning Experience framework when the user edits records. Because the flexipage:availableForRecordHome interface is specified, this component can only be placed on record pages. The ComplianceCheckerControler.js file is as follows:

({ 
  onInit : function(component, event, helper) { 
    helper.verifyCompliance(component, event, helper); 
  }, 
  onRefreshView : function(component, event, helper) { 
    helper.verifyCompliance(component, event, helper); 
  } 
}) 

The ComplianceCheckerHelper.js file is as follows:

({ 
   verifyCompliance : function(component) { 
      var action = component.get("c.verify"); 
      action.setParams({ "recordId" : 
component.get("v.recordId") }); action.setCallback(this, function(response) { if(response.getState() === 'SUCCESS') { var messages = response.getReturnValue(); if(messages!=null) { component.set("v.category", "error"); component.set("v.messages", messages); } else { component.set("v.category", "success"); component.set("v.messages", ["Verified compliance" ]); } } }); $A.enqueueAction(action); } })

Unlike the Visualforce page, no specific reference to an object is made, allowing the component to be used on any page.

If you wanted to restrict the visibility of the component within the Lightning App Builder tool, where users customize Lightning Experience and Salesforce1 Mobile, you can utilize the sfdc:objects tag in your .design file. The following shows an example of this:

<sfdc:objects>
<sfdc:object>Driver__c</sfdc:object>
<sfdc:object>Car__c</sfdc:object>
</sfdc:objects>

The component in this section does not use the latest Lightning Web Component framework from Salesforce in this case. This is because, at the time of writing, handling the force:refreshView event was not supported; therefore, the preceding component uses the Aura framework. Fortunately, Salesforce allows you to mix components developed in either framework – we will be exploring this further in a later chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174