Hi,
I’m working to write a javascript that has requirement as below:
1. On entity “SystemUser”, we created a lookup field ‘primary team name’ which is a N-to-1 relation with the “Team” entity
2. in Task, we want to retrieve the ‘primary team name’ of the owner of the Task. This will be retrieved into a new text field called "Team Name",
for example.
3. in Task, we want to abbreviate the team name with two letter words (for example AC for Accounting, etc). This abbreviated field is currently nowhere in CRM right now, but if it's easier then we're willing to create in under the "Team" or "SystemUser"
entity.
I'm pretty new with crm development. Do you have any suggestion on what method would be the best for this situation? (oData, REST, FetchXML, etc.)
I tried to follow a code posted by Surya on this link :http://inventcrm.wordpress.com/2012/06/06/crm-2011-retrieve-values-from-other-entity-based-on-lookup-value/comment-page-1/#comment-240
This is the code that I came with - but I'm not sure, when registering this javascript as a webresource in CRM, which function do we call in OnLoad event? So far this code is not working yet for me.
Thanks in advance for your help.
Cheers,
elisabeth
primaryteam = function () {
if (Xrm.Page.getAttribute(“ownerid”).getValue() != null) {
ownid = Xrm.Page.getAttribute(“ownerid”).getValue()[0].id;
this.retrieveRecord(ownid, “SystemUser”, null, null, function (res) { this.successCallback(res) }, function () { alert(‘Error’); });
}
}
function successCallback(results) {
debugger;
var primaryteam = results.nwp_primaryteam;
Xrm.Page.getAttribute(“nwp_primaryteamname”).setValue(primaryteam);
}
retrieveRecord = function (id, type, select, expand, successCallback, errorCallback) {
var systemQueryOptions = “”;
if (select != null || expand != null) {
systemQueryOptions = “?”;
if (select != null) {
var selectString = “$select=” + select;
if (expand != null) {
selectString = selectString + “,” + expand;
}
systemQueryOptions = systemQueryOptions + selectString;
}
if (expand != null) {
systemQueryOptions = systemQueryOptions + “&$expand=” + expand;
}
}
var req = new XMLHttpRequest();
var surl = Xrm.Page.context.getServerUrl();
var url = http://localhost:5555/testing/XRMServices/2011/OrganizationData.svc/ +
type + “Set(guid’” + id + “’)” + systemQueryOptions;
req.open(“GET”, url, true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8″);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
if (this.status == 200) {
successCallback(JSON.parse(this.responseText, this._dateReviver).d);
}
else {
errorCallback(this._errorHandler(this));
}
}
};
req.send();
}