2014年12月30日 星期二

CRM 2011 Set Owner in JavaScript

CRM 2011 Set Owner in JavaScript



JavaScript:
  1. function OnLoadForm()
  2. {
  3. setApprovalOwner()
  4. }
  1. function setApprovalOwner()
  2. {
  3. var setUservalue = new Array();
  4. setUservalue[0] = new Object();
  5. setUservalue[0].id = Xrm.Page.context.getUserId();
  6. setUservalue[0].entityType = 'systemuser';
  7. //setUservalue[0].name = retrievedUser.FullName;
  8.  
  9. Xrm.Page.getAttribute("new_approvalowner").setValue(setUservalue)
  10. }

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:
  1. function OnLoadForm()
  2. {
  3. ApproveQuote();
  4. }
  1. function ApproveQuote()
  2. {
  3. control = Xrm.Page.getControl("new_approvalstatus");
  4. attribute = control.getAttribute();
  5. attribute.setValue('271770002');
  6. }

CRM 2011 Set DateTime Value in JavaScript


CRM 2011 Set DateTime Value in JavaScript




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

CRM 2011 Set Two Options Value in JavaScript

CRM 2011 Set Two Options Value in JavaScript




JavaScript:
  1. function OnLoadForm()
  2. {
  3. isApproval();
  4. }
  1. function isApproval()
  2. {
  3. control = Xrm.Page.getControl("new_isapproval");
  4. attribute = control.getAttribute();
  5. attribute.setValue(true);
  6. }

CRM 2011 Get Owner ID in JavaScript

CRM 2011 Get Owner ID in JavaScript



JavaScript:
  1. function OnLoadForm()
  2. {
  3. var OwnerId = GetLookup_OwnerId();
  4. }
  1. function GetLookup_OwnerId()
  2. {
  3. var lookup = new Array();
  4. lookup = Xrm.Page.getAttribute("ownerid").getValue();
  5. if (lookup != null) {
  6. var name = lookup[0].name;
  7. var id = lookup[0].id;
  8. var entityType = lookup[0].entityType;
  9. return id;
  10. }
  11. }

CRM 2011 Get Current User ID in JavaScript

CRM 2011 Get Current User ID in JavaScript

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

Other Sample(Ref Web):
  1. function getCurrentUserFullName() {
  2. var serverUrl;
  3. if (Xrm.Page.context.getClientUrl !== undefined) {
  4. serverUrl = Xrm.Page.context.getClientUrl();
  5. } else {
  6. serverUrl = Xrm.Page.context.getServerUrl();
  7. }
  8. var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
  9. var userRequest = new XMLHttpRequest();
  10. userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);
  11. userRequest.setRequestHeader("Accept", "application/json");
  12. userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  13. userRequest.send();
  14. if (userRequest.status === 200) {
  15. var retrievedUser = JSON.parse(userRequest.responseText).d;
  16. var userFullName = retrievedUser.FullName;
  17. return userFullName;
  18. }
  19. else {
  20. return "error";
  21. }
  22. }
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:
  1. function OnLoadForm()
  2. {
  3. HideApprovalButton();
  4. }
  1. function HideApprovalButton()
  2. {
  3. var btnApprove=top.document.getElementById("quote|NoRelationship|Form|Mscrm.Form.quote.MainTab.Management.Approve-Large");
  4. var btnReject=top.document.getElementById("quote|NoRelationship|Form|Mscrm.Form.quote.MainTab.Management.Reject-Large");
  5. btnApprove.style.display='none';
  6. btnReject.style.display='none';
  7. }

Other Sample(Ref web)
  1. function HideRibbonControl(formName)
  2. {
  3. var saveButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Save-Large";
  4. var saveandcloseButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".SaveAndClose-Large";
  5. var saveandnewButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".SaveandNew-Medium";
  6. var deactivateButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Deactivate-Medium";
  7. var deleteButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Delete-Medium";
  8. var approveButtonID = formName + "|NoRelationship|Form|Mscrm.Form." + formName + ".Approve-Large";
  9.  
  10. HideARibbonButton(saveButtonID);
  11. HideARibbonButton(saveandcloseButtonID);
  12. HideARibbonButton(saveandnewButtonID);
  13. HideARibbonButton(deactivateButtonID);
  14. HideARibbonButton(deleteButtonID);
  15. HideARibbonButton(approveButtonID);
  16. }
  1. function HideARibbonButton(nameOfButton)
  2. {
  3. var btn = window.top.document.getElementById(nameOfButton);
  4. var intervalId = window.setInterval(function () {
  5. if (btn != null) {
  6. window.clearInterval(intervalId);
  7. btn.disabled = true;
  8. btn.style.display='none';
  9. }
  10.  
  11. }, 50);
  12. }