Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <!-- AccountManager.cmp -->
  2. <aura:component controller='CRAccountGetService' implements='flexipage:availableForRecordHome,force:hasRecordId,force:hasSobjectName' access='global'>
  3.  
  4. <!-- Attributes-->
  5. <aura:attribute name='recordId' type='string' default='{! v.recordId }' access='global' />
  6. <aura:attribute name='sObjectName' type='string' access='global' />
  7.  
  8. <!-- Handlers -->
  9. <aura:handler name='init' value='{! this }' action='{! c.handleInit }' />
  10. <aura:handler name="refreshAccountForm" event="c:CRRefreshAccountForm" action='{! c.handleRefreshAccountForm }' />
  11.  
  12. <!-- Screen -->
  13. <div>
  14. <!-- AccountForm dynamically injected here -->
  15. {! v.body }
  16. </div>
  17. </aura:component>
  18.  
  19. // AccountManagerController.js
  20. ({
  21. handleInit: function(component, event, helper) {
  22. helper.newAccountForm(component);
  23. },
  24.  
  25. // handler for child component refresh event. Destroy & recreate account form component
  26. handleRefreshAccountForm: function(component, event, helper) {
  27.  
  28. // destroy existing Account form child component
  29. var body = component.get("v.body");
  30. var arrayLength = body.length;
  31. for (var i = 0; i < arrayLength; i++) {
  32. body[i].destroy();
  33. }
  34. component.set('v.body',[]);
  35.  
  36. // create new Account form child component
  37. var recId = component.get('v.recordId');
  38. $A.createComponent("c:AccountForm", { 'recordId': recId },
  39. function (newAccountForm, status, errorMsg) {
  40. if (status === "SUCCESS") {
  41. component.set("v.body", newAccountForm);
  42. } else {
  43. // show error
  44. }
  45. });
  46. },
  47. })
  48.  
  49. <aura:handler name='init' value='{! this }' action='{! c.handleInit }' />
  50.  
  51. <!-- Event to let parent (manager) know we need to be refreshed -->
  52. <aura:registerEvent name="refreshAccountForm" type="c:CRRefreshAccountForm" />
  53. <aura:attribute name='recordId' type='string' default='{! v.recordId }' access='global' />
  54. <aura:attribute name='mode' type='string' default='view' access='global' />
  55.  
  56. <!-- Screen -->
  57. <lightning:messages />
  58.  
  59. <lightning:recordForm
  60. aura:id='accountForm'
  61. objectApiName='Account'
  62. recordId='{! v.recordId }'
  63. layoutType='Full'
  64. columns='2'
  65. mode='{! v.mode }'
  66. onsuccess='{! c.handleSuccess }'
  67. />
  68. </aura:component>
  69.  
  70. // AccountFormController.js
  71. ({
  72. // After a successful save to SF database, call external service
  73. handleSuccess: function(component, event, helper) {
  74. helper.callExternalService(component, event, helper).then (
  75. // promise resolved: external server accepted data. SF database unchanged
  76. $A.getCallback(function () {
  77. helper.showToast('Account Save', 'All good', 'success', 1000);
  78. }),
  79.  
  80. // promise rejected: external server rejected data and overwrote values in SF database,
  81. // fire event so the Manager (the parent component) refreshes the form view
  82. $A.getCallback(function (message) {
  83. var refreshMeEvent = component.getEvent("refreshAccountForm");
  84. refreshMeEvent.setParam("message", message);
  85. refreshMeEvent.fire();
  86. })
  87. );
  88. },
  89. })
  90.  
  91. // AccountFormHelper.js
  92. ({
  93. callExternalService: function(component,event, helper) {
  94. return new Promise($A.getCallback(function(resolve, reject) {
  95.  
  96. var externalService = component.get('c.callExternalService');
  97. externalService.setParams({
  98. 'accountId': component.get('v.recordId')
  99. });
  100.  
  101. externalService.setCallback(this, function(response) {
  102. if (response.getState() === "SUCCESS") {
  103. var rspMsg = JSON.parse(response.getReturnValue());
  104. if (rspMsg.success) {
  105. resolve();
  106. } else {
  107. reject(rspMsg.message);
  108. }
  109. } else {
  110. reject('Controller failure:' + response.getState());
  111. }
  112. });
  113. $A.enqueueAction(externalService);
  114. }));
  115. },
  116. })
Add Comment
Please, Sign In to add comment