Guest User

Untitled

a guest
Dec 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.40 KB | None | 0 0
  1. var formContext = null;
  2. var parentLocationId = null;
  3. var stage1 = "Investigation Request";
  4. var stage2 = "Terms Of Reference";
  5. var currentStage = "";
  6.  
  7. function onLoadEmailSender(executionContext) {
  8. var formContext = executionContext.getFormContext();
  9. if (formContext.getAttribute("regardingobjectid") !== null) {
  10. var regardingobject = formContext
  11. .getAttribute("regardingobjectid")
  12. .getValue();
  13. if (regardingobject !== null) {
  14. var objectId = regardingobject[0].id.replace("{", "").replace("}", "");
  15. Xrm.WebApi.retrieveRecord(
  16. regardingobject[0].entityType,
  17. objectId,
  18. "?$select=_apuk_parentorganisationid_value"
  19. ).then(
  20. function success(res) {
  21. if (res.hasOwnProperty("_apuk_parentorganisationid_value")) {
  22. Xrm.WebApi.retrieveRecord(
  23. res[
  24. "_apuk_parentorganisationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
  25. ],
  26. res._apuk_parentorganisationid_value,
  27. "?$select=_apuk_emailsender_value"
  28. ).then(
  29. function sucess(result) {
  30. if (result._apuk_emailsender_value !== null) {
  31. var arrVal = [
  32. {
  33. entityType:
  34. result[
  35. "_apuk_emailsender_value@Microsoft.Dynamics.CRM.lookuplogicalname"
  36. ],
  37. id: result._apuk_emailsender_value,
  38. name:
  39. result[
  40. "_apuk_emailsender_value@OData.Community.Display.V1.FormattedValue"
  41. ]
  42. }
  43. ];
  44. formContext.getAttribute("from").setValue(arrVal);
  45. }
  46. },
  47. function error(error) {
  48. console.log(error);
  49. }
  50. );
  51. }
  52. },
  53. function error(err) {
  54. console.log(err);
  55. }
  56. );
  57. }
  58. }
  59. }
  60.  
  61. function onLoadInvestigation(executionContext) {
  62. formContext = executionContext.getFormContext();
  63. var gridContext = formContext.getControl("teammembers");
  64. var gridContextIssue = formContext.getControl("subgrid_investigatedissues");
  65. formContext.data.process.addOnStageSelected(handleState);
  66. if (
  67. formContext.ui.getFormType() === 1 ||
  68. formContext.ui.getFormType() === 2
  69. ) {
  70. if (formContext.ui.getFormType() === 1) {
  71. setAskEvaKeywords(formContext);
  72. }
  73.  
  74. if (formContext.getAttribute("ownerid") !== null) {
  75. var owner = formContext.getAttribute("ownerid").getValue();
  76. Xrm.WebApi.retrieveRecord(
  77. "systemuser",
  78. owner[0].id.replace("{", "").replace("}", ""),
  79. "?$select=_apuk_parentlocationid_value"
  80. ).then(
  81. function success(res) {
  82. if (res._apuk_parentlocationid_value !== null) {
  83. var arrVal = [
  84. {
  85. entityType:
  86. res[
  87. "_apuk_parentlocationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
  88. ],
  89. id: res._apuk_parentlocationid_value,
  90. name:
  91. res[
  92. "_apuk_parentlocationid_value@OData.Community.Display.V1.FormattedValue"
  93. ]
  94. }
  95. ];
  96. formContext
  97. .getAttribute("apuk_parentorganisationid")
  98. .setValue(arrVal);
  99.  
  100. setHealthCareType();
  101. }
  102. },
  103. function error(err) {
  104. console.log(err);
  105. }
  106. );
  107. }
  108. }
  109.  
  110. HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue);
  111.  
  112. formContext.data.process.addOnStageChange(function () {
  113. HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue);
  114. });
  115. }
  116.  
  117. function HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue) {
  118. var reportTitle;
  119. var incidentDesc;
  120. var bgAndContext;
  121. var selectedRow;
  122. var stageName;
  123. var flag = [false, false, false, false, false];
  124.  
  125. if (formContext.getAttribute("apuk_investigationtitle") !== null) {
  126. reportTitle = formContext
  127. .getAttribute("apuk_investigationtitle")
  128. .getValue();
  129. formContext.getAttribute("apuk_investigationtitle").addOnChange(function () {
  130. stageName = formContext.data.process.getActiveStage().getName();
  131. reportTitle = formContext
  132. .getAttribute("apuk_investigationtitle")
  133. .getValue();
  134. if (stageName === "Terms Of Reference") {
  135. if (reportTitle === null) {
  136. flag[1] = false;
  137. formContext.ui.setFormNotification(
  138. "Report Title is required",
  139. "ERROR",
  140. "reporttitlerror"
  141. );
  142. checkAllIsClear(flag, formContext);
  143. } else {
  144. formContext.ui.clearFormNotification("reporttitlerror");
  145. flag[1] = true;
  146. checkAllIsClear(flag, formContext);
  147. }
  148. }
  149. });
  150. }
  151. if (formContext.getAttribute("apuk_description") !== null) {
  152. incidentDesc = formContext.getAttribute("apuk_description").getValue();
  153. formContext.getAttribute("apuk_description").addOnChange(function () {
  154. stageName = formContext.data.process.getActiveStage().getName();
  155. incidentDesc = formContext.getAttribute("apuk_description").getValue();
  156. if (stageName === "Terms Of Reference") {
  157. if (incidentDesc === null) {
  158. flag[2] = false;
  159. formContext.ui.setFormNotification(
  160. "Incident Description is required",
  161. "ERROR",
  162. "incidentdeserror"
  163. );
  164. checkAllIsClear(flag, formContext);
  165. } else {
  166. formContext.ui.clearFormNotification("incidentdeserror");
  167. flag[2] = true;
  168. checkAllIsClear(flag, formContext);
  169. }
  170. }
  171. });
  172. }
  173. if (formContext.getAttribute("apuk_backgroundcontext") !== null) {
  174. bgAndContext = formContext
  175. .getAttribute("apuk_backgroundcontext")
  176. .getValue();
  177. formContext.getAttribute("apuk_backgroundcontext").addOnChange(function () {
  178. stageName = formContext.data.process.getActiveStage().getName();
  179. bgAndContext = formContext
  180. .getAttribute("apuk_backgroundcontext")
  181. .getValue();
  182. if (stageName === "Terms Of Reference") {
  183. if (bgAndContext === null) {
  184. flag[3] = false;
  185. formContext.ui.setFormNotification(
  186. "Background and Context is required",
  187. "ERROR",
  188. "bgandcontexterr"
  189. );
  190. checkAllIsClear(flag, formContext);
  191. } else {
  192. formContext.ui.clearFormNotification("bgandcontexterr");
  193. flag[3] = true;
  194. checkAllIsClear(flag, formContext);
  195. }
  196. }
  197. });
  198. }
  199. stageName = formContext.data.process.getActiveStage().getName();
  200. selectedRow = gridContext.getGrid().getTotalRecordCount();
  201. var numberOfIssueRow = gridContextIssue.getGrid().getTotalRecordCount();
  202. if (stageName === "Terms Of Reference") {
  203. var fetchTeamMember =
  204. '<fetch top="5">' +
  205. '<entity name="apuk_investigationteammember">' +
  206. '<attribute name="apuk_name" />' +
  207. "<filter>" +
  208. '<condition attribute="apuk_investigationid" operator="eq" value="' +
  209. formContext.data.entity.getId() +
  210. '" />' +
  211. '<condition attribute="statecode" operator="eq" value="0" />' +
  212. "</filter>" +
  213. "</entity>" +
  214. "</fetch>";
  215. fetchTeamMember = "?fetchXml=" + encodeURIComponent(fetchTeamMember);
  216. Xrm.WebApi.retrieveMultipleRecords(
  217. "apuk_investigationteammember",
  218. fetchTeamMember
  219. ).then(
  220. function (result) {
  221. if (result.entities.length !== 0) {
  222. formContext.ui.clearFormNotification("teamerror");
  223. flag[0] = true;
  224. } else {
  225. formContext.ui.setFormNotification(
  226. "Investigation members are required",
  227. "ERROR",
  228. "teamerror"
  229. );
  230. flag[0] = false;
  231. }
  232. },
  233. function (error) {
  234. console.log(error);
  235. }
  236. );
  237.  
  238. var fetchIssue =
  239. '<fetch top="5">' +
  240. '<entity name="apuk_investigatedissue">' +
  241. '<attribute name="apuk_name" />' +
  242. "<filter>" +
  243. '<condition attribute="apuk_investigationid" operator="eq" value="' +
  244. formContext.data.entity.getId() +
  245. '" />' +
  246. '<condition attribute="statecode" operator="eq" value="0" />' +
  247. "</filter>" +
  248. "</entity>" +
  249. "</fetch>";
  250. fetchIssue = "?fetchXml=" + encodeURIComponent(fetchIssue);
  251. Xrm.WebApi.retrieveMultipleRecords(
  252. "apuk_investigatedissue",
  253. fetchIssue
  254. ).then(
  255. function (result) {
  256. debugger;
  257. if (result.entities.length !== 0) {
  258. formContext.ui.clearFormNotification("issuerror");
  259. flag[4] = true;
  260. } else {
  261. formContext.ui.setFormNotification(
  262. "Issues to be Investigated is required",
  263. "ERROR",
  264. "issuerror"
  265. );
  266. flag[4] = false;
  267. }
  268. },
  269. function (error) {
  270. console.log(error);
  271. }
  272. );
  273.  
  274. if (reportTitle === null) {
  275. formContext.ui.setFormNotification(
  276. "Report Title is required",
  277. "ERROR",
  278. "reporttitlerror"
  279. );
  280. flag[1] = false;
  281. } else {
  282. formContext.ui.clearFormNotification("reporttitlerror");
  283. flag[1] = true;
  284. }
  285.  
  286. if (incidentDesc === null) {
  287. formContext.ui.setFormNotification(
  288. "Incident Description is required",
  289. "ERROR",
  290. "incidentdeserror"
  291. );
  292. flag[2] = false;
  293. } else {
  294. formContext.ui.clearFormNotification("incidentdeserror");
  295. flag[2] = true;
  296. }
  297.  
  298. if (bgAndContext === null) {
  299. formContext.ui.setFormNotification(
  300. "Background and Context is required",
  301. "ERROR",
  302. "bgandcontexterr"
  303. );
  304. flag[3] = false;
  305. } else {
  306. formContext.ui.clearFormNotification("bgandcontexterr");
  307. flag[3] = true;
  308. }
  309.  
  310. // setTimeout(function () {
  311. // checkAllIsClear(flag, formContext);
  312. // }, 1000);
  313. }
  314.  
  315. gridContext.addOnLoad(function () {
  316. stageName = formContext.data.process.getActiveStage().getName();
  317. selectedRow = gridContext.getGrid().getTotalRecordCount();
  318. if (stageName === "Terms Of Reference") {
  319. if (selectedRow !== 0) {
  320. formContext.ui.clearFormNotification("teamerror");
  321. flag[0] = true;
  322. checkAllIsClear(flag, formContext);
  323. } else {
  324. formContext.ui.setFormNotification(
  325. "Investigation members are required",
  326. "ERROR",
  327. "teamerror"
  328. );
  329. flag[0] = false;
  330. checkAllIsClear(flag, formContext);
  331. }
  332. }
  333. });
  334.  
  335. gridContextIssue.addOnLoad(function () {
  336. debugger;
  337. stageName = formContext.data.process.getActiveStage().getName();
  338. numberOfIssueRow = gridContextIssue.getGrid().getTotalRecordCount();
  339. if (stageName === "Terms Of Reference") {
  340. if (numberOfIssueRow !== 0) {
  341. formContext.ui.clearFormNotification("issuerror");
  342. flag[4] = true;
  343. checkAllIsClear(flag, formContext);
  344. } else {
  345. formContext.ui.setFormNotification(
  346. "Issues to be Investigated is required",
  347. "ERROR",
  348. "issuerror"
  349. );
  350. flag[4] = false;
  351. checkAllIsClear(flag, formContext);
  352. }
  353. }
  354. });
  355. }
  356.  
  357. function checkAllIsClear(flag, formContext) {
  358. for (var i = 0; i < flag.length; i++) {
  359. if (flag[i] === false) {
  360. //formContext.ui.process.setVisible(false);
  361. formContext.data.save().then(function(){
  362. formContext.data.process.setStatus("aborted");
  363. }, function(err, message){
  364. console.log(message);
  365. });
  366. break;
  367. } else if (i === flag.length - 1) {
  368. //formContext.ui.process.setVisible(true);
  369. formContext.data.save().then(function () {
  370. formContext.data.process.setStatus("active");
  371. }, function (err, message) {
  372. console.log(message);
  373. });
  374. }
  375. }
  376. }
  377.  
  378. function setAskEvaKeywords(formContext) {
  379. var fetchXML =
  380. "<fetch>" +
  381. "<entity name='apuk_askevakeywords' >" +
  382. "<attribute name='apuk_name' />" +
  383. "<attribute name='apuk_processstage' />" +
  384. "<filter>" +
  385. "<condition attribute='apuk_processstage' operator='eq' value='200000000' />" +
  386. "</filter>" +
  387. "</entity>" +
  388. "</fetch>";
  389. fetchXML = "?fetchXml=" + encodeURI(fetchXML);
  390. console.log("apuk_askevakeywords");
  391. Xrm.WebApi.retrieveMultipleRecords("apuk_askevakeywords", fetchXML, 1).then(
  392. function success(result) {
  393. for (var i = 0; i < result.entities.length; i++) {
  394. console.log("apuk_askevakeywords");
  395. var lookup = [
  396. {
  397. entityType: "apuk_askevakeywords",
  398. id: result.entities[i].apuk_askevakeywordsid,
  399. name: result.entities[i].apuk_name
  400. }
  401. ];
  402.  
  403. formContext.getAttribute("apuk_askevakeywordsid").setValue(lookup);
  404. }
  405. },
  406. function (error) {
  407. console.log("error:", error.message);
  408. }
  409. );
  410. }
  411.  
  412. function setLocation(executionContext, isOnChange) {
  413. if (executionContext != null) {
  414. formContext = executionContext.getFormContext();
  415. }
  416.  
  417. currentStage = formContext.data.process.getSelectedStage().getName();
  418. var location = formContext.getControl("header_process_apuk_organisationid");
  419. var location1 = formContext.getControl(
  420. "header_process_apuk_organisationid_1"
  421. );
  422. var parentLocation = formContext.getAttribute("apuk_parentorganisationid");
  423. if (parentLocation !== null && parentLocation.getValue() != null) {
  424. parentLocationId = parentLocation.getValue()[0].id;
  425. if (typeof isOnChange != "undefined") {
  426. if (location !== null && currentStage == stage1) {
  427. location.removePreSearch(filterLocation);
  428. }
  429.  
  430. if (location1 !== null && currentStage == stage2) {
  431. location1.removePreSearch(filterLocation);
  432. }
  433. }
  434.  
  435. if (currentStage == stage1) {
  436. location.addPreSearch(filterLocation);
  437. } else if (currentStage == stage2) {
  438. location1.addPreSearch(filterLocation);
  439. }
  440. } else {
  441. if (typeof isOnChange != "undefined") {
  442. location.removePreSearch(filterLocation);
  443. location1.removePreSearch(filterLocation);
  444. }
  445. }
  446. }
  447.  
  448. function filterLocation() {
  449. var customFilter =
  450. "<filter><condition attribute = 'parentaccountid' operator = 'eq' value = '" +
  451. parentLocationId +
  452. "'/></filter>";
  453. if (currentStage == stage1) {
  454. var location = formContext.getControl("header_process_apuk_organisationid");
  455. location.addCustomFilter(customFilter);
  456. } else if (currentStage == stage2) {
  457. var location1 = formContext.getControl(
  458. "header_process_apuk_organisationid_1"
  459. );
  460. location1.addCustomFilter(customFilter);
  461. }
  462. }
  463.  
  464. function handleState() {
  465. setLocation();
  466. }
  467.  
  468. function setHealthCareType() {
  469. if (formContext.ui.getFormType() === 1) {
  470. var parentLocation = formContext.getAttribute("apuk_parentorganisationid");
  471. if (parentLocation !== null && parentLocation.getValue() != null) {
  472. var parentLocationId = parentLocation.getValue()[0].id;
  473. Xrm.WebApi.retrieveRecord(
  474. "account",
  475. parentLocationId.replace("{", "").replace("}", ""),
  476. "?$select=_apuk_healthcaretypeid_value"
  477. ).then(
  478. function success(data) {
  479. if (data._apuk_healthcaretypeid_value !== null) {
  480. var lookup = [
  481. {
  482. entityType:
  483. data[
  484. "_apuk_healthcaretypeid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
  485. ],
  486. id: data._apuk_healthcaretypeid_value,
  487. name:
  488. data[
  489. "_apuk_healthcaretypeid_value@OData.Community.Display.V1.FormattedValue"
  490. ]
  491. }
  492. ];
  493. formContext.getAttribute("apuk_healthcaretypeid").setValue(lookup);
  494. }
  495. },
  496. function error(err) {
  497. console.log(err);
  498. }
  499. );
  500. }
  501. }
  502. }
  503.  
  504. function onLoadEvidence(executionContext) {
  505. formContext = executionContext.getFormContext();
  506. var gridContext = formContext.getControl("evidenceitems");
  507. gridContext.addOnLoad(function () {
  508. var totalRows = gridContext.getGrid().getTotalRecordCount();
  509. if (totalRows !== 0) {
  510. formContext.ui.clearFormNotification("evidencerror");
  511. formContext.ui.process.setVisible(true);
  512. } else {
  513. formContext.ui.setFormNotification(
  514. "Evidence item is required",
  515. "ERROR",
  516. "evidencerror"
  517. );
  518. formContext.ui.process.setVisible(false);
  519. }
  520. });
  521. }
  522.  
  523. function onLoadChronologyAnalysis(executionContext) {
  524. formContext = executionContext.getFormContext();
  525. var flag = [false, false, false];
  526. var overallConclusion;
  527. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  528. if (formContext.getAttribute("apuk_overallconclusion") !== null) {
  529. overallConclusion = formContext
  530. .getAttribute("apuk_overallconclusion")
  531. .getValue();
  532. formContext.getAttribute("apuk_overallconclusion").addOnChange(function () {
  533. overallConclusion = formContext
  534. .getAttribute("apuk_overallconclusion")
  535. .getValue();
  536. if (overallConclusion === null) {
  537. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  538. flag[0] = false;
  539. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  540. formContext.ui.setFormNotification(
  541. "Overall Conclusion is required",
  542. "ERROR",
  543. "overallerr"
  544. );
  545. checkAllIsClear(
  546. JSON.parse(sessionStorage.getItem("flagArr")),
  547. formContext
  548. );
  549. } else {
  550. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  551. flag[0] = true;
  552. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  553. formContext.ui.clearFormNotification("overallerr");
  554. checkAllIsClear(
  555. JSON.parse(sessionStorage.getItem("flagArr")),
  556. formContext
  557. );
  558. }
  559. });
  560. }
  561.  
  562. var fetchXml =
  563. "<fetch top='5'>" +
  564. "<entity name='apuk_caremanagementproblem'>" +
  565. "<attribute name='apuk_eventid' />" +
  566. "<filter>" +
  567. "<condition attribute='apuk_chronologyanalysisid' operator='eq' value='" +
  568. formContext.data.entity.getId() +
  569. "' /></filter><order attribute='createdon' descending='true' /></entity></fetch>";
  570. fetchXml = "?fetchXml=" + encodeURIComponent(fetchXml);
  571. Xrm.WebApi.retrieveMultipleRecords(
  572. "apuk_caremanagementproblem",
  573. fetchXml
  574. ).then(
  575. function (result) {
  576. if (result.entities.length !== 0) {
  577. parent.Xrm.Page.ui.clearFormNotification("cmperror");
  578. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  579. flag[2] = true;
  580. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  581. for (var i = 0; i < flag.length; i++) {
  582. if (flag[i] === false) {
  583. parent.parent.Xrm.Page.ui.process.setVisible(false);
  584. break;
  585. } else if (i === flag.length - 1) {
  586. parent.parent.Xrm.Page.ui.process.setVisible(true);
  587. }
  588. }
  589. } else {
  590. parent.Xrm.Page.ui.setFormNotification(
  591. "CMP is required",
  592. "ERROR",
  593. "cmperror"
  594. );
  595. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  596. flag[2] = false;
  597. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  598. for (var i = 0; i < flag.length; i++) {
  599. if (flag[i] === false) {
  600. parent.parent.Xrm.Page.ui.process.setVisible(false);
  601. break;
  602. } else if (i === flag.length - 1) {
  603. parent.parent.Xrm.Page.ui.process.setVisible(true);
  604. }
  605. }
  606. }
  607. },
  608. function (error) {
  609. console.log(error);
  610. }
  611. );
  612.  
  613. if (overallConclusion === null) {
  614. formContext.ui.setFormNotification(
  615. "Overall Conclusion is required",
  616. "ERROR",
  617. "overallerr"
  618. );
  619. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  620. flag[0] = false;
  621. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  622. } else {
  623. debugger;
  624. formContext.ui.clearFormNotification("overallerr");
  625. flag = JSON.parse(sessionStorage.getItem("flagArr"));
  626. flag[0] = true;
  627. sessionStorage.setItem("flagArr", JSON.stringify(flag));
  628. }
  629. }
  630.  
  631. function onLoadQualityImprovemntPlan(executionContext) {
  632. formContext = executionContext.getFormContext();
  633. var flag = [false, false];
  634. sessionStorage.setItem("qipFlag", JSON.stringify(flag));
  635. var gridContext = formContext.getControl("subgrid_tasks");
  636. var totalRow = gridContext.getGrid().getTotalRecordCount();
  637. if (totalRow === 0) {
  638. formContext.ui.setFormNotification(
  639. "Quality Improvement Plan is required",
  640. "ERROR",
  641. "taskerror"
  642. );
  643. flag[0] = false;
  644. sessionStorage.setItem("qipFlag", JSON.stringify(flag));
  645. } else {
  646. flag[0] = true;
  647. sessionStorage.setItem("qipFlag", JSON.stringify(flag));
  648. formContext.ui.clearFormNotification("taskerror");
  649. }
  650. gridContext.addOnLoad(function () {
  651. totalRow = gridContext.getGrid().getTotalRecordCount();
  652. if (totalRow === 0) {
  653. formContext.ui.setFormNotification(
  654. "Quality Improvement Plan is required",
  655. "ERROR",
  656. "taskerror"
  657. );
  658. flag = JSON.parse(sessionStorage.getItem("qipFlag"));
  659. flag[0] = false;
  660. sessionStorage.setItem("qipFlag", JSON.stringify(flag));
  661. checkAllIsClear(flag, formContext);
  662. } else {
  663. formContext.ui.clearFormNotification("taskerror");
  664. flag = JSON.parse(sessionStorage.getItem("qipFlag"));
  665. flag[0] = true;
  666. sessionStorage.setItem("qipFlag", JSON.stringify(flag));
  667. checkAllIsClear(flag, formContext);
  668. }
  669. });
  670. }
Add Comment
Please, Sign In to add comment