Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

get Value of look up fields from another record

Posted on by 280

I have wrote a Jscript to get value of fields from another opportunity record. The values shown only for text fields but can anyone suggest me how to get values from lookup and optionset fields and insert them into similar fields. 

var lookup = new Array();
        lookup[0] = new Object();
        lookup[0].id = opportunity.OpportunityId;
        lookup[0].name = opportunity.Name;
        lookup[0].entityType = "opportunity";
        Xrm.Page.getAttribute("new_masteropportunity").setValue(lookup);                                       

        Xrm.Page.getAttribute("new_topic").setValue(opportunity.Name);                                    
        Xrm.Page.getAttribute("new_street1").setValue(opportunity.new_street1);                                 
        Xrm.Page.getAttribute("new_city").setValue(opportunity.new_city);      


*This post is locked for comments

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: get Value of look up fields from another record

    Hi Raviteja Reddy,

    Try the code below.

     Xrm.Page.getAttribute("optionSetFieldName").setValue(opportunity.optionSetFieldName.Value); //To set option-set field value
    
     var lookupValue = new Array();
     lookupValue[0] = new Object();
     lookupValue[0].id = opportunity.lookupFieldName.id;
     lookupValue[0].name = opportunity.lookupFieldName.name;
     lookupValue[0].entityType = opportunity.lookupFieldName.entityType;
     if (lookupValue[0].id != null)
      Xrm.Page.getAttribute("lookupFieldName").setValue(lookupValue); //To set lookup value

    Hope this helps.

  • Suggested answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: get Value of look up fields from another record

    I would suggest using the CRM REST Builder Tool.

    github.com/.../CRMRESTBuilder

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: get Value of look up fields from another record

    If you want to do this you need to retrieve the Opportunity record from the oData feed.

    https://msdn.microsoft.com/en-us/library/gg334767(v=crm.6).aspx 

    Here is how todo is with javascript:

    https://msdn.microsoft.com/en-us/library/gg328025(v=crm.6).aspx 

    you can try this out with

    [URL of your CRM]\api/data/v8.0/opportunities([KNOWN IF OF OPP])

    Example:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/opportunities(ID GOES HERE)", 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 opportunityid = result["opportunityid"];
    console.log(result);
    //You get set values here,
    } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send();

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans