Dynamics 365 JavaScript Cheat Sheet - Simplify Your Development Efforts Part-2

Harshavardhan Pullata
0

 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:

javascript
formContext.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:

javascript
var 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:

javascript
var 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:

javascript
var 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:

javascript
var 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:

javascript
var attributeName = "new_optionsetfield"; // Replace with the name of the option set attribute formContext.data.entity.attributes.getByName(attributeName).getOptions();

Set Option Set Value:

javascript
var 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:

javascript
function formOnLoad(executionContext) { var formContext = executionContext.getFormContext(); // Your code here }

OnSave Event:

javascript
function formOnSave(executionContext) { var formContext = executionContext.getFormContext(); // Your code here }

6. Show/Hide and Enable/Disable Controls

Show a Control:

javascript
var controlName = "new_field"; // Replace with the name of the control formContext.getControl(controlName).setVisible(true);

Hide a Control:

javascript
var controlName = "new_field"; // Replace with the name of the control formContext.getControl(controlName).setVisible(false);

Enable a Control:

javascript
var controlName = "new_field"; // Replace with the name of the control formContext.getControl(controlName).setDisabled(false);

Disable a Control:

javascript
var controlName = "new_field"; // Replace with the name of the control formContext.getControl(controlName).setDisabled(true);

7. Working with Web Resources

Get Web Resource URL:

javascript
var webResourceName = "new_mywebresource"; // Replace with the name of the web resource var webResourceUrl = formContext.context.getClientUrl() + "/WebResources/" + webResourceName;

8. Form Notifications

Show Success Notification:

javascript
formContext.ui.setFormNotification("Record saved successfully.", "INFO", "successNotification");

Show Error Notification:

javascript
formContext.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!

Tags:

Post a Comment

0Comments

Post a Comment (0)