We were able to resolve our issue by creating a separate entity (MySite). The entity includes a lookup to the sites and the user. The user can change the site each time they are at a different location and the referral will be assigned to the team based on the site that it gets referred by. Below is the script we used to automatically update the referral based on the MySite location of the current user:
function getAssignedSite() {
//Make sure the record is new
let FORM_TYPE_CREATE = 1;
let formType = Xrm.Page.ui.getFormType();
if (formType == FORM_TYPE_CREATE) {
try {
//Use CRM's Advanced Find to build the FetchXML query
let mysiteFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
'<entity name="org_mysite">' +
'<attribute name="org_mysiteid" />' +
'<attribute name="org_name" />' +
'<attribute name="org_userassignedtositeid"/>' +
'<attribute name="org_siteid"/>' +
'<order attribute="org_name" descending="false" />' +
'<filter type="and">' +
' <condition attribute="org_userassignedtositeid" operator="eq-userid" />' +
'</filter>' +
'</entity>' +
'</fetch>'].join('');
//Encode the FetchXML query.
let encodedFetchXML = encodeURIComponent(mysiteFetchXML);
//get the Service Root URL (getClientUrl); Web API found in the Developer Resources; LogicalCollectionName found in the XRMToolBox Metadata Browser (org_mysites), query statement for FetchXML (?fetchXml=)
let ODataPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/org_mysites?fetchXml=";
//Build the request to retrieve data
let req = new XMLHttpRequest();
req.open("GET", ODataPath + encodedFetchXML, 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.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
let results = JSON.parse(this.response);
//If there are no my site records for the user, use the site from the user profile
//mysiteidValue = mysiteDetails['_org_siteid_value'] -- had to lookup this value (['_org_siteid_value'] ) from debugging the script in Chrome and walking through the code
if (results.value.length == 0) { getUsersite(); }
else {
for (let i = 0; i < results.value.length; i++) {
let mysiteDetails = results.value[i];
let mysiteidValue = mysiteDetails['_org_siteid_value'];
let mysiteidName = mysiteDetails['_org_siteid_value@OData.Community.Display.V1.FormattedValue'];
let mysiteEntityType = "org_site";
//Set the value of the lookup (id: guid, name:name of the entity being looked up, entityType: type of record () of the entity being looked up)
Xrm.Page.getAttribute("org_referredbysiteid").setValue([{ id: mysiteidValue, name: mysiteidName, entityType: mysiteEntityType }]);
}
}
}
else {
alert(this.statusText);
}
}
};
req.send();
}
catch (err) {
alert("Please contact the CRM System Administrator.")
}
}
}
//Retrieve the user's site on the user profile
function getUsersite() {
let usersiteFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
'<entity name="systemuser">' +
'<attribute name="systemuserid"/>' +
'<attribute name="org_siteid"/>' +
'<order descending="false" attribute="org_siteid"/>' +
'<filter type="and">' +
' <condition attribute="systemuserid" operator="eq-userid" />' +
'</filter>' +
'</entity>' +
'</fetch>'].join('');
let encodedFetchXML = encodeURIComponent(usersiteFetchXML);
//get the Service Root URL (getClientUrl); Web API found in the Developer Resources; LogicalCollectionName found in the XRMToolBox Metadata Browser (systemusers), query statement for FetchXML (?fetchXml=)
let ODataPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/systemusers?fetchXml=";
//Build the request to retrieve data
let req = new XMLHttpRequest();
req.open("GET", ODataPath + encodedFetchXML, 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");
//this header is needed for FetchXML
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
let results = JSON.parse(this.response);
if (results.length == 0) { alert("no record") }
else {
for (let i = 0; i < results.value.length; i++) {
let usersiteDetails = results.value[i];
let usersiteidValue = usersiteDetails['_org_siteid_value'];
let usersiteidName = usersiteDetails['_org_siteid_value@OData.Community.Display.V1.FormattedValue'];
let usersiteEntityType = "org_site";
//Set the value of the lookup (id: guid, name:name of the entity being lookuped, entityType: type of record () of the entity being looked up)
Xrm.Page.getAttribute("org_referredbysiteid").setValue([{ id: usersiteidValue, name: usersiteidName, entityType: usersiteEntityType }]);
}
}
}
else {
alert(this.statusText);
}
}
};
req.send();
}
Hope this can help someone else,
Chrys