Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

I need to copy the Product ID field to the  Opportunity Product when populating the Existing Product field on an Opportunity Product.

*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

    Hi Graeme,

    Here is sample API with complete function-

    function GetProductId()
    {
        var productiID ="DCCE5294-9447-E711-8104-5065F38A4A21"; //GUID Of the product 
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/products(" + productiID + ")?$select=name,_pricelevelid_value,productid", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.response);
                    var name = result["name"];                  
                    var _pricelevelid_value = result["_pricelevelid_value"];
                    var _pricelevelid_value_formatted = result["_pricelevelid_value@OData.Community.Display.V1.FormattedValue"];
                    var _pricelevelid_value_lookuplogicalname = result["_pricelevelid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                    var productid = result["productid"];
    
                    // Set the value 
    
    
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

    Thanks, however how do i put this in a function?

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

    Hello Graeme,

    Try with this -

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/products(DCCE5294-9447-E711-8104-5065F38A4A21)?$select=name,_pricelevelid_value,productid", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var name = result["name"];
                var _pricelevelid_value = result["_pricelevelid_value"];
                var _pricelevelid_value_formatted = result["_pricelevelid_value@OData.Community.Display.V1.FormattedValue"];
                var _pricelevelid_value_lookuplogicalname = result["_pricelevelid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var productid = result["productid"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();


  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Does anyone know of a simple JavaScript to copy a text field from a related entity to the current page?

    Hello Graeme ,

    Download the rest builder which will be easy to create the rest API to get the product id. Also you can find sample code from here  -

    community.dynamics.com/.../243887

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans