Update OptionSet value of related entity on change of another OptionSet(Parent entity) using Web API JavaScript

How to update OptionSet value of related entity on change of another OptionSet value using Web API – JavaScript. Or How to update subgrid field values using Web API(client side java script).

I need parent entity optionset value on child entity when it change. As we are aware that we can copy field value to related entity using “field mapping” but the case where field value change frequently then “field mapping” will not work – for example – Status field. Suppose, I have an optionset for status field which change frequently and it’s normal.

There are two ways to achieve it – One is to write plugin which will change optionset value of child entity based on parent entity optionset value change and two – write JS using Web API to update related entity field.

function updateOptionSetValue() {
var status = Xrm.Page.getAttribute(“statusfieldschemaname”).getValue();
if (status != “”) {
var rowCount = Xrm.Page.getControl(“subgridname”).getGrid().getTotalRecordCount();
for (var i = 0; i < rowCount; i++) { //Read last record values
var allRows = Xrm.Page.getControl(“subgridname”).getGrid().getRows();
var allGridRowData = [];
allRows.forEach(function (row, i) {
allGridRowData.push(row.getData());
});

//Read values of all the child grid fields and update Status value
for (var i = 0; i < allGridRowData.length; i++) {
var Name = (allGridRowData[i].getEntity().getAttributes().getByName(“name”).getValue());
if (Name != null) {
var recordId = Xrm.Page.getControl(“subgridname”).getGrid().getRows().get(i).getData().getEntity().getId();
var ID = recordId.replace(/\{|\}/gi, “”);
var data = {
“value”: status
};

var req = new XMLHttpRequest();
req.open(“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v8.2/entityschemaname(” + ID + “)/relatedentityoptionsetschemaname”, true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Xrm.Utility.alertDialog(‘Record Updated Successfully.’);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}
}
}
}
}

Advertisements

How to configure Email using Server Side Synchronization in CRM 2016 Online?

How to configure Email using Server Side Synchronization in CRM 2016 Online?

Arun Potti's MS CRM blog

Follow the below steps to Configure Email in CRM 2016 Online,

Step 1:Click here and follow my previous article to configure Office 365 Enterprise E3 Trial Subscription and Assign Product License to the required user.

Step 2: Go to Microsoft Dynamics CRM -> Settings -> Email Configuration.

Click on Email Server Profiles.

Server Side Synchronization - Step 2

Microsoft Exchange Online” Email Server Profile record created automatically by Online CRM. We can use the same.

Server Side Synchronization - Step 2.1Step 3: Open the “Microsoft Exchange Online” Email Server Profile record.

Click on Mailboxes (1) and Search for required User Mail Box record (2) and Open it (3).

Server Side Synchronization - Step 3.0

Update Email Address field with your Office 365 Login Id and Save.

Server Side Synchronization - Step 3.1Step 4: Click on Approve Email ribbon button and Click on OK.

Server Side Synchronization - Step 4Step 5: Click on Test & Enable Mailbox ribbon button and Click on OK.

Server Side Synchronization - Step 5Step 6: Click on Alerts to…

View original post 151 more words

How to configure subscriptions in MS CRM 2016 Online?

How to configure “Office 365 Enterprise E3” Subscription in Online CRM 2016?

Arun Potti's MS CRM blog

Follow the below steps to configure Office 365 Enterprise E3 Subscription in Online CRM 2016.

For more details on Office 365 Enterprise E3, please Click here.

Step 1: Create CRM Online 2016 30 days’ trial version. Click here to create.

(Or)

Use the existing trial Version, if you have.

Step 2: Open https://portal.office.com/. Provide your credentials and Sign in.

99.Office 365 LoginStep 3: Under Office 365 Admin center preview Home Page,

  1. Click on Billing.
  2. Click on Subscriptions.
  3. By default, can see only one Subscription “Microsoft Dynamics CRM Online Trial” for 30 days.

Click on Add subscriptions, to find & subscribe Office 365 Enterprise E3 Trial Version.

99.Admin center preview - SubscriptionsStep 4: Under Enterprise Suite, can see “Office 365 Enterprise E3”.

  1. Hover on it, and click on Start free trial.

99.Enterprise Suite2. Click on Try now.

99.Confirm your order

3. Click on Continue.

99.order receipt

4. Within few minutes, the subscription will…

View original post 158 more words

Javascript – Prevent Save

Microsoft Dynamics 365 and Unified Service Desk

From time to time you might need to add some validation to the save event of an entity, this actually used to be an approach I would use on a regular basis but since the introduction of business rules have found myself doing this less and less. But still, knowing the ability is available is handy.

When you define the onsave event function, you must tick the “Pass execution contact as first parameter” option. (See below)

Having done that you can create an onSave function with code similar to the example I have shown below. Note forgetting the “(context)”, which will take the context parameter allowing you to prevent the save when needed.

function onSave(context) {
  var saveEvent = context.getEventArgs();
  if (Xrm.Page.getAttribute("telephone1").getValue() == null) {
    // *** Note: I am using an alert for testing a notification maybe better!
    alert("Put in a phone number!");
    saveEvent.preventDefault();
  }
}

Note: This simple…

View original post 30 more words

Add days to date using JavaScript in Dynamics CRM

This blog post is related to Datetime field in CRM. In many scenarios, we want to recalculate the date field value in Dyanamics CRM by adding days to it. The below JavaScript function is useful in that condition :

//Function to add days to date

function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + parseInt(days));
return result;
}

//Recalculate date field value

function recalculateDateFieldValue() {
var date= Xrm.Page.getAttribute(“datefieldname”).getValue();

if (firstemaildueDate != null) {
Xrm.Page.getAttribute(“datefieldname”).setValue(addDays(date, 42));
}
}

we can recalculate date field value by adding days as per our needs by checking conditions. Hope this helps.!

 

To calculate date using business working days

Microsoft Dynamics 365 and Unified Service Desk

Recently I had a small challenge to calculate a date “n” working days from today. This short post will show you the code I used.

In my example, a working day was defined as Monday to Friday.

I wasn’t worried about business closures or bank holidays. I simply needed a date to act as a target to complete work by.

The code snippet below was the function I finally used;

function BusinessDays(d, n) {
 // *** d = a date 
 // *** n = number of working days to increment by
  d = new Date(d.getTime());
  var day = d.getDay();
  d.setDate(d.getDate() + n + (day === 6 ? 2 : +!day) + (Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
 return d;
} // End function

View original post

Remove option of OptionSet based on form selection.

Suppose we have one OptionSet field with below values:

{1 – First }

{2 – Second }

{3 – Third }

We want to hide “Third” option from the OptionSet on form selection then the below code works fine for us :

function removeOptionSetOption() {
var formName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
if (formName == ‘Information’) {//Suppose we have form called “Information”
//Check options.
var pickList = Xrm.Page.getControl(“OptionSetName”);
var options = pickList.getOptions();

for (var i = 0; i < options.length; i++)
if (options[i].value == 3) // Third
{
pickList.removeOption(options[i].value);
return;
}
}
}