Asked by:
Getting the GUID of a Record Retrieved By Jscript/OData

Question
-
I've created a function which returns the record in an entity where a field is equal to a certian value.
How can i now get the guid of this record.
function getCurrentLabourRate() { var serverUrl = Xrm.Page.context.getServerUrl(); var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$filter=c2_Status/Value eq 745670000" jQuery.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataUrl, async: false, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { if (data && data.d != null && data.d.results != null) { var labourRateCurrentRecord = data.d; } }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert("Error : has occured during retrieval of the records "); } }); }
I believe below only selects the record of the entity c2_labourrate where the status of an option set is equal to the value 745670000 the name in the optionset of this value is current.
/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$filter=c2_Status/Value eq 745670000"
- Edited by KKHAN1991 Tuesday, August 6, 2013 1:44 PM
Tuesday, August 6, 2013 1:40 PM
All replies
-
Hi,
When you don't specify any field in the Odata query it by default selects all the attributes. So you can get the Guid using the below statement.
The above query will return multiple results. so you can loop through and get the Id of the retrieved entity.
if (labourRateCurrentRecord!= null && labourRateCurrentRecord [0].results.length > 0) {
for (var count = 0; count < labourRateCurrentRecord[0].results.length; count++) {
var id=labourRateCurrentRecord[0].results[count].c2_labourrateId; // You have to specify the schema name of attribute not a Logical name.
}
If you want to select the specific attribute then we can specify the attribute as given below.
var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$select= c2_labourrateId&$filter=c2_Status/Value eq 745670000"
Please let me know if you need any further help
Thanks!
Kalim Khan
Tuesday, August 6, 2013 5:56 PM -
So i think i get what you are trying to say,
The theory is my query should only return 1 result anyway. The labour rate where the optionset is set to current.
Once i have found the guid of the current labourrate record. I want to then set a lookup located on another form with this record. So this script will be ran on the form with the lookup.
What i need is the id and name of the record returned to populate the lookup. The id i believe being the GUID, and then the name.
How can i populate this with what u have said. Here's what i have tried let me know if it looks alright.
function getCurrentLabourRate() { var serverUrl = Xrm.Page.context.getServerUrl(); var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$filter=c2_Status/Value eq 745670000" jQuery.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataUrl, async: false, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { if (data && data.d != null && data.d.results != null) { var labourRateCurrentRecord = data.d; if (labourRateCurrentRecord!= null && labourRateCurrentRecord [0].results.length > 0) { for (var count = 0; count < labourRateCurrentRecord[0].results.length; count++) { var id= labourRateCurrentRecord[0].results[count].c2_labourrateId; var name= abourRateCurrentRecord[0].results[count].c2_labourrateName; } var lookupItem = new Array(); lookupItem[0] = new Object(); lookupItem[0].id = id; lookupItem[0].name = name; lookupItem[0].entityType = "c2_labourrate"; Xrm.Page.getAttribute("c2_labourrate").setValue(lookupItem); } }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert("Error : has occured during retrieval of the records "); } }); }
OR can i do it with this like you mentioned
var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$select= c2_labourrateId, c2_labourrateName&$filter=c2_Status/Value eq 745670000"
If so how would i populate the lookup with this method if this is the right way to populate a lookup, with the record returned.
- Edited by KKHAN1991 Tuesday, August 6, 2013 6:34 PM
Tuesday, August 6, 2013 6:25 PM -
Hi,
The Query may return multiple result. there may be multiple Labourrate records in the system for the status 745670000.
When you use the below Query, it returns all the attributes by default. It is like sql query Select * from c2_labourrate where c2_Status=745670000.
var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$filter=c2_Status/Value eq 745670000"
When you use the below query, You specify the selected attributes that need be retrieved. It is like sql query (Select c2_labourrateId,c2_labourrateName from c2_labourrate where c2_Status=745670000)
var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$select= c2_labourrateId, c2_labourrateName&$filter=c2_Status/Value eq 745670000"
After retrieving the record the Lookup population will be the same for both the case.
if (data && data.d != null && data.d.results != null) {
var labourRateCurrentRecord = data.d;
var id;var name;
if (labourRateCurrentRecord!= null && labourRateCurrentRecord [0].results.length > 0) {
for (var count = 0; count < labourRateCurrentRecord[0].results.length; count++) {
id= labourRateCurrentRecord[0].results[count].c2_labourrateId;
name= abourRateCurrentRecord[0].results[count].c2_labourrateName;
}
var lookupItem = new Array();
lookupItem[0] = new Object();
lookupItem[0].id = id;
lookupItem[0].name = name;
lookupItem[0].entityType = "c2_labourrate";
Xrm.Page.getAttribute("c2_labourrate").setValue(lookupItem);
}
Please make sure that you specify the Proper schema name for the entity and attributes. You can refer the below forum replied by me to get the understanding on Schema name and logical name.
Thanks!
Wednesday, August 7, 2013 5:52 AM -
Put alert in your code. Do Debugging using F12 to find out where your script is breaking.
try
id= labourRateCurrentRecord[0].results[count].c2_labourrate.Id;
alert(id):Regards Faisal
Wednesday, August 7, 2013 11:13 AM -
Hi,
Please refer this forum reply. They have done the same way.
I think there is an error in your code. Your condition will return multiple result. You can take the first result found. just take the 0'th element of collection found.
alert(labourRateCurrentRecord[0].results[count].c2_labourrateId);
Hope this helps!
Thanks!
Wednesday, August 7, 2013 11:39 AM -
<Message>Unable to get property 'results' of undefined or null reference</Message>
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ __ _ _
So i am unsure if i am explaining my requirement properly.
1. I am creating a system where the customer needs to dynamically change the rate of his labour charge. So i have created an entity called "Labour Rate" with 2 fields, the rate(10.00) and the status(Current or OutDated).
2. The Second entity is a Form they will fill out for the "jobs" they have carried out. On this entity record there will be a lookup to the "Labour Rate" record. This lookup should automatically populate upon creation with the record in the "labour rate" entity where the status is set to current. On this form is where the script will be ran.
3. If the customer wants to update the labour rate charge, they will create a new record , set the new amount and then set the status to current. Upon changing the status it should automatically change prvious records status to outdated. Doing it this way allows previous job forms to maintain there labour charge at the time they were created. But newer Job Forms will be created and automatically populate with the current labour rate.
So in the end the query should only be returning one result, because at any given time in the Labour Rate entity there will be only one record where the status is set to current.
- Edited by KKHAN1991 Wednesday, August 7, 2013 12:19 PM
Wednesday, August 7, 2013 11:59 AM -
Hi,
Are you able to debug your code?
Just go through with this. you have to replace your query and Field, entity name only.
http://inogic.blogspot.in/2011/12/read-records-synchronously-using-json_13.html
Hope this helps!
Wednesday, August 7, 2013 12:12 PM -
Kalim, the link you sent. Is not same as what i am doing. Could you read my requirement i posted above and see. It is not as simple as replacing the fields, firstly he is getting a field from the form he is running the script on. Which i am not doing.
Could you provide a link how i can set this up to debug? As i am using Notepad++ this is the first time im doing something of this scale.- Edited by KKHAN1991 Wednesday, August 7, 2013 12:30 PM
Wednesday, August 7, 2013 12:16 PM -
Ty Kalim, i got it worked with your help and the link you sent me, had to change some details including the initial query.
Working Code.
function setCurrentLabourRate() { if(Xrm.Page.ui.getFormType() == 1) { getCurrentLabourRate(); } } function getCurrentLabourRate() { var serverUrl = Xrm.Page.context.getServerUrl(); var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$select=c2_labourrateId,c2_name&$filter=c2_Status/Value eq 745670000" jQuery.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataUrl, async: false, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { ProcessReturnedEntities(data.d.results); }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); } }); } //Get Entities function ProcessReturnedEntities(ManyEntities) { for (i = 0; i < ManyEntities.length; i++) { var oneEntity = ManyEntities[i]; var recordIdAttribute = oneEntity.c2_labourrateId var recordIdValue = oneEntity.c2_name; SetLookupValue("c2_labourrate", recordIdAttribute, recordIdValue, "c2_labourrate"); } } // Set the value of a lookup field function SetLookupValue(fieldName, id, name, entityType) { if (fieldName != null) { var lookupValue = new Array(); lookupValue[0] = new Object(); lookupValue[0].id = id; lookupValue[0].name = name; lookupValue[0].entityType = entityType; Xrm.Page.getAttribute(fieldName).setValue(lookupValue); } }
Wednesday, August 7, 2013 1:10 PM -
The link what i provided is just for your reference. So that you can modify javascript as per your requirement.
I have updated the javascript below. I did not tried but it should work.
function getCurrentLabourRate() {
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataUrl = serverUrl + "/xrmservices/2011/OrganizationData.svc/c2_labourrateSet?$select= c2_labourrateId, c2_labourrateName& $filter=c2_Status/Value eq 745670000"
jQuery.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataUrl,
async: false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (data && data.d != null && data.d.results != null && data.d.results.length>0) {
var labourRateCurrentRecord = data.d.results[0];
if (labourRateCurrentRecord!= null ) {
var id= labourRateCurrentRecord.c2_labourrateId;
var name= abourRateCurrentRecord.c2_labourrateName;
var lookupItem = new Array();
lookupItem[0] = new Object();
lookupItem[0].id = id;
lookupItem[0].name = name;
lookupItem[0].entityType = "c2_labourrate";
Xrm.Page.getAttribute("c2_labourrate").setValue(lookupItem);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Error : has occured during retrieval of the records ");
}
});
}
You can refer below link to debug javascript,
Let me know if it works for you.
Wednesday, August 7, 2013 1:11 PM