Posted By

chetkloss on 02/21/13


Tagged

form crm Dynamics tidbits


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

emregulcan


Helpful Dynamics CRM 2011 form scripting tidbits


 / Published in: JavaScript
 

A set of common From scripting tasks

  1. //Get the value from a CRM field
  2. var varMyValue = Xrm.Page.getAttribute("CRMFieldSchemaName").getValue() ; 
  3.  
  4. //Set the value of a CRM field
  5. Xrm.Page.getAttribute("po_CRMFieldSchemaName").setValue('My New Value')
  6.  
  7. //Hide/Show a tab/section
  8. Xrm.Page.ui.tabs.get(5).setVisible(false);
  9. Xrm.Page.ui.tabs.get(5).setVisible(true)
  10.  
  11. //Call the onchange event of a field
  12. Xrm.Page.getAttribute("CRMFieldSchemaName").fireOnChange()
  13.  
  14. //Get the selected value of picklist
  15. Xrm.Page.getAttribute("CRMFieldSchemaName").getSelectedOption().text
  16.  
  17. //Set the requirement level
  18. Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("none");
  19. Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("required");
  20. Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("recommended")
  21.  
  22. //Set the focus to a field
  23. Xrm.Page.getControl("CRMFieldSchemaName").setFocus(true)
  24.  
  25. //Stop an on save event
  26. event.returnValue = false;
  27.  
  28. //Return array of strings of users security role GUIDs:
  29. Xrm.Page.context.getUserRoles()
  30.  
  31. Hide/Show Tabs and Sections
  32. function setVisibleTabSection(tabname, sectionname, show) {
  33. var tab = Xrm.Page.ui.tabs.get(tabname);
  34. if (tab != null) {
  35. if (sectionname == null)
  36. tab.setVisible(show);
  37. else {
  38. var section = tab.sections.get(sectionname);
  39. if (section != null) {
  40. section.setVisible(show);
  41. if (show)
  42. tab.setVisible(show);
  43. }
  44. }
  45. }
  46. }
  47.  
  48. //Load a window from a button
  49. function callDialog() {
  50.     window.open("https://propertymanagement.crmhost.selfip.net:555/cs/dialog/rundialog.aspx?DialogId=%7b4A863B63-53D6-4776-97AC-0FF1755545AC%7d&EntityName=opportunity&ObjectId=%7b10EBC949-3233-DD11-B811-0003FF4BD89F%7d", "mywindow");
  51. }
  52.  
  53. //how to clear all options in an OptionSet
  54. Xrm.Page.getControl(“optionSet”).clearOptions();
  55.  
  56. //Save Options
  57. // Equivalent to 'SAVE' button found on the entity form
  58. Xrm.Page.data.entity.save();
  59. // Equivalent to 'SAVE AND NEW' button found on the entity form
  60. Xrm.Page.data.entity.save('saveandnew');
  61. // Equivalent to 'SAVE AND CLOSE' button found on the entity form
  62. Xrm.Page.data.entity.save('saveandclose');
  63.  
  64. /*
  65. How to check form mode using Javascript?
  66.  
  67. Xrm.Page.ui.getFormType() function returns the form type or form mode. Using this function you can make out if the form is currently opened in edit mode or create mode
  68. */
  69. //Create mode
  70. if (Xrm.Page.ui.getFormType() == 1) {
  71. //Do this
  72. }
  73.  
  74. //Edit Mode
  75. if (Xrm.Page.ui.getFormType() == 2) {
  76. // Do this
  77. }
  78.  
  79. /*
  80. How to get Parent Entity Id when a form is in create mode?
  81.  
  82. When you add a new record from subgrid of an entity and you need the Parent Entity Id which is the Id of the record from which the child form is created ,you can get it from the query string.
  83. */
  84. //Create mode
  85. if (Xrm.Page.ui.getFormType() == 1) {
  86.  
  87. var qs = new Querystring();
  88. var Id = qs.get("_CreateFromId", "");
  89. }
  90.  
  91. /*
  92. How to set Lookups attributes?
  93. */
  94. //Using the code in below line you can set the lookup for entity of type 2 i.e. Contacts only
  95. document.getElementById("to").setAttribute("lookuptypes", "2");
  96.  
  97. //Using the code below you can set the default lookup type to Contacts
  98. document.getElementById("to").setAttribute("defaulttype", "2");
  99. /*
  100. The difference in the above two is that the first would restrict the lookup to Contacts only whereas the second would set the default lookup type as Contacts but would not restrict the lookup to Contacts.
  101. */
  102. //Using the code below you can restrict the user for single value selection
  103. document.getElementById("to").setAttribute("lookupstyle", "single");
  104.  
  105. //You can set the icons for selected lookup value using the code below
  106. document.getElementById("to").setAttribute("lookuptypeIcons", "/_imgs/ico_16_2.gif");
  107.  
  108. /*
  109. How to implement window.open functionality in Outlook?
  110.  
  111. When you use window.open to popup a new window programmatically, it works fine with web application of CRM. But when you execute it in Outlook the new window will open as an IE window and a Sign In page is displayed. So to open it in Outlook like any other outlook window you can use the function “openStdWin”.
  112. */ 
  113. openStdWin("Url", "_blank", width, height, features);
  114.  
  115. /*
  116. How to implement Oncheck event of checkbox?
  117.  
  118. When you click on a checkbox the onclick/oncheck event is not fired until the checkbox loses focus. To solve this issue and implement oncheck event immediately after the checkbox is checked or unchecked attach the following function on the onload event of the form and this will fire the onclick event.
  119. */ 
  120. // fire Oncheck event of checkbox
  121. function CheckboxOnLoad() {
  122. crmForm.all.ink_applyvat.onclick = function () {
  123. crmForm.all.ink_applyvat.FireOnChange();
  124. };
  125. }
  126.  
  127. /*
  128. How to reload a parent form OnRefresh of a subgrid?
  129.  
  130. To reload a form OnRefresh event of its subgrid attach the following function to the onLoad event of the form
  131. */ 
  132. //Reload parent form to reflect changes from related subgrid entity
  133. var interval = null;
  134.  
  135. function OrderLoad() {
  136. interval = setInterval("SubGridRefresh();", 15000);
  137. }
  138.  
  139. function SubGridRefresh() {
  140. var grid = document.getElementById("OrderProducts");
  141. if (grid) {
  142. grid.attachEvent("onrefresh", ReLoadForm);
  143. if (interval != null) {
  144. clearInterval(interval);
  145. }
  146. }
  147. }
  148.  
  149. function ReLoadForm() {
  150. window.location.reload(true);
  151. }
  152.  
  153. /*
  154. The function SubGridRefresh attaches the function ReLoadForm with the OnRefresh event of Subgrid. SubGridRefresh is called after some delay because at the time of OnLoad event the subgrid may or may not have loaded so the event handler cannot be attached in case it is not loaded. So the setInterval function will keep on calling the SubGridRefresh function till the Subgrid is loaded and the event handler is attached.
  155. */

Report this snippet  

You need to login to post a comment.