2014年12月30日 星期二

CRM 2011 Set Owner in JavaScript

CRM 2011 Set Owner in JavaScript



JavaScript:
function OnLoadForm()
{
    setApprovalOwner()
}
function setApprovalOwner()
{
    var setUservalue = new Array();
    setUservalue[0] = new Object();
    setUservalue[0].id = Xrm.Page.context.getUserId();
    setUservalue[0].entityType = 'systemuser';
    //setUservalue[0].name = retrievedUser.FullName;

    Xrm.Page.getAttribute("new_approvalowner").setValue(setUservalue)
}

https://crmbusiness.wordpress.com/2012/03/21/crm-2011-setting-a-user-lookup-with-the-logged-in-user-with-javascript/

CRM 2011 Set Option Set Value in JavaScript

CRM 2011 Set Option Set Value in JavaScript



JavaScript:
function OnLoadForm()
{
    ApproveQuote();
}
function ApproveQuote()
{
    control = Xrm.Page.getControl("new_approvalstatus");
    attribute = control.getAttribute();
    attribute.setValue('271770002');
}

CRM 2011 Set DateTime Value in JavaScript


CRM 2011 Set DateTime Value in JavaScript




JavaScript:
function OnLoadForm()
{
    confirmApproval();
    // OR
    confirmApproval2();
}
function confirmApproval()
{
   var currentDateTime = new Date();
   Xrm.Page.getAttribute("new_approvaldate").setValue(currentDateTime);
}
function confirmApproval2()
{
   var currentDateTime = new Date();
   control = Xrm.Page.getControl("new_approvalddate");
   attribute = control.getAttribute();
   attribute.setValue(currentDateTime);
}

CRM 2011 Set Two Options Value in JavaScript

CRM 2011 Set Two Options Value in JavaScript




JavaScript:
function OnLoadForm()
{
    isApproval();
}
function isApproval()
{
    control = Xrm.Page.getControl("new_isapproval");
    attribute = control.getAttribute();
    attribute.setValue(true);
}

CRM 2011 Get Owner ID in JavaScript

CRM 2011 Get Owner ID in JavaScript



JavaScript:
function OnLoadForm()
{
    var OwnerId = GetLookup_OwnerId();
}
function GetLookup_OwnerId()
{
   var lookup = new Array();
   lookup = Xrm.Page.getAttribute("ownerid").getValue();
   if (lookup != null) {
      var name = lookup[0].name;
       var id = lookup[0].id;
       var entityType = lookup[0].entityType;
       return id;
   }
}

CRM 2011 Get Current User ID in JavaScript

CRM 2011 Get Current User ID in JavaScript

JavaScript:
function OnLoadForm()
{
   var userId = Xrm.Page.context.getUserId();
}

Other Sample(Ref Web):
function getCurrentUserFullName() {
    var serverUrl;
    if (Xrm.Page.context.getClientUrl !== undefined) {
        serverUrl = Xrm.Page.context.getClientUrl();
    } else {
        serverUrl = Xrm.Page.context.getServerUrl();
    }
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var userRequest = new XMLHttpRequest();
    userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);
    userRequest.setRequestHeader("Accept", "application/json");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    if (userRequest.status === 200) {
        var retrievedUser = JSON.parse(userRequest.responseText).d;
        var userFullName = retrievedUser.FullName;
        return userFullName;
    }
    else {
        return "error";
    }
}
http://msdn.microsoft.com/en-us/library/gg334511.aspx

CRM 2011 Hide Custom Ribbon Button in JavaScript

CRM 2011 Hide Custom Ribbon Button in JavaScript

↓↓↓


JavaScript:
function OnLoadForm()
{
    HideApprovalButton();
}
function HideApprovalButton()
{
    var btnApprove=top.document.getElementById("quote|NoRelationship|Form|Mscrm.Form.quote.MainTab.Management.Approve-Large");
    var btnReject=top.document.getElementById("quote|NoRelationship|Form|Mscrm.Form.quote.MainTab.Management.Reject-Large");
    btnApprove.style.display='none';
    btnReject.style.display='none';
}

Other Sample(Ref web)
function HideRibbonControl(formName) 
{
    var saveButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Save-Large";
    var saveandcloseButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".SaveAndClose-Large";
    var saveandnewButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".SaveandNew-Medium";
    var deactivateButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Deactivate-Medium";
    var deleteButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Delete-Medium";
    var approveButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Approve-Large";

    HideARibbonButton(saveButtonID);
    HideARibbonButton(saveandcloseButtonID);
    HideARibbonButton(saveandnewButtonID);
    HideARibbonButton(deactivateButtonID);
    HideARibbonButton(deleteButtonID);
    HideARibbonButton(approveButtonID);
}
function HideARibbonButton(nameOfButton) 
{
   var btn = window.top.document.getElementById(nameOfButton);
   var intervalId = window.setInterval(function () {
       if (btn != null) {
           window.clearInterval(intervalId);
           btn.disabled = true;
           btn.style.display='none';
       }

    }, 50);
}