Showing posts with label D365 Javascript. Show all posts
Showing posts with label D365 Javascript. Show all posts

Monday, August 19, 2019

D365 Client API Object Model

The Client API object model for model-driven apps provides you objects and methods that you can use to apply custom business logic in model-driven apps using JavaScript, such as:
  • Get or set attribute values.
  • Show and hide user interface elements.
  • Reference multiple controls per attribute.
  • Access multiple forms per entity.
  • Manipulate form navigation items.
  • Interact with the business process flow control.

Root objects in the Client API object model

At the root of the Client API object model are the following contexts and the Xrm object:
ObjectDescription
executionContextRepresents the execution context for an event in model-driven apps forms and grids.
More information: Client API execution context

formContextProvides a reference to a form or an item on the form against which the current code executes. To get the formContext object, use the executionContext.getFormContext method.
More information: Client API form context
gridContextProvides a reference to a grid or a subgrid on a form against which the current code executes.
More information: Client API grid context
XrmProvides a global object for performing operations that do not directly impact the data and UI in forms, grids, subgrids, controls, or attributes. For example, navigate forms, create and manage records using Web API.
More information: Client API Xrm object





Execution Context :

Context which is passed when an event occurs on a form or grid which you can use it in your event handler to determine formContext, gridContext or manage the save event . It can be passed ext through UI or code:

Pass through UI – The execution context is an optional parameter that can be passed to a JavaScript library function through an event handler. Use the Pass execution context as first parameter option in the Handler Properties dialog while specify the name of the function to pass the event execution context. The execution context is the first parameter passed to a function.

Pass execution context



Pass through code – Event handlers which cannot be defined from UI, can be defined from code. The execution context is automatically passed as the first parameter to functions set using code. For example:
addOnChange – formContext.getAttribute(arg).addOnChange(myFunction)

removeOnChange – formContext.getAttribute(arg).removeOnChange(myFunction)

FUNCTION MYFUNCTION(EXECUTIONCONTEXT)
{
//FIRST PARAMETER PASSED AS EXECUTION CONTEXT
}


Previously 
function displayName()

{

var firstName = Xrm.Page.getAttribute(“firstname”).getValue();

var lastName = Xrm.Page.getAttribute(“lastname”).getValue();

console.log(firstName + ” ” + lastName);

}

Now
function displayName(executionContext)

{

var formContext = executionContext.getFormContext(); // get formContext

// use formContext instead of Xrm.Page

var firstName = formContext.getAttribute(“firstname”).getValue();

var lastName = formContext.getAttribute(“lastname”).getValue();

console.log(firstName + ” ” + lastName);

}

Form Context 
provides a reference to the form or to an item on the form, such as, a quick view control or a row in an editable grid, against which the current code is executed.
Earlier, the global Xrm.Page object was used to represent a form or an item on the form. With the latest version, the Xrm.Page object is deprecated, and you should use the getFormContext method of the passed in execution context object to return reference to the appropriate form or an item on the form.

Use the data and ui objects under the formContext object to programmatically manipulate data and user interface elements in model-driven apps.
formContext object model


Grid Context 

Context provides reference to grid or sub-grid on form against which code is executed. Grid context can be retrieved from execution context based on where code is being executed. Grid context also replaced Xrm.Page which was used to represent grid or sub grid on form earlier.


Previously 

 function doSomething()
 { var contactsSubgrid = Xrm.Page.getControl(“Contacts”);
 }

 Now 

1) Code is executed on form event

 function doSomething(executionContext)
 { var formContext = executionContext.getFormContext();
// get the form Context
var gridContext = formContext.getControl(“Contacts”);
// get the grid context
 // Perform operations on the subgrid

 }

 2) Code is executed on grid event

 function doSomething(executionContext)
 {
 var formContext = executionContext.getFormContext();
// get the form Context
var gridContext = formContext.getControl(“Contacts”);
// get the grid context
 // Perform operations on the subgrid
 }

Previously

 Xrm.Page.getControl(“Contacts”).getGrid();  // Grid
Xrm.Page.getControl(“Contacts”).getGrid().getRows(); // GridRows
Xrm.Page.getControl(“Contacts”).getGrid().getRows(); // GridRowData
Xrm.Page.getControl(“Contacts”).getGrid().getRows().get(0).getEntity()  // GridEntity

 Now 
 gridContext.getGrid();
gridContext.getGrid().getRows();
gridContext.getGrid().getRows().get(0).data
gridContext.getGrid().getRows().get(0).entity


Xrm Object

 Xrm object is globally available which provides methods to use device capabilities, navigation settings, information specific to organization or user and offline capabilities without having to use execution context in Client API. In this release Xrm object is updated to have new namespaces and new APIs in existing namespaces.

