Guest User

Untitled

a guest
Oct 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.74 KB | None | 0 0
  1. Uncaught Error in $A.getCallback() [Cannot read property 'getElement' of
  2. undefined]
  3. throws at https://xxxx--sandboxv1.lightning.force.com/auraFW/javascript/nRhh35vBDuL0P3mlbcWPkg/aura_prod.js:8:15
  4. Object.updateAriaDescribedBy()@https://xxxx--sandboxv1.lightning.force.com/components/lightning/select.js:3:315
  5. Object.updateHelpMessage()@https://xxxx--sandboxv1.lightning.force.com/components/lightning/select.js:4:106
  6. Object.showHelpMessage()@https://xxxx--sandboxv1.lightning.force.com/components/lightning/select.js:7:239
  7. Object.eval()@https://xxxx--sandboxv1.lightning.force.com/components/lightning/select.js:8:60
  8. eval()@https://xxxx--sandboxv1.lightning.force.com/libraries/lightning/eventEmitterLibrary/EventEmitter.js:1:550
  9. b.emit()@https://xxxx--sandboxv1.lightning.force.com/libraries/lightning/eventEmitterLibrary/EventEmitter.js:1:528
  10. a.leave()@https://xxxx--sandboxv1.lightning.force.com/libraries/lightning/interactingStateLibrary/InteractingState.js:2:134
  11. eval()@https://xxxx--sandboxv1.lightning.force.com/libraries/lightning/rateLimitLibrary/debounce.js:1:349
  12.  
  13. <aura:component controller="createOrderFromWonOpprotunityController" implements="force:lightningQuickActionWithoutHeader,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global">
  14. <aura:attribute name="isWon" type="Boolean" default="false"/>
  15. <aura:attribute name="shouldContinue" type="Boolean" default="false"/>
  16. <aura:attribute name="notified" type="String" />
  17.  
  18. <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  19.  
  20. <div class="slds-scope">
  21. <div id="notificationRes"><span style="color: red;">{!v.notified}</span><br/></div>
  22.  
  23. recordId = {!v.recordId}
  24. shouldContinue = {!v.shouldContinue}
  25.  
  26. <aura:if isTrue="{!v.isWon == false}">
  27. <p> Your current opportunity is not <strong>closed</strong>. Please select on of the status below.</p>
  28. <lightning:select aura:id="selectClosedStage" name="selectClosedStage" label="Select an Stage Status" onchange="{!c.changeStatus}">
  29. <option value="">choose one...</option>
  30. <option value="Closed Won">Closed Won</option>
  31. <option value="Closed Lost">Closed Lost</option>
  32. </lightning:select>
  33. <aura:set attribute="else">
  34. <c:createOrderFromWonOpportunityAfterClosedWon recordId="{!v.recordId}" shouldContinue="{!v.shouldContinue}"/>
  35. </aura:set>
  36. </aura:if>
  37. </div>
  38.  
  39. ({
  40. doInit : function(component, event, helper) {
  41. var action = component.get("c.isClosedWon");
  42. action.setParams({
  43. "oppId" : component.get("v.recordId")
  44. });
  45. action.setCallback(this, function(response) {
  46. var state = response.getState();
  47. console.log("doInit >> val >> " + response.getReturnValue());
  48. console.log("doInit >> state >> " + state);
  49. if (state === "SUCCESS") {
  50. component.set("v.isWon", response.getReturnValue());
  51. if (response.getReturnValue())
  52. component.set("v.shouldContinue", true);
  53. else
  54. component.set("v.shouldContinue", false);
  55. } else {
  56. component.set("v.notified", "A server Error occuried, please try again later.");
  57. }
  58. });
  59. $A.enqueueAction(action);
  60. },
  61.  
  62. changeStatus : function (component, event, helper) {
  63. try {
  64. var selectValue = component.find("selectClosedStage").get("v.value");
  65. var action = component.get("c.CustomUpdateRecord");
  66. action.setParams({
  67. "oppId" : component.get("v.recordId"),
  68. "stageName" : selectValue
  69. });
  70. console.log('changeStatus >> selectValue >> '+ selectValue);
  71. action.setCallback(this, function(response) {
  72. var res = response.getReturnValue();
  73. console.log("changeStatus >> val >> " + response.getReturnValue());
  74. console.log("changeStatus >> state >> " + response.getState());
  75. if (selectValue === 'Closed Won') {
  76. component.set("v.isWon", true);
  77. component.set("v.shouldContinue", true);
  78. } else {
  79. component.set("v.isWon", false);
  80. component.set("v.shouldContinue", false);
  81. component.set("v.notified", 'the current opportunity is Closed Lost. No Order can be created for it');
  82. }
  83. });
  84. $A.enqueueAction(action);
  85. } catch (e) {
  86. console.log(e);
  87. }
  88. }
  89. })
  90.  
  91. <aura:component controller="createOrderFromWonOpprotunityController">
  92. <aura:attribute name="recordId" type="String"/>
  93. <aura:attribute name="shouldContinue" type="Boolean"/>
  94. <aura:attribute name="errorCheck" type="List" />
  95. <pre>
  96. under child :
  97. recordId = {!v.recordId}
  98. shouldContinue = {!v.shouldContinue}
  99. </pre>
  100. <aura:if isTrue="{!v.shouldContinue}">
  101. <div class="slds-grid slds-grid--align-spread">
  102. You are about to convert the current Opportunity to an Order.<br/>
  103. If you are ready, Please click on "Confirm" button below<br/>
  104. <lightning:button label="Confirm" onclick="{!c.doSubmit}" class="slds-m-top--medium" />
  105. </div>
  106. <aura:set attribute="else">
  107. <p>Sorry you can not convert the current Opportunity to an Order for the following reason.</p>
  108. <aura:iteration items="{!v.errorCheck}" var="item">
  109. <span style="color: red;"> - {!item}</span>
  110. </aura:iteration>
  111. </aura:set>
  112. </aura:if>
  113. </aura:component>
  114.  
  115. ({
  116. doSubmit : function (component, event, helper) {
  117. console.log("confirm has been hit");
  118. var action = component.get("c.createOrder");
  119. action.setParams({
  120. "oppId" : component.get("v.recordId")
  121. });
  122. console.log("cdoSubmit >> " + component.get("v.recordId"));
  123. action.setCallback(this, function(response) {
  124. var res = response.getReturnValue();
  125. console.log("res >> " + res);
  126. if (response.getState() === 'SUCCESS') {
  127. var NotificationError = [];
  128. if (res !== 'SUCCESS' && res !== '') {
  129. switch(res) {
  130. case 'NotClosedWon':
  131. NotificationError.push("change this Opportunity Status as Won before continuing.");
  132. break;
  133. case 'NoProduct' :
  134. NotificationError.push("The current Opportunity does not have any associated products");
  135. break;
  136. case 'ErrorAccountBillingCity' :
  137. NotificationError.push("The Account associated to the current Opportunity is missing the Billing City Information.");
  138. break;
  139. case 'ErrorAccountBillingStreet' :
  140. NotificationError.push("The Account associated to the current Opportunity is missing the Billing Street Information.");
  141. break;
  142. case 'ErrorAccountBillingState' :
  143. NotificationError.push("The Account associated to the current Opportunity is missing the Billing State Information.");
  144. break;
  145. case 'ErrorAccountBillingPostalCode' :
  146. NotificationError.push("The Account associated to the current Opportunity is missing the Billing Postal Code Information.");
  147. break;
  148. case 'ErrorAccountBillingCountry' :
  149. NotificationError.push("The Account associated to the current Opportunity is missing the Billing Country Information.");
  150. break;
  151. case 'ErrorNoContactAssociated' :
  152. NotificationError.push("The Account associated to the current Opportunity do not have any Contact associated.");
  153. break;
  154. case 'Error' :
  155. component.set("v.notified", "An Error happened, please try again. If it persists please contact your administrator");
  156. break;
  157. } //end switch
  158. component.set("v.errorCheck", NotificationError);
  159. component.set("v.shouldContinue", false);
  160. console.log(NotificationError);
  161. } //end if
  162. else {
  163. console.log("after Process: " + res);
  164. var resultsToast = $A.get("e.force:showToast");
  165. resultsToast.setParams({
  166. "title": "Conversion done succesfully",
  167. "message": "Your Order is now available under the Order tab. Please review and Activate."
  168. });
  169. $A.get("e.force:closeQuickAction").fire();
  170. resultsToast.fire();
  171. }
  172. } else {
  173. component.set("v.errorCheck", ["An Error happened, please try again. If it persists please contact your administrator"]);
  174. }
  175. });
  176. $A.enqueueAction(action);
  177. }
  178. })
  179.  
  180. public with sharing class createOrderFromWonOpprotunityController {
  181.  
  182. @AuraEnabled
  183. public static Boolean isClosedWon (String oppId) {
  184. try {
  185. Opportunity opp = [SELECT Id, IsClosed, StageName FROM Opportunity Where Id= :oppId][0];
  186. if( opp.StageName.equals('Closed Won'))
  187. return true;
  188. else return false;
  189. } catch(Exception e) {
  190. system.debug(e);
  191. }
  192. return false;
  193. }
  194.  
  195.  
  196. @AuraEnabled
  197. public static Boolean CustomUpdateRecord (String oppId, String stageName) {
  198. try {
  199. Opportunity opp = [SELECT Id, IsClosed, StageName FROM Opportunity Where Id= :oppId][0];
  200. opp.StageName = stageName;
  201. update opp;
  202. return true;
  203. } catch(Exception e) {
  204. system.debug(e);
  205. }
  206. return false;
  207. }
  208.  
  209. public static String isMissingData(String oppId) {
  210. try {
  211. Opportunity opp = [SELECT Id, AccountId, IsClosed, StageName, CloseDate, OrderId__c FROM Opportunity Where Id= :oppId][0];
  212. system.debug(opp);
  213. Boolean shouldContinue = opp.IsClosed && opp.StageName == 'Closed Won'; //&& (opp.OrderId__c == null || opp.OrderId__c == '');
  214. system.debug('shouldContinue >>> ' + shouldContinue);
  215. if (shouldContinue) {
  216. if (isMissingOpportunityProduct(oppId))
  217. return 'NoProduct';
  218. else {
  219. String missingInfoAccount = isMissingAccountInfo(opp);
  220. if (!missingInfoAccount.equals(''))
  221. return missingInfoAccount;
  222. String oppSED = isMissingOpportunityProductStartEndDate(oppId);
  223. if (!oppSED.equals(''))
  224. return oppSED;
  225. /*
  226. String missingIntakeForm = MissingIntakeForm(oppId);
  227. if (!missingIntakeForm.equals(''))
  228. return missingIntakeForm;
  229. */
  230. }
  231. } else {
  232. return 'NotClosedWon';
  233. }
  234. return 'SUCCESS';
  235. }catch(Exception e) {
  236. system.debug(e);
  237. return 'Error';
  238. }
  239. }
  240.  
  241. public static String isMissingAccountInfo(Opportunity opp) {
  242. Account acc = [SELECT Id, AccountNumber, BillingStreet, BillingCity, BillingPostalCode, BillingState, BillingCountry FROM Account WHERE Id = :opp.AccountId][0];
  243. system.debug(acc);
  244. Contact contact = [SELECT Id FROM Contact WHERE AccountId = :acc.Id][0];
  245. if (acc.BillingCity == null)
  246. return 'ErrorAccountBillingCity';
  247. if (acc.BillingStreet == null)
  248. return 'ErrorAccountBillingStreet';
  249. if (acc.BillingPostalCode == null)
  250. return 'ErrorAccountBillingPostalCode';
  251. if (acc.BillingState == null)
  252. return 'ErrorAccountBillingState';
  253. if (acc.BillingCountry == null)
  254. return 'ErrorAccountBillingCountry';
  255. if (contact == null)
  256. return 'ErrorNoContactAssociated';
  257. return '';
  258. }
  259.  
  260. public static Boolean isMissingOpportunityProduct(String oppId) {
  261. try {
  262. List<OpportunityLineItem> ProductsInOpportunity = [SELECT Id FROM OpportunityLineItem WHERE OpportunityId = :oppId];
  263. return ProductsInOpportunity.size() == 0;
  264. } catch(Exception e) {
  265. system.debug(e);
  266. return true;
  267. }
  268. }
  269.  
  270. public static String isMissingOpportunityProductStartEndDate(String oppId) {
  271. List<OpportunityLineItem> ProductsInOpportunity = [SELECT Id, Product2Id, ServiceDate, End_Date__c FROM OpportunityLineItem WHERE OpportunityId = :oppId];
  272. if (ProductsInOpportunity.size() > 0) {
  273. for(OpportunityLineItem line : ProductsInOpportunity) {
  274. if (line.ServiceDate == null || line.End_Date__c == null)
  275. return 'MissingDate';
  276. }
  277. }
  278. return '';
  279. }
  280.  
  281. public static String isMissingIntakeForm(String oppId) {
  282. try {
  283. List<OpportunityLineItem> ProductsInOpportunity = [SELECT Id, Name, OpportunityId, Intake_Form_Required__c,
  284. IntakeFormId__c, Product2Id, Product_Intake_Form__c
  285. FROM OpportunityLineItem
  286. WHERE OpportunityId = :oppId];
  287. system.debug('ProductsInOpportunity');
  288. system.debug(ProductsInOpportunity);
  289. for (OpportunityLineItem item : ProductsInOpportunity) {
  290. if (item.Intake_Form_Required__c == true ) {
  291. Product2 pro = [SELECT Id, Name, Product_Intake_Form__c FROM Product2 WHERE Id = : item.Product2Id][0];
  292. String objIntakeFormName = pro.Product_Intake_Form__c;
  293. String query = 'SELECT Id FROM ' + objIntakeFormName + ' WHERE OpportunityProductId__c = ''+ item.Id +''';
  294. List<SObject> IntakeForms = Database.query(query);
  295. if (IntakeForms.size() == 0)
  296. return 'missingIntakeForm#'+ pro.Name;
  297. else {
  298. if (item.Product_Intake_Form__c == '' || item.Product_Intake_Form__c == null)
  299. item.Product_Intake_Form__c = objIntakeFormName;
  300. item.IntakeFormId__c = IntakeForms[0].Id;
  301. update item;
  302. }
  303. }
  304. }
  305. return '';
  306. }
  307. catch(Exception e) {
  308. system.debug(e);
  309. return 'missingIntakeForm#Error';
  310. }
  311. }
  312.  
  313.  
  314. @AuraEnabled
  315. public static String createOrder(String oppId) {
  316. try {
  317. //if (updateOppAsWon)
  318. // createOrderFromWonOpprotunityController.CustomUpdateRecord(oppId);
  319. system.debug('createOrder');
  320. String res = createOrderFromWonOpprotunityController.isMissingData(oppId);
  321. if (!res.equals('SUCCESS'))
  322. return res;
  323. Opportunity opp = [SELECT Id,IsClosed, Name, AccountId, Amount, CloseDate, Description, HasOpportunityLineItem,
  324. OwnerId, Pricebook2Id, StageName, OrderId__c
  325. FROM Opportunity
  326. WHERE Id = :oppId][0];
  327. system.debug(opp);
  328. List<OpportunityLineItem> ProductsInOpportunity = [SELECT Id, Name, Name__c, Description, OpportunityId, Product2Id, PricebookEntryId, ProductCode, UnitPrice,
  329. ListPrice, Final_Price__c, Months__c, SetupFee__c, ServiceDate, End_Date__c, Booking_Date__c,
  330. Product_Intake_Form__c, IntakeFormId__c
  331. FROM OpportunityLineItem
  332. WHERE OpportunityId = :oppId];
  333. Account acc = [SELECT Id, AccountNumber, BillingStreet, BillingCity, BillingPostalCode, BillingState, BillingCountry FROM Account WHERE Id = :opp.AccountId][0];
  334. if (opp.HasOpportunityLineItem) {
  335. system.debug(ProductsInOpportunity);
  336. system.debug(acc);
  337. Order order = new Order();
  338. order.AccountId = acc.Id;
  339. order.BillToContactId = [SELECT Id FROM Contact WHERE AccountId = :acc.Id][0].Id;
  340. order.BillingStreet = acc.BillingStreet;
  341. order.BillingCity = acc.BillingCity;
  342. order.BillingPostalCode = acc.BillingPostalCode;
  343. order.BillingState = acc.BillingState;
  344. order.BillingCountry = acc.BillingCountry;
  345. order.OpportunityId = opp.Id;
  346. order.Pricebook2Id = opp.Pricebook2Id;
  347. order.Name = 'Order_' + opp.Name;
  348. order.OwnerId = opp.OwnerId;
  349. order.EffectiveDate = system.today();
  350. order.Status = 'Draft'; // is status set to Activate, it cannot be changed
  351. insert order;
  352. system.debug(order);
  353. List<OrderItem> orderItems = new List<OrderItem>();
  354. for (OpportunityLineItem oppItem : ProductsInOpportunity) {
  355. Product2 pro = [SELECT Id, Name, ProductCode FROM Product2 WHERE Id = : oppItem.Product2Id][0];
  356. OrderItem orderProduct = new OrderItem();
  357. orderProduct.OrderId = order.Id;
  358. orderProduct.Name__c = oppItem.Name__c;
  359. orderProduct.Status__c = 'Draft';
  360. orderProduct.Product2Id = pro.Id;
  361. orderProduct.Product_Name__c = pro.Name;
  362. orderProduct.PricebookEntryId = oppItem.PricebookEntryId;
  363. orderProduct.Description = oppItem.Description;
  364. orderProduct.Quantity = oppItem.Months__c;
  365. orderProduct.Month__c = oppItem.Months__c;
  366. orderProduct.UnitPrice = oppItem.UnitPrice;
  367. orderProduct.ServiceDate = oppItem.ServiceDate; // correspond to start date
  368. orderProduct.EndDate = oppItem.End_Date__c;
  369. orderProduct.Booking_Date__c = oppItem.Booking_Date__c;
  370. orderItems.add(orderProduct);
  371. }
  372. insert orderItems;
  373. opp.OrderId__c = order.Id;
  374. update opp;
  375. system.debug(opp);
  376. return 'SUCCESS'; //order.Id;
  377. }
  378. } catch( Exception e) {
  379. system.debug(e);
  380. return 'Error';
  381. }
  382. return '';
  383.  
  384. }
  385. }
Add Comment
Please, Sign In to add comment