web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to filter the results of an oData array

(0) ShareShare
ReportReport
Posted on by

Hi All,

On the quote entity, I have an oData query that returns the records of a custom entity (new_alerts).

"/XRMServices/2011/OrganizationData.svc/new_alertsSet?$select=new_alertsId,new_Alert,new_Message,new_ClientId,new_Category"

I would like to take the GUID from the Potential Customer field (customerid) and select the records that also have that customers GUID held in (new_ClientId). This gives me the GUID of customerid in entityId:

function guidLookup() {
        var entityName, entityId, entityLabel, lookupFieldObject;

        lookupFieldObject = Xrm.Page.data.entity.attributes.get("customerid");
        if (lookupFieldObject.getValue() != null) {
           entityId = lookupFieldObject.getValue()[0].id;
           entityName = lookupFieldObject.getValue()[0].entityType;
           entityLabel = lookupFieldObject.getValue()[0].name;
        }
}

But how would I get the following (results[0].new_ClientId.Id) out of my oData query to use in a comparison?  Bearing in mind I might have many records, i.e. [1], [2], [3], etc. and I would like to search in all of them for the GUID in entityId.

7024.Capture2.PNG

Thanks

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: How to filter the results of an oData array

    I've got myself this far....

        // Process the results
        var alertArray = JSON.parse(req.responseText).d.results;
        if (alertArray != null && alertArray.length > 0) {
            for (i = 0; i < alertArray.length; i++) {
                var showAlert += alertArray[i].new_ClientId.Name + "   -   " + alertArray[i].new_Alert + "<br>";
            }
       }


    However, now when the script fires, it creates this error:

    3414.Capture3.PNG

    ReferenceError: 'function' is undefined at eval code (eval code:1:1)

    ReferenceError: 'quote_Populate_Alerts' is undefined
       at eval code (eval code:1:1)
       at RunHandlerInternal (espprojects.crm4.dynamics.com/.../ClientApiWrapper.aspx)
       at RunHandlers (espprojects.crm4.dynamics.com/.../ClientApiWrapper.aspx)
       at ExecuteHandler (espprojects.crm4.dynamics.com/.../ClientApiWrapper.aspx)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.$Aw_1 (espprojects.crm4.dynamics.com/.../formcontrols.js)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandler (espprojects.crm4.dynamics.com/.../formcontrols.js)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandlerByDescriptor (espprojects.crm4.dynamics.com/.../formcontrols.js)
       at Anonymous function (espprojects.crm4.dynamics.com/.../formcontrols.js)
       at Anonymous function (espprojects.crm4.dynamics.com/.../global.ashx)
       at Mscrm.TurboForm.Control.Data.DataEntity.prototype.$Bk_1 (espprojects.crm4.dynamics.com/.../formcontrols.js)


  • Verified answer
    Anne Pessoa Profile Picture
    14 on at
    RE: How to filter the results of an oData array

    Hi Ederen,

    Not sure if I understood what you want to do.  

    You want to search all alerts for the customer?  If so you can use the filter for that:

    msdn.microsoft.com/.../gg309461(v=crm.7).aspx

    Would be something like "/XRMServices/2011/OrganizationData.svc/new_alertsSet?$select=new_alertsId,new_Alert,new_Message,new_ClientId,new_Category$filter=new_ClientId/Id eq (guid'" + customerid+ "')"

  • Community Member Profile Picture
    on at
    RE: How to filter the results of an oData array

    Thanks for the answer.  The filter might just move me a step closer to what I need.

    I want to retrieve all the alerts for the customer and show them in a text field with the Name of the account, maybe the date created and the actual alert.  

    That ReferenceError is a bit odd.  In my troubleshooting, I have found that the webpage is not loading the script during the event and so the event is missing the reference, hence 'undefined'.  I have no idea why this is happening.

  • Suggested answer
    Aman Kothari Profile Picture
    on at
    RE: How to filter the results of an oData array

    Hi,

    You can use normal JS code or third parties library like underscore , lodash etc for iterating JS array .

    you can iterate using

    1. forEach

     alertArray.forEach(function (obj, index) {
      //check your condition here
    if(obj.new_ClientId.Id==entityId){
    //write your logic here
      }
    });

    2.Underscore.js or Lodash.js

    _.each(
    alertArray,function(obj){
    //check your condition here
    if(obj.new_ClientId.Id==entityId){
    //write your logic here
      }
    })

    Hope it'll help you.

    Thanks

    Aman Kothari

    Blog  LinkedIn 

  • Community Member Profile Picture
    on at
    RE: How to filter the results of an oData array

    Hi Anne,

    If I use "$filter=new_ClientId/Id eq (Guid'" + customerId + "')" like you said, each time the code runs, it gives the error

    TypeError: Unable to get property 'results' of undefined or null reference

      at quote_Populate_Alerts

    This is directly related to the $filter= string above.

    The error suggests it cannot find any results, but I can confirm that customerId contains a guid string without curly braces and that there are at least 2 records in the search with that GUID

  • Community Member Profile Picture
    on at
    RE: How to filter the results of an oData array

    Anne, you have cause me hours of pain lool!!  Well not you directly, it's more my noobness!

    You missed the & before the $filter. I should've tested the url first but instead I just bashed my head for hours until I had the glorious idea of trying the url and hey-presto....a decent error message about '&' being expected.

    A great (and much needed) learning experience.  Thank you

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans