JavaScript 有用的Dynamics CRM 2011表格脚本花絮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 有用的Dynamics CRM 2011表格脚本花絮相关的知识,希望对你有一定的参考价值。

//Get the value from a CRM field
var varMyValue = Xrm.Page.getAttribute("CRMFieldSchemaName").getValue() ; 
 
//Set the value of a CRM field
Xrm.Page.getAttribute("po_CRMFieldSchemaName").setValue('My New Value'); 
 
//Hide/Show a tab/section
Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true); 
 
//Call the onchange event of a field
Xrm.Page.getAttribute("CRMFieldSchemaName").fireOnChange(); 
 
//Get the selected value of picklist
Xrm.Page.getAttribute("CRMFieldSchemaName").getSelectedOption().text; 
 
//Set the requirement level
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("none");
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("required");
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("recommended"); 
 
//Set the focus to a field
Xrm.Page.getControl("CRMFieldSchemaName").setFocus(true); 
 
//Stop an on save event
event.returnValue = false;
 
//Return array of strings of users security role GUIDs:
Xrm.Page.context.getUserRoles()

Hide/Show Tabs and Sections
function setVisibleTabSection(tabname, sectionname, show) {
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab != null) {
        if (sectionname == null)
            tab.setVisible(show);
        else {
            var section = tab.sections.get(sectionname);
            if (section != null) {
                section.setVisible(show);
                if (show)
                    tab.setVisible(show);
            }
        }
    }
}

//Load a window from a button
function callDialog() {
    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");
}

//how to clear all options in an OptionSet
Xrm.Page.getControl(“optionSet”).clearOptions();

//Save Options
// Equivalent to 'SAVE' button found on the entity form
Xrm.Page.data.entity.save();
// Equivalent to 'SAVE AND NEW' button found on the entity form
Xrm.Page.data.entity.save('saveandnew');
// Equivalent to 'SAVE AND CLOSE' button found on the entity form
Xrm.Page.data.entity.save('saveandclose');

/*
How to check form mode using Javascript?

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
*/
//Create mode
if (Xrm.Page.ui.getFormType() == 1) {
    //Do this
}
 
//Edit Mode
if (Xrm.Page.ui.getFormType() == 2) {
    // Do this
}

/*
How to get Parent Entity Id when a form is in create mode?

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. 
*/
//Create mode
if (Xrm.Page.ui.getFormType() == 1) {
 
    var qs = new Querystring();
    var Id = qs.get("_CreateFromId", "");
}

/*
How to set Lookups attributes?
*/
//Using the code in below line you can set the lookup for entity of type 2 i.e. Contacts only
document.getElementById("to").setAttribute("lookuptypes", "2");
 
//Using the code below you can set the default lookup type to Contacts
document.getElementById("to").setAttribute("defaulttype", "2");
/*
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.
*/
//Using the code below you can restrict the user for single value selection
document.getElementById("to").setAttribute("lookupstyle", "single");
 
//You can set the icons for selected lookup value using the code below
document.getElementById("to").setAttribute("lookuptypeIcons", "/_imgs/ico_16_2.gif");

/*
How to implement window.open functionality in Outlook?

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”.
*/ 
openStdWin("Url", "_blank", width, height, features);

/*
How to implement Oncheck event of checkbox?

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.
*/ 
// fire Oncheck event of checkbox
function CheckboxOnLoad() {
    crmForm.all.ink_applyvat.onclick = function () {
        crmForm.all.ink_applyvat.FireOnChange();
    };
}

/*
How to reload a parent form OnRefresh of a subgrid?

To reload a form OnRefresh event of its subgrid attach the following function to the onLoad event of the form
*/ 
//Reload parent form to reflect changes from related subgrid entity
var interval = null;
 
function OrderLoad() {
    interval = setInterval("SubGridRefresh();", 15000);
}
 
function SubGridRefresh() {
    var grid = document.getElementById("OrderProducts");
    if (grid) {
        grid.attachEvent("onrefresh", ReLoadForm);
        if (interval != null) {
            clearInterval(interval);
        }
    }
}
 
function ReLoadForm() {
    window.location.reload(true);
}

/*
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.
*/

以上是关于JavaScript 有用的Dynamics CRM 2011表格脚本花絮的主要内容,如果未能解决你的问题,请参考以下文章

转 Dynamics CRM Alert and Notification JavaScript Methods

JavaScript Dynamics CRM - 获取或设置值查找字段值

Dynamics 365中使用JavaScript和C#调用操作示例

Dynamics 365 无需频繁发布来开发和调试Javascript

Dynamics 365 无需频繁发布来开发和调试Javascript

Dynamics CRM Form表单中通过javascript抓取触发change事件字段的属性名