How to get the Current Organization Settings using JavaScript in Dynamics 365 V9.X?

Below is the New Syntax available to get Current Organization Settings in Dynamics 365 V9.X,

var organizationSettings = Xrm.Utility.getGlobalContext().organizationSettings;

The organizationSettings object provides following propertiSELRES_9e827887-e80e-4701-803c-ed9d51c22944SELRES_cb9358a4-783e-469a-80a0-29a2ac8ee3f7SELRES_b890e71f-c06b-4ce9-a24a-a17bfb5db450SELRES_a4fc1d9c-9f44-47c2-83a9-985e52e8f7a3SELRES_e24bf811-7ea2-4a3b-944f-f6e83797109aSELRES_7c5468be-ec29-4be3-839e-54997f6da776SELRES_17d15528-4b44-4ddb-b280-8a186a56999fSELRES_2528afac-6b72-49d3-836d-6498b40d5dc6SELRES_2528afac-6b72-49d3-836d-6498b40d5dc6SELRES_17d15528-4b44-4ddb-b280-8a186a56999fSELRES_7c5468be-ec29-4be3-839e-54997f6da776SELRES_e24bf811-7ea2-4a3b-944f-f6e83797109aSELRES_a4fc1d9c-9f44-47c2-83a9-985e52e8f7a3SELRES_b890e71f-c06b-4ce9-a24a-a17bfb5db450SELRES_cb9358a4-783e-469a-80a0-29a2ac8ee3f7SELRES_9e827887-e80e-4701-803c-ed9d51c22944es.

Syntax Type Description
organizationSettings.attributes Object An object with attributes and their values.
organizationSettings.baseCurrencyId String ID of the base currency.
organizationSettings.defaultCountryCode String Default country/region code for phone numbers.
organizationSettings.isAutoSaveEnabled Boolean true if enabled; false otherwise.
organizationSettings.languageId Number Preferred Language ID. For example: 1033 for English
organizationSettings.organizationId String Id of the current organization.
organizationSettings.uniqueName String Unique name of the current organization.
organizationSettings.useSkypeProtocol Boolean true if Skype protocol is used; false otherwise.

Example: Below function configured in Account Page Load.

function GetOrganizationDetails() {
 var outputText = "Organization Details\n--------------------------------------------\n";
 var organizationSettings = Xrm.Utility.getGlobalContext().organizationSettings;

outputText += "Unique Name : " + organizationSettings.uniqueName +"\n";
 outputText += "Id : " + organizationSettings.organizationId + "\n";
 outputText += "Language Id : " + organizationSettings.languageId + "\n";
 outputText += "Base Currency Id : " + organizationSettings.baseCurrencyId + "\n";
 outputText += "Default Country Code : " + organizationSettings.defaultCountryCode + "\n";
 outputText += "Is Auto Save Enabled : " + organizationSettings.isAutoSaveEnabled + "\n";
 outputText += "Use Skype Protocol : " + organizationSettings.useSkypeProtocol + "\n";
 outputText += "Attributes : " + organizationSettings.attributes;

Xrm.Utility.alertDialog(outputText, null); 
}

Output:

Organization Details
Current Organization Settings
Organization Details JS Debug
Debug Mode

Hope you learned a new thing today :):):)

 

How to use Barcode scanner feature in Dynamics 365 for Phones App using Javascript?

Invokes the device camera to scan the barcode information, such as a product number.

Syntax:

Xrm.Device.getBarcodeValue().then(successCallback, errorCallback);

Parameters:

Parameter Name Type Required Description
successCallback Function Yes A function to call when the barcode value is returned as a String.
errorCallback Function Yes A function to call when the operation fails. An error object with the message property (String) will be passed that describes the error details.

Return Value: On success, returns a string containing the scanned barcode value.

Note: This method is supported only for the mobile clients.

Example: Created a Scan Barcode button on Account Form and configured the below function on OnClick of the button.

function GetBarcodeValue() {
 Xrm.Device.getBarcodeValue().then(
 function success(result) {
 Xrm.Navigation.openAlertDialog({ text: "Barcode value: " + result });
 },
 function (error) {
 Xrm.Navigation.openAlertDialog({ text: error.message });
 }
 );
}

Output:

1.Open Dynamics 365 for Phones App in Mobile or Tablet,

2.Navigate to Accounts and open any Account record and Tap on commands (…).

3.Tap on Scan Barcode.

