Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- AccountManager.cmp -->
- <aura:component controller='CRAccountGetService' implements='flexipage:availableForRecordHome,force:hasRecordId,force:hasSobjectName' access='global'>
- <!-- Attributes-->
- <aura:attribute name='recordId' type='string' default='{! v.recordId }' access='global' />
- <aura:attribute name='sObjectName' type='string' access='global' />
- <!-- Handlers -->
- <aura:handler name='init' value='{! this }' action='{! c.handleInit }' />
- <aura:handler name="refreshAccountForm" event="c:CRRefreshAccountForm" action='{! c.handleRefreshAccountForm }' />
- <!-- Screen -->
- <div>
- <!-- AccountForm dynamically injected here -->
- {! v.body }
- </div>
- </aura:component>
- // AccountManagerController.js
- ({
- handleInit: function(component, event, helper) {
- helper.newAccountForm(component);
- },
- // handler for child component refresh event. Destroy & recreate account form component
- handleRefreshAccountForm: function(component, event, helper) {
- // destroy existing Account form child component
- var body = component.get("v.body");
- var arrayLength = body.length;
- for (var i = 0; i < arrayLength; i++) {
- body[i].destroy();
- }
- component.set('v.body',[]);
- // create new Account form child component
- var recId = component.get('v.recordId');
- $A.createComponent("c:AccountForm", { 'recordId': recId },
- function (newAccountForm, status, errorMsg) {
- if (status === "SUCCESS") {
- component.set("v.body", newAccountForm);
- } else {
- // show error
- }
- });
- },
- })
- <aura:handler name='init' value='{! this }' action='{! c.handleInit }' />
- <!-- Event to let parent (manager) know we need to be refreshed -->
- <aura:registerEvent name="refreshAccountForm" type="c:CRRefreshAccountForm" />
- <aura:attribute name='recordId' type='string' default='{! v.recordId }' access='global' />
- <aura:attribute name='mode' type='string' default='view' access='global' />
- <!-- Screen -->
- <lightning:messages />
- <lightning:recordForm
- aura:id='accountForm'
- objectApiName='Account'
- recordId='{! v.recordId }'
- layoutType='Full'
- columns='2'
- mode='{! v.mode }'
- onsuccess='{! c.handleSuccess }'
- />
- </aura:component>
- // AccountFormController.js
- ({
- // After a successful save to SF database, call external service
- handleSuccess: function(component, event, helper) {
- helper.callExternalService(component, event, helper).then (
- // promise resolved: external server accepted data. SF database unchanged
- $A.getCallback(function () {
- helper.showToast('Account Save', 'All good', 'success', 1000);
- }),
- // promise rejected: external server rejected data and overwrote values in SF database,
- // fire event so the Manager (the parent component) refreshes the form view
- $A.getCallback(function (message) {
- var refreshMeEvent = component.getEvent("refreshAccountForm");
- refreshMeEvent.setParam("message", message);
- refreshMeEvent.fire();
- })
- );
- },
- })
- // AccountFormHelper.js
- ({
- callExternalService: function(component,event, helper) {
- return new Promise($A.getCallback(function(resolve, reject) {
- var externalService = component.get('c.callExternalService');
- externalService.setParams({
- 'accountId': component.get('v.recordId')
- });
- externalService.setCallback(this, function(response) {
- if (response.getState() === "SUCCESS") {
- var rspMsg = JSON.parse(response.getReturnValue());
- if (rspMsg.success) {
- resolve();
- } else {
- reject(rspMsg.message);
- }
- } else {
- reject('Controller failure:' + response.getState());
- }
- });
- $A.enqueueAction(externalService);
- }));
- },
- })
Add Comment
Please, Sign In to add comment