J5
New Namespaces:

Device – Provides methods to use native device capabilities of mobile devices.

  • captureAudio
  • captureImage
  • captureVideo
  • getBarcodeValue
  • getCurrentPosition
  • pickFile


Encoding – Provides methods to encode strings.

  • xmlAttributeEncode
  • xmlEncode

Navigation – Provides methods for navigating forms and items in Customer Engagement.

  • openAlertDialog
  • openConfirmDialog
  • openErrorDialog
  • openFile
  • openForm
  • openUrl
  • openWebResource

WebApi – Provides methods to use Web API to create, manage records and execute Web API actions and functions. Comes with offline capabilities also for mobile clients.

          Properties:

online – perform actions or functions when connect to server (online mode)

offline – perform actions or functions when offline with mobile clients

          Methods: 


  • createRecord – Creates an entity record
  • deleteRecord – Deletes an entity record
  • retrieveRecord – Retrieves an entity record
  • retrieveMultipleRecords – Retrieves a collection of entity records
  • updateRecord – Updates an entity record
  • isAvaiableOffline – Returns a boolean value indicating whether an entity is                            present in user’s profile and is currently available for use in offline mode
  • execute – Execute a single action, function, or CRUD operation
  • executeMultiple – Execute a collection of action, function, or CRUD operations

Source: https://docs.microsoft.com/en-gb/powerapps/developer/model-driven-apps/clientapi/understand-clientapi-object-model

Javascript differences between CRM and D365 CRM


Deprecated Client API
Replacement Client API
Comments
Forms: ExecutionContext.getFormContext 
Commands: Send it as the PrimaryControl parameter
Use of the Xrm.Page object as a static access to the primary form context is still supported to maintain backward compatibility with the existing scripts. Based on the feedback, we understand that the usage of Xrm.Page is high, and it won’t be removed as soon as some other client API methods listed in this section. We encourage you to use the new way of getting form content where possible. More information: Client API form context
Although Xrm.Page is deprecated, parent.Xrm.Page will continue to work in case of HTML web resources embedded in forms as this is the only way to access the form context from the HTML web resource.
Xrm.Utility.getGlobalContext
Allows access to the global context without going through the form context.
formContext.data.attributes
The formContext.data.attributes API will make retrieval of non-entity bound data consistent across entity forms, metadata-driven dialogs, and task-based flows. The data will be a combination of custom values sent using the query string and what was specified in the parameters in the openForm method.
globalContext.userSettings.getTimeZoneOffsetMinutes
Moved to globalContext.userSettings
globalContext.userSettings.userId
Moved to globalContext.userSettings
globalContext.userSetings.languageId
Moved to globalContext.userSettings
globalContext.userSettings.userName
Moved to globalContext.userSettings
globalContext.userSettings.securityRoles
Moved to globalContext.userSettings
globalContext.organizationSettings.isAutoSaveEnabled
Moved to globalContext.organizationSettings
globalContext.organizationSettings.languageId
Moved to globalContext.organizationSettings
globalContext.organizationSettings.uniqueName
Moved to globalContext.organizationSettings
No change in the method, but use "typename" instead of type for lookup attributes.

GridRow.data
GridRow is essentially a form context. This change unifies the interface of GridRow with formContext.
GridRowData.entity
GridRowData is form data. This change unifies the interface of GridRowData with formContextData.
Xrm.Mobile.offline
Moved the offline-related methods under Xrm.WebApi.offline

Earlier: An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility. 
Now: parent.Xrm.* will work if the HTML web resource is loaded in a form container. It won't work for HTML web resources that are stand alone, or referenced from the site map or any other places.
addOnKeyPress
Use a custom control

removeOnKeyPress
Use a custom control

showAutoComplete
Use a custom control and corresponding UI

hideAutoComplete
Use a custom control and corresponding UI

Xrm.Navigation.openAlertDialog
The new signature is consistent with other APIs (openForm) and takes a new set of parameters for flexibility.
Xrm.Navigation.openConfirmDialog
The new signature is consistent with other APIs (openForm) and takes a new set of parameters for flexibility.
Xrm.Utility.getEntityMetadata
The isActivityType method is synchronous so it was suitable for ribbon rules. However, the replacement method, getEntityMetadata, is asynchronous, and is not suitable for ribbon rules.
Xrm.Navigation.openForm
Moving navigation actions to Xrm.Navigation
Xrm.Navigation.openForm
Moving navigation actions to Xrm.Navigation
Xrm.Navigation.openWebResource
Moving navigation actions to Xrm.Navigation 
Note: This API returns VOID in Unified Interface.