It's been a while since I've had the time to work on the BrightSIGN (formerly known as Signature Capture), but I had a couple of days off this week and what better way to spend them than a bit of front end coding.
The Problem
A feature requested by a user posting a review on the app exchange was the ability to update a field on the record that the signature image is attached to. There are a number of ways to achieve this, but one thing was for sure - this wasn't going into the package. Dynamically generating update statements to change records in the subscriber org is the kind of thing that really slows down the security review!
The scenario I wanted to satisfy was a Lightning App Builder page that would show a BrightSIGN component, and once the user had saved the signature image, update the record and hide the BrightSIGN component so they weren't asked to sign multiple times.
I was originally thinking that I'd go the route of a trigger on Attachment/ContentDocument, depending on how the component was configured, but I decided against this for a couple of reasons -
- I'd have to make some assumptions about the name of the attachment/content document in order to determine that it had been created by BrightSIGN, which would lead to false positives if attachments/documents with the same name were used elsewhere
- I'd be detached from the front end and unable to signal to the container that the record had been updated - this is key when I want to conditionally render parts of the page depending on whether the record has been 'signed' or not.
The Solution
<force:recordData aura:id="recordUpdater" recordId="{!v.recordId}" fields="Signature_Captured__c" targetFields="{!v.theRecordFields}" targetError="{!v.recordLoadError}" mode="EDIT" />
This component also has a handler for the SignatureCaptured event:
<aura:handler event="BGSIGCAP:SignatureCapturedEvt" action="{!c.handleCaptured}"/>
Thus when the user saves the signature image, the handleCaptured method from my component's controller is invoked. This sets the Signature_Captured__c field and updates the record using the force:recordData component, again avoiding me having to write any Apex code. Once the update is complete, the view is refreshed to notify the container that it's changed and allow any conditional rendering to be re-evaluated:
handleCaptured : function(component, event, helper) { component.get('v.theRecordFields').Signature_Captured__c=true; component.find("recordUpdater").saveRecord(function(saveResult) { var resultsToast = $A.get("e.force:showToast"); if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") { resultsToast.setParams({ "type":"success", "title": "Record Signed", "message": "The record whas been marked as signed." }); resultsToast.fire(); $A.get("e.force:refreshView").fire(); } // error handling }); }
You can see a very brief video of this below :
No comments:
Post a Comment