Screenshot_2017-12-28-10-19-52-172_com.microsoft.crm.crmphone

4.Mobile inbuilt Barcode Scanner app will be opened. Place a Bar Code and scan for it.

Screenshot_2017-12-28-13-13-00-570_com.microsoft.crm.crmphone

5.Once the App identifies the Barcode correctly, it will capture the Barcode Value.

Screenshot_2017-12-28-13-08-00-481_com.microsoft.crm.crmphone

Hope you learned a new thing today :):):)

 

How to use Progress Indicator in Dynamics 365 CRM Online V9.X?

1. Show Progress Indicator:

It Displays a progress dialog with the specified message.

Any subsequent call to this method will update the displayed message in the existing progress dialog with the message specified in the latest method call.

Warning:

The progress dialog blocks the execution until it is closed using the closeProgressIndicator method. So, you must use this method with caution.

Syntax:

Xrm.Utility.showProgressIndicator(message)

Parameters:

Name Type Required Description
Message String Yes The message to be displayed in the progress dialog.

2. Close Progress Indicator:

It Closes a progress dialog box.

If no progress dialog is displayed currently, this method will do nothing. You can display a progress dialog using the showProgressIndicator method.

Syntax:

Xrm.Utility.closeProgressIndicator()

Example: Used the below code and configured ShowProgressIndicator() in Account Onload.

function ShowProgressIndicator() {
 Xrm.Utility.showProgressIndicator("Show Progress Indicator Demo..........................");
 setTimeout('HideProgressIndicator()', 5000);
}

function HideProgressIndicator() {
 Xrm.Utility.closeProgressIndicator();
}

Output: Open any Existing Account Record, Progress Indicator Text is shown and after 5 Seconds, it will be disappeared.

Show Progress Indicator

Hope you learned a new thing today :):):)

Show Ribbon Buttons Only in Mobile Or Tablets in CRM

Configure the below Enable Rule for the required ribbon buttons to show Only in Mobile/ Tablets and not in CRM Web.

Show Or Hide Ribbon Buttons in Mobile or Tablets

Use the below function,

function ShowOrHideDeviceButtons() {
 var showOrHideFlag = false;
 // Form Factor = 2 - Tablet
 // Form Factor = 3 - Phone
 if (Xrm.Page.context.client.getClient() == "Mobile" && (Xrm.Page.context.client.getFormFactor() == 2 || Xrm.Page.context.client.getFormFactor() == 3)) {
 // Add code that should only run in CRM for phones here
 showOrHideFlag = true;
 }
 return showOrHideFlag;
}

Output:

Dynamics 365 for phones App View: Highlighted ribbon buttons can be seen only in Mobile/ Tablet.

Show Or Hide Ribbon Buttons in Mobile or Tablets in CRM App

CRM Web View: Buttons are not displayed in CRM Web View.

Show Or Hide Ribbon Buttons in Mobile or Tablets in CRM Web

Hope you learned a new thing today :):):)

 

How to delete a record in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Below is the latest Syntax available to delete a record in Dynamics 365 Online V9.X using JavaScript,

Syntax:

Xrm.WebApi.deleteRecord(entityLogicalName, id).then(successCallback, errorCallback);

Parameters:

Name Type Required Description
entityLogicalName String Yes The entity logical name of the record you want to delete. For example: “account”.
id String Yes GUID of the entity record you want to delete.
successCallback Function No A function to call when a record is deleted. An object with the following properties will be passed to identify the deleted record:

·        entityType: String. The entity type of the record.·        id: String. GUID of the record.

·        name: String. Name of the record.

errorCallback Function No A function to call when the operation fails.

Return Value:
On success, returns a promise object containing the attributes specified earlier in the description of the successCallback parameter.

Example:
Create a new Javascript Webresource (new_Account.js) and copy paste the below code in it and call it on OnLoad of the Account Form.

After that, open an existing Account record.

function DeleteRecord() {
 // Delete Account record
 Xrm.WebApi.deleteRecord("account", "9C36DC38-79E9-E711-A95E-000D3AF27CC8").then(
 function success(result) {
 // Perform operations on record deletion
 Xrm.Utility.alertDialog("Account deleted", null);
 },
 function (error) {
 // Handle error conditions
 Xrm.Utility.alertDialog(error.message, null);
 });
}

Output:

Dynamics 365 Record Deletion V9.X

