Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

JavaScript to open up a lookup fields record in the same window if it exists.

(0) ShareShare
ReportReport
Posted on by

Does anyone have an example JavaScript script, that on being called (will be doing this via a ribbon button) it can check if a lookup field on the form in not null, and then open the lookup record itself in the current screen? (to act like a divert).

  • Verified answer
    RE: JavaScript to open up a lookup fields record in the same window if it exists.

    Here is the function, you just have to replace lookup field name and the entity name.

    function OpenLookupWindow() {
    
       if (Xrm.Page.getControl("lookupfieldName") != null && Xrm.Page.getControl("lookupfieldName").getAttribute().getValue() != null) {
           contactLookup = window.parent.Xrm.Page.getControl("lookupfieldName").getAttribute().getValue();
           lookupId = contactLookup[0].id;
           lookupName = contactLookup[0].name;
           var parameters = {};
          // parameters["attributename"] = "test"; //You can pass additional parameters if you want.
           var windowOptions = {
               openInNewWindow: false  // You can control the window
           };  
           window.parent.Xrm.Utility.openEntityForm("EntityName", lookupId, parameters, windowOptions);   // Replace entity name here . If its not work try to remove "window.parent"  
       }
       else
       {
           alert("control not found");
       }
    }

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans