Skip to content

Instantly share code, notes, and snippets.

View DynCon365's full-sized avatar

Dynamic Consulting DynCon365

View GitHub Profile
@DynCon365
DynCon365 / gist:b55037366bf9fc7ada2e628b36d766b3
Created April 4, 2018 16:28
Dynamics 365/CRM | Set Date field with date from OData query
// Set Date field with date from OData query
// Set a variable with the value of the date field from OData query (query not shown here)
var date = data.d.results[0].DateField;
if (date != null) {
// Format the date field
var formattedDate = new Date(parseInt(date.replace("/Date(", "").replace(")/", ""), 10));
Xrm.Page.data.entity.attributes.get("new_datefield").setValue(formattedDate);
}
@DynCon365
DynCon365 / gist:3d5078ddf686ca429159ba14c5f60138
Last active April 4, 2018 16:34
Dynamics 365/CRM | Entity Set Results
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<service xmlns="http://www.w3.org/2007/app" xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="https://yourorg.crm.dynamics.com/XRMServices/2011/OrganizationData.svc/">
-<workspace>
<atom:title>Default</atom:title>
-<collection href="OpportunityCompetitorsSet">
<atom:title>OpportunityCompetitorsSet</atom:title>
</collection>
-<collection href="SystemFormSet">
@DynCon365
DynCon365 / gist:87eba60b974df5d4fed83acf6bdd6b3e
Last active April 4, 2018 16:44
Dynamics 365/CRM | Pull data from another entity
//-------------------------------------------------------------------------------------------
// getContactData
// Query of the Contact entity from the Opportunity, based on Primary Contact
//-------------------------------------------------------------------------------------------
function getContactData() {
// Get Contact object
try {var contactObject = Xrm.Page.getAttribute("parentcontactid");}
catch(err){}
@DynCon365
DynCon365 / gist:401257b5931867c737c0b72c9f836886
Last active April 4, 2018 16:55
Dynamics 365/CRM | Open Specific Form based on Option Set field
function showForm() {
//if the form is update form
if (Xrm.Page.ui.getFormType()==2)
// variable to store the name of the form
var lblForm;
// get the value picklist field
var relType = Xrm.Page.getAttribute("customertypecode").getValue();
@DynCon365
DynCon365 / gist:1a2b932cea846db94325f457d71208a5
Last active April 26, 2018 16:24
Dynamics 365/CRM | Use the value of an Option Set to set up a custom form layout
// Use the value of an Option Set to set up a custom form layout
// This example assumes a company uses the same Opportunity form,
// but shows/hides sections and makes fields required/optional based
// on the value of a new_division field
// Consider using Business Rules before using this code
function opportunity_onLoad () {
// hook this up to the onLoad event and the onChange event of the new_division field
@DynCon365
DynCon365 / gist:38c564f1ec4a233a701014dbedc3fa3d
Last active April 24, 2018 16:52
Dynamics 365/CRM | Set required level of a field
// Consider using Business Rules before using this code
// Set Opportunity Est. Close Date as optional
Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("none");
// Set Opportunity Est. Close Date as required
Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("required");
@DynCon365
DynCon365 / gist:8faaf8705fd709d7da2de86da7e1ed3f
Last active April 24, 2018 16:51
Dynamics 365/CRM | Show/hide form sections
// Consider using Business Rules before using this code
// Create variables for the tab an section
var tab=Xrm.Page.ui.tabs.get("TabName");
var section = tab.sections.get("SectionName");
// Hide a section
section.setVisible(false);
// Show a section
@DynCon365
DynCon365 / gist:64c47edef24d9c4207d8a305a0b7efce
Created April 4, 2018 17:16
Dynamics 365/CRM | Get the value of a Date field (01/31/2018 or 31/01/2018)
// Get the value of the Opportunity Est. Close Date
var date = Xrm.Page.data.entity.attributes.get("estimatedclosedate").getValue();
if (date != null) {
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
month++;
// Add leading 0's to single digit days and months
if (day < 10) {day = "0" + day;}
@DynCon365
DynCon365 / gist:bcdcaba61bd6051116eed7bcb7a4c11f
Last active April 24, 2018 16:47
Dynamics 365/CRM | Set the value of an Option Set
// Set the value of the Opportunity Status Reason field
// Note: Option Set fields are set based on the database id, rather than the name
Xrm.Page.data.entity.attributes.get("statuscode").setValue(100000001);
// Set the value of the Opportunity Status Reason field to a variable
var statusReasonId = 100000001;
Xrm.Page.data.entity.attributes.get("statuscode").setValue(statusReasonId);
@DynCon365
DynCon365 / gist:ae22db16c6ebf2a9cddcdd7f10a53d27
Created April 4, 2018 17:41
Dynamics 365/CRM | Get value of a text field
// Get the value of a Contact's first name
var firstName = Xrm.Page.data.entity.attributes.get("firstname").getValue();