This works for me.
function SetReleatedEmailSubgrid(executionContext) {
var isUnified = isUCI();
if (isUnified) {
var formContext = executionContext.getFormContext();
var objSubGrid = formContext.getControl("RelatedEmails");
var entityId = Xrm.Page.data.entity.getId();
if (objSubGrid == null) {
setTimeout(SetReleatedEmailSubgrid(executionContext), 2000);
return;
}
//cc_relatedemailsid is a custom field
var FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' returntotalrecordcount='true' page='1' count='4' no-lock='false' >" +
"<entity name='email' >" +
"<attribute name='statecode' />" +
"<attribute name='subject' />" +
"<attribute name='to' />" +
"<attribute name='from' />" +
"<attribute name='statuscode' />" +
"<attribute name='prioritycode' />" +
"<attribute name='createdon' />" +
"<order attribute='createdon' descending='true' />" +
"<filter type='and' >" +
"<condition attribute='cc_relatedemailsid' operator='eq' value='" + entityId + "' />" +
"</filter>" +
"<attribute name='activityid' />" +
"<link-entity name='email' from='activityid' to='cc_relatedemailsid' alias='bb' >" +
"<filter type='and' >" +
"<condition attribute='activityid' operator='eq' value='" + entityId + "' />" +
"</filter>" +
" </link-entity>" +
"</entity>" +
"</fetch>";
objSubGrid.setFilterXml(FetchXml);
objSubGrid.refresh();
}
}
function isUCI() {
return Xrm.Internal.isUci()
}