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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Trying to get the name/id/else of an account from a lookup to apply a filter to another lookup (on opportunities)

(0) ShareShare
ReportReport
Posted on by

Hello everyone! I made a custom entity and I'm trying to get the name or id or anything that could work of an account from a lookup, and to apply a filter with that account on another lookup on opportunities, the point is to find opportunities easily (since there are a lot) by selecting the account before.

Here is what I did (It is not working and I don't know why) : 

function OnChangeOfAccount(){
Xrm.Page.getControl("rta_opportunity").addPreSearch(function (){
addLookupFilter();
});

function addLookupFilter(){
if(XRM.Page.getAttribute("rta_account") != null){
var account = XRM.Page.getAttribute("rta_account").getValue()[0];
if (account != null){
fetchXml = "<filter type='and'><condition attribute='parrentaccountid' operator='eq' value='" + account + "' /></filter>";
Xrm.Page.getControl("rta_opportunity").addCustomFilter(fetchXml);
}
}

}

The error I get is : There was an error with this field customized event

Field : rta_account

Event : onchange

Error : undefined

Help me please!

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Does that field rta_account exist?
    Xrm is case-sensitive so don't spell it as XRM. (unless you're referencing a library which has it spelt like that)
    Also your fetchxml has attribute parrentaccountid with two Rs

  • Community Member Profile Picture
    on at

    The field does exist! I corrected the mistakes you found but I still have the exact same problem..

    What would you suggest?

    Is there a way to have more precise error messages?

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello,

    Try with this - 

    function OnChangeOfAccount() {
    Xrm.Page.getControl("rta_opportunity").addPreSearch(function () {
    addLookupFilter();
    });
    }  // Bracket is missing 

    function addLookupFilter() {
    if (XRM.Page.getAttribute("rta_account") != null) {
      var account = Xrm.Page.data.entity.attributes.get("rta_account").getValue()[0].id; // Xrm shhoud be smalll not like XRM
    if (account != null) {
    fetchXml = "<filter type='and'><condition attribute='parrentaccountid' operator='eq' value='" + account + "' /></filter>";
    Xrm.Page.getControl("rta_opportunity").addCustomFilter(fetchXml);
    }
    }
    }

  • Community Member Profile Picture
    on at

    What does the [0] refers to? And the .id?

    By the way thank you one more time it's working... I make too much dumb mistakes.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello Vincent ,

    Since you are retrieving lookup value , which contains primary field called   id and name . id contains GUID/primary key  of the record and name contains the record name of the entity.

    var account = Xrm.Page.data.entity.attributes.get("rta_account").getValue()[0]   // by doing this you will get the lookup object which contains id and name  both but in your  condition XML you need the GUID or id of the record not object. Thats what you need to get id .

    Hope this helps .

  • Community Member Profile Picture
    on at

    Nice!

    Ok, I thought that since I was comparing two lookups objects I needed to compare the whole object and not only the Id or name.

    Thank you very much, see you soon! :D

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans