Retrieve product number based on product name entered in product lookup field
// JScript source codeproductnumber = function () {
if (Xrm.Page.getAttribute("productid").getValue() != null) {
prdid = Xrm.Page.getAttribute("productid").getValue()[0].id;
this.retrieveRecord(prdid, "Product", null, null, function (res) { this.successCallback(res) }, function () { alert(‘Error’); });
}
}function successCallback(results) {
debugger;
var prdnumber = results.ProductNumber;
var quan=results.QuantityOnHand;
Xrm.Page.getAttribute("productnumber").setValue(prdnumber);
Xrm.Page.getAttribute("availablequantity").setValue(quan);
}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();
}_dateReviver: function (key, value) {
///<summary>
/// Private function to convert matching string values to Date objects.
///</summary>
///<param name="key" type="String">
/// The key used to identify the object property
///</param>
///<param name="value" type="String">
/// The string value representing a date
///</param>var a;
if (typeof value === ‘string’) {
a = /Date\(([-+]?\d+)\)/.exec(value);
if (a) {
return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
}
}
return value;
}function errorHandler(error) {
writeMessage(error.message);
}
No comments:
Post a Comment