This is my form. When I create a New communication and click on the tab Risk Assessment which has a iframe, redirects back the communication I created, its not only for the communication but also for Notes and Documents as well. When I create a Notes and click Risk Assessment it redirects back to the notes I created. Below is my Web resource code, When the debugger is on its working properly
function redirectURL(executionContext,iFrameName){
debugger;
var IFrame = Xrm.Page.ui.controls.get(iFrameName);
var Url = IFrame.getSrc();
var userId = RemoveBrackets(parent.Xrm.Utility.getGlobalContext().userSettings.userId);
var req = new XMLHttpRequest();
req.open(/GET/, parent.Xrm.Page.context.getClientUrl() + /api/data/v9.1/contacts?$select=contactid,_uph_uphuser_value&$filter=_uph_uphuser_value eq / + userId + //, false);
req.setRequestHeader(/OData-MaxVersion/, /4.0/);
req.setRequestHeader(/OData-Version/, /4.0/);
req.setRequestHeader(/Accept/, /application/json/);
req.setRequestHeader(/Content-Type/, /application/json; charset=utf-8/);
req.setRequestHeader(/Prefer/, /odata.include-annotations=///*////);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var contactid = results.value[i][/contactid/];
var _uph_uphuser_value = results.value[i][/_uph_uphuser_value/];
var _uph_uphuser_value_formatted = results.value[i][/_uph_uphuser_value@OData.Community.Display.V1.FormattedValue/];
var _uph_uphuser_value_lookuplogicalname = results.value[i][/_uph_uphuser_value@Microsoft.Dynamics.CRM.lookuplogicalname/];
var parameter1 = generateToken();
CreateRecord(contactid, parameter1);
if (parent.Xrm.Page.context.getQueryStringParameters().TreeData !== undefined) {
if (Url.indexOf(/?/) !== -1)
{
Url = Url.substr(0, Url.indexOf(/?/));
}
var param1 = Url+ /?Data=/+parent.Xrm.Page.context.getQueryStringParameters().TreeData+ /&Token=/ + parameter1;
IFrame.setSrc(param1);
}
else{
IFrame.setSrc(Url + /&Token=/ + parameter1);
}
}
} else {
parent.Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
function generateToken() {
var tokenLength = 20;
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var token = '';
for (var i = 0; i < tokenLength; i++) {
var randomIndex = Math.floor(Math.random() * characters.length);
token += characters.charAt(randomIndex);
}
return token;
}
function CreateRecord(contactid, parameter1) {
debugger;
var entity = {};
entity[/uph_ContactId@odata.bind/] = //contacts(/ + contactid + /)/;
entity.uph_name = parameter1;
var req = new XMLHttpRequest();
req.open(/POST/, parent.Xrm.Page.context.getClientUrl() + /api/data/v9.1/uph_uphtokenauthorizes/, true);
req.setRequestHeader(/OData-MaxVersion/, /4.0/);
req.setRequestHeader(/OData-Version/, /4.0/);
req.setRequestHeader(/Accept/, /application/json/);
req.setRequestHeader(/Content-Type/, /application/json; charset=utf-8/);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader(/OData-EntityId/);
var regExp = ///(([^)]+)//)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
} else {
parent.Xrm.Utility.alertDialog(this.sstatusText);
}
}
};
req.send(JSON.stringify(entity));
}
function RemoveBrackets(guid) {
if (IsValidValue(guid))
return guid.replace(/[{}]/g, '');
return null;
}
function IsValidValue(value) {
if (value === undefined || value === null || value === // || value === /undefined/ || value === /null/)
return false;
return true;
}
function redirectPageToSearch()
{
debugger;
console.log(/Before getting extraqsValue/ + parent.Xrm.Page.context.getQueryStringParameters());
console.log(/extraqsValue: / + parent.Xrm.Page.context.getQueryStringParameters().TreeData);
if (parent.Xrm.Page.context.getQueryStringParameters().TreeData !== undefined) {
// // Get the value of the 'extraqs' parameter
var extraqsValue = parent.Xrm.Page.context.getQueryStringParameters().TreeData;
Xrm.Page.ui.tabs.get(/Search Resources/).setFocus();
Xrm.Page.ui.tabs.get(/Search Resources/).setDisplayState(/expanded/);
// // Log the value to the browser console
console.log(/extraqs value: / + extraqsValue);
} else {
console.log(/extraqs parameter not found in the URL./);
}
}