Introduction:
Are you a developer working with Dynamics 365 and struggling to keep track of all the essential JavaScript functions and methods? Look no further! In this blog post, we've put together a comprehensive Dynamics 365 JavaScript cheat sheet to streamline your development efforts and help you navigate through the intricacies of working with this powerful CRM platform. Whether you're a seasoned developer or just starting with Dynamics 365, this cheat sheet will be your go-to resource for writing efficient and effective JavaScript code.
1. Retrieving Data
Get a Single Record:
javascriptformContext.data.entity.getRecordId(); // Use the ID for the recordId parameter
formContext.data.entity.getEntityName(); // Use the entity name for the entityName parameter
Retrieve Multiple Records:
javascriptvar entityName = "account"; // Replace with the desired entity name
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='account'>" +
"<attribute name='name' />" +
"</entity>" +
"</fetch>";
formContext.data.webAPI.retrieveMultipleRecords(entityName, "?fetchXml=" + encodeURIComponent(fetchXml))
.then(successCallback, errorCallback);
2. Creating and Updating Records
Create a Record:
javascriptvar entityName = "account"; // Replace with the desired entity name
var data = {
"name": "New Account",
"accountnumber": "12345"
};
formContext.data.webAPI.createRecord(entityName, data)
.then(successCallback, errorCallback);
Update a Record:
javascriptvar recordId = "{GUID}"; // Replace with the GUID of the record to update
var entityName = "account"; // Replace with the desired entity name
var data = {
"name": "Updated Account Name",
"accountnumber": "54321"
};
formContext.data.webAPI.updateRecord(entityName, recordId, data)
.then(successCallback, errorCallback);
3. Deleting Records
Delete a Record:
javascriptvar recordId = "{GUID}"; // Replace with the GUID of the record to delete
var entityName = "account"; // Replace with the desired entity name
formContext.data.webAPI.deleteRecord(entityName, recordId)
.then(successCallback, errorCallback);
4. Working with Option Sets
Get the Option Set Values:
javascriptvar attributeName = "new_optionsetfield"; // Replace with the name of the option set attribute
formContext.data.entity.attributes.getByName(attributeName).getOptions();
Set Option Set Value:
javascriptvar attributeName = "new_optionsetfield"; // Replace with the name of the option set attribute
var optionSetValue = 2; // Replace with the desired option set value
formContext.data.entity.attributes.getByName(attributeName).setValue(optionSetValue);
5. Form Events
OnLoad Event:
javascriptfunction formOnLoad(executionContext) {
var formContext = executionContext.getFormContext();
// Your code here
}
OnSave Event:
javascriptfunction formOnSave(executionContext) {
var formContext = executionContext.getFormContext();
// Your code here
}
6. Show/Hide and Enable/Disable Controls
Show a Control:
javascriptvar controlName = "new_field"; // Replace with the name of the control
formContext.getControl(controlName).setVisible(true);
Hide a Control:
javascriptvar controlName = "new_field"; // Replace with the name of the control
formContext.getControl(controlName).setVisible(false);
Enable a Control:
javascriptvar controlName = "new_field"; // Replace with the name of the control
formContext.getControl(controlName).setDisabled(false);
Disable a Control:
javascriptvar controlName = "new_field"; // Replace with the name of the control
formContext.getControl(controlName).setDisabled(true);
7. Working with Web Resources
Get Web Resource URL:
javascriptvar webResourceName = "new_mywebresource"; // Replace with the name of the web resource
var webResourceUrl = formContext.context.getClientUrl() + "/WebResources/" + webResourceName;
8. Form Notifications
Show Success Notification:
javascriptformContext.ui.setFormNotification("Record saved successfully.", "INFO", "successNotification");
Show Error Notification:
javascriptformContext.ui.setFormNotification("An error occurred while processing your request.", "ERROR", "errorNotification");
Conclusion:
This Dynamics 365 JavaScript cheat sheet provides a quick reference to some of the most commonly used functions and methods when working with Dynamics 365. By using formContext
instead of Xrm.Page
, you'll stay up-to-date with the latest best practices and ensure your scripts work seamlessly across different Dynamics 365 environments.
Remember, this is just a starting point, and the possibilities with Dynamics 365 and JavaScript are vast. Always refer to the official Microsoft Dynamics 365 documentation for more in-depth information and stay updated with the latest releases.
Happy coding!