Hope you have successfully Deleted an Account record using New Syntax.

How to retrieve a record in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Below is the latest Syntax available to retrieve a record in Dynamics 365 Online V9.X using JavaScript,

Syntax:

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

Parameters:

Name Type Required Description
entityLogicalName String Yes The entity logical name of the record you want to retrieve. For example: “account”.
id String Yes GUID of the entity record you want to retrieve.
options String No OData system query options, $select and $expand, to retrieve your data.

·        Use the $select system query option to limit the properties returned by including a comma-separated list of property names. This is an important performance best practice. If properties aren’t specified using $select, all properties will be returned.

·        Use the $expand system query option to control what data from related entities is returned. If you just include the name of the navigation property, you’ll receive all the properties for related records. You can limit the properties returned for related records using the $select system query option in parentheses after the navigation property name. Use this for both single-valued and collection-valued navigation properties.

You specify the query options starting with ?. You can also specify multiple query options by using & to separate the query options. For example:

?$select=name&$expand=primarycontactid($select=contactid,fullname)

See examples later in this topic to see how you can define the options parameter for various retrieve scenarios.

successCallback Function No A function to call when a record is retrieved. A JSON object with the retrieved properties and values will be passed to the function.
errorCallback Function No A function to call when the operation fails.

Return Value:
On success, returns a promise containing a JSON object with the retrieved attributes and their values.

Example:
Create a new Javascript Webresource (new_Account.js) and copy paste the below code in it and call it on OnLoad of the Account Form.

After that, open an Existing Account Record.

function RetrieveAccountRecord() {
 var accountRecordGuid = null, nextLine = "\n";
 accountRecordGuid = Xrm.Page.data.entity.getId();

if (accountRecordGuid != null && accountRecordGuid != "") {
 // Retrieve Record
 Xrm.WebApi.retrieveRecord("account", accountRecordGuid, "?$select=name,creditonhold,description,revenue,industrycode,_primarycontactid_value").then(
 function success(result) {
 // Perform operations on record retrieval
 var sampleValueText = "Retrieved values: ";

// Single Line of Text
 if (result.name != null) {
 sampleValueText += "Account Name : " + result.name + nextLine;
 }

// Two Option Set
 if (result.creditonhold != null) {
 sampleValueText += "CreditOnHold : " + result.creditonhold + nextLine;
 }

// Multiple Lines of Text
 if (result.description != null) {
 sampleValueText += "Description : " + result.description + nextLine;
 }

// Currency Field
 if (result.revenue != null) {
 sampleValueText += "Revenue : " + result.revenue + nextLine;
 sampleValueText += "Revenue (Formatted Text) : " + result['revenue@OData.Community.Display.V1.FormattedValue'] + nextLine;
 }

// Option Set
 if (result.industrycode != null) {
 sampleValueText += "Industry Text : " + result['industrycode@OData.Community.Display.V1.FormattedValue'] + nextLine;
 sampleValueText += "Industry Value : " + result.industrycode + nextLine;
 }

// Lookup
 if (result._primarycontactid_value != null) {
 sampleValueText += "Primary Contact GUID: " + result._primarycontactid_value + nextLine;
 sampleValueText += "Primary Contact Logical Name: " + result['_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname'] + nextLine;
 sampleValueText += "Primary Contact Text: " + result['_primarycontactid_value@OData.Community.Display.V1.FormattedValue'] + nextLine;
 }

Xrm.Utility.alertDialog(sampleValueText, null);
 },
 function (error) {
 // handle error conditions
 Xrm.Utility.alertDialog("Error while retrieving the Account Record : " + error.message, null);
 }
 );
 }
}

Output:
Dynamics 365 Retrieve Record V9.X

Hope you have successfully retrieved Account record using New Syntax.

How to retrieve multiple records in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Below is the latest Syntax available to retrieve multiple records in Dynamics 365 Online V9.X using JavaScript,

Syntax:

Xrm.WebApi.retrieveMultipleRecords(entityLogicalName, options, maxPageSize).then(successCallback, errorCallback);

Parameters:

Name Type Required Description
entityLogicalName String Yes The entity logical name of the records you want to retrieve. For example: “account”.
options String No OData system query options or FetchXML query to retrieve your data.

·        Following system query options are supported: $select, $top, $filter, $expand, and $orderby.

·        To specify a FetchXML query, use the fetchXml attribute to specify the query.

NOTE: You must always use the $select system query option to limit the properties returned for an entity record by including a comma-separated list of property names. This is an important performance best practice. If properties aren’t specified using $select, all properties will be returned.

You specify the query options starting with ?. You can also specify multiple system query options by using & to separate the query options.

See examples later in this topic to see how you can define the options parameter for various retrieve multiple scenarios.

maxPageSize Number No Specify a positive number that indicates the number of entity records to be returned per page. If you do not specify this parameter, the default value is passed as 5000.

If the number of records being retrieved is more than the specified maxPageSize value, nextLink attribute in the returned promise object will contain a link to retrieve the next set of entities.

successCallback Function No A function to call when entity records are retrived. An object with the following attributes is passed to the function:1

·        entities: An array of JSON objects, where each object represents the retrieved entity record containing attributes and their values as key: value pairs. The Id of the entity record is retrieved by default.

·        nextLink: String. If the number of records being retrieved is more than the value specified in the maxPageSize paramter, this attribute returns the URL to return next set of records.

errorCallback Function No A function to call when the operation fails.

Return Value:
On success, returns a promise that contains an array of JSON objects (entities) containing the retrieved entity records and the nextLink attribute (optional) with the URL pointing to next set of records in case paging (maxPageSize) is specified and the record count returned exceeds the paging value.

Example:
Create a new Javascript Webresource (new_Account.js) and copy paste the below code in it and call it on OnLoad of the Account Form.

After that, open an existing account record.

function RetrieveAllAccountRecords() {
 var outputText = "Account Name\t\t\tPhone\n---------------------------------------------------\n";
 Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name,telephone1").then(
 function success(result) {
 for (var accountRecordsCount = 0; accountRecordsCount < result.entities.length; accountRecordsCount++) {
 outputText += result.entities[accountRecordsCount].name + "\t\t" + result.entities[accountRecordsCount].telephone1 + "\n";
 }
 Xrm.Utility.alertDialog(outputText, null);
 },
 function (error) {
 // Handle error conditions
 Xrm.Utility.alertDialog(error.message, null);
 });
}

Output:

Dynamics 365 Retrieve All Records V9.X

Hope you have successfully retrieved all Account records using New Syntax.

How to update a record in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Below is the latest Syntax available to update a record in Dynamics 365 Online V9.X using JavaScript,

Syntax:

Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback);

Parameters:

Name Type Required Description
entityLogicalName String Yes The entity logical name of the record you want to update. For example: “account”.
id String Yes GUID of the entity record you want to update.
data Object Yes A JSON object containing key: value pairs, where key is the property of the entity and value is the value of the property you want to update.

See examples later in this topic to see how you can define the data object for various update scenarios.

successCallback Function No A function to call when a record is updated. An object with the following properties will be passed to identify the updated record:

·        entityType: String. The entity type of the updated record.

·        id: String. GUID of the updated record.

errorCallback Function No A function to call when the operation fails. An object with the following properties will be passed:

·        errorCode: Number. The error code.

·        message: String. An error message describing the issue.

Return Value:
On success, returns a promise object containing the attributes specified earlier in the description of the successCallback parameter.

Example:
Create a new Javascript Webresource (new_Account.js) and copy paste the below code in it and call it on OnLoad of the Account Form.

After that, open an existing Account record.

function UpdateAccountRecord() {
 // define the data to update a record
 var data =
 {
 "name": "Arun Potti Inc", // Single Line of Text
 "creditonhold": true, // Two Option Set
 "description": "This is the description of the sample account after update", // Multiple Lines of Text
 "revenue": 20000000, // Currency
 "industrycode": 2, // 2 - Agriculture and Non-petrol Natural Resource Extraction // OptionSet
 "primarycontactid@odata.bind": "/contacts(39582A13-E6E7-E711-A95E-000D3AF27CC8)" // ContactId - Arun Potti // Lookup
 }
 // Update the Account Record
 Xrm.WebApi.updateRecord("account", "9CCDEB5F-E9E7-E711-A95E-000D3AF27CC8", data).then(
 function success(result) {
 // Perform operations on record update
 Xrm.Utility.alertDialog("Account updated successfully", null);
 },
 function (error) {
 // Handle error conditions
 Xrm.Utility.alertDialog(error.message, null);
 }
 );
}

Output:
Dynamics 365 Update Record V9.X

Hope you have successfully updated a record using New Syntax.