Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. ({
  2. onInit: function (component, event, helper) {
  3.  
  4. /* In the component, we're using implements="force:hasRecordId"
  5. so we can simply retrieve the associated recordId. This won't
  6. "work" until you've wired up your component to reference a record,
  7. e.g. once you assign your lightning component as a custom button
  8. or quick action on an object. For debugging, you could set a default Id*/
  9. var recordId = component.get("v.recordId");
  10. component.set("v.newLead.AccountId", recordId);
  11. helper.fetchPicklistValues(component, 'Industry', 'industryAuraID');
  12. },
  13. onSelectIndustry: function (component, event, helper) {
  14. /* this function is retrieving the industry option selected by
  15. the user and assigning it to the newLead variable */
  16. var industryPicklistSelection = event.getSource().get("v.value");
  17. component.set("v.newLead.Industry", industryPicklistSelection);
  18. },
  19. validateEmail: function (component, event, helper) {
  20. /* calls a function in the helper method that uses
  21. a regExp to validate email input */
  22. helper.validateEmailInput(component);
  23. },
  24. validatePhoneNumber: function(component,event,helper){
  25. /* using getSource() & getLocalId() methods lets us determine
  26. which phone number input is referenced, letting us reuse the
  27. validatePhoneNumber helper method */
  28. var source = event.getSource();
  29. var id = source.getLocalId();
  30.  
  31. if(id == 'mobilePhoneId'){
  32. helper.validatePhoneNumber(component,'mobilePhoneId','MobilePhone');
  33. } else if(id == 'phoneId'){
  34. helper.validatePhoneNumber(component,'phoneId','Phone');
  35. }
  36. },
  37. onSave: function(component, event, helper){
  38.  
  39. /* validate standard form fields */
  40. var validInput = component.find('formInput').reduce(function (validSoFar, inputCmp) {
  41. inputCmp.showHelpMessageIfInvalid();
  42. return validSoFar && inputCmp.get('v.validity').valid;
  43. }, true);
  44.  
  45. /* custom validation for picklist field */
  46. helper.validatePhoneNumber(component,'phoneId','Phone');
  47. helper.validatePhoneNumber(component,'mobilePhoneId','MobilePhone');
  48. var hasEmailErrors = component.get("v.hasEmailErrors");
  49. var hasPhoneErrors = component.get("v.hasPhoneErrors");
  50. helper.validatePicklist(component,'industryAuraID');
  51. var hasPicklistErrors = component.get("v.hasPicklistErrors");
  52. var newLead = component.get("v.newLead");
  53. var postalCode = component.get("v.postalCode");
  54.  
  55. /* debug is variable used here for debugging -- set it to
  56. false when you're through debugging to avoid console.log statements. */
  57. var debug = true;
  58. if(debug){
  59. Object.keys(newLead).forEach(function(key){
  60. console.log(key + ': ' + newLead[key]);
  61. });
  62. }
  63.  
  64. if(hasEmailErrors == false && hasPhoneErrors == false && hasPicklistErrors == false){
  65. /* pass the newLead variable to server-side method to
  66. save the new Lead */
  67. var action = component.get("c.createLead");
  68. action.setParams({
  69. "newLead": newLead,
  70. "postalCode": postalCode
  71. });
  72.  
  73. action.setCallback(this, function (response) {
  74.  
  75. var state = response.getState();
  76.  
  77. if (state === "SUCCESS") {
  78.  
  79. var resultsToast = $A.get("e.force:showToast");
  80.  
  81. resultsToast.setParams({
  82. "title": "Success",
  83. "message": "New Lead was created.",
  84. "type": "success"
  85. });
  86.  
  87. resultsToast.fire();
  88. $A.get("e.force:refreshView").fire();
  89. $A.get("e.force:closeQuickAction").fire();
  90.  
  91. } else if (state === "ERROR") {
  92.  
  93. var errors = response.getError();
  94. helper.handleErrors(component, errors);
  95. }
  96. }
  97. );
  98. $A.enqueueAction(action);
  99. }
  100.  
  101. },
  102. onCancel: function (component, event, helper) {
  103.  
  104. $A.get("e.force:closeQuickAction").fire();
  105. },
  106. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement