Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get contact information by using ParentCustomerId from Opportunity entity

(0) ShareShare
ReportReport
Posted on by 1,771

Hi,

I want to get department name from ContactSet from opportunity form by filtering ParentCustomerId. But I am getting retrieveResult.status = 400.  Please find below script and help me to find solution.

Calling from Opportunity form

var accountID= Xrm.Page.getAttribute("customerid").getValue()[0].id;  (Potential customer from Opportunity)
var context = Xrm.Page.context;
            serverUrl = context.getServerUrl(); 
            var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
            var retrieveResult = new XMLHttpRequest();
            retrieveResult.open("GET", ODataPath + "/ContactSet?$select=new_Department&$filter=ParentCustomerId eq guid'" + accountID + "'", false);
            retrieveResult.setRequestHeader("Accept", "application/json");
            retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveResult.send();
            if (retrieveResult.readyState == 4 )
             alert("e");
               {
                if (retrieveResult.status == 200)
                   {
                    var retrieved = JSON.parse(retrieveResult.responseText).d;
                    alert(retrieved.results[0].new_Department)
    
                
                }
            }

*This post is locked for comments

  • Moshe Hayun Profile Picture
    Moshe Hayun 365 on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    Hello,

    To retrieve the name of the department, you must write a second request of type RetrieveAttributeRequest.
    The SDK.Metadata library contains a function that executes this RetrieveAttribute.

    More info here: https://nishantrana.me/2014/02/05/retrieve-optionset-label-using-sdk-metadata-rertrieveattribute-method-in-javascript-crm-2011/

    Just pay attention to one thing, the function presented in SDK.Metadata runs asynchronously. To make it sunchronous, you just need to change a boolean parameter to false

      req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", true); //Asynchronous

    to

      req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", false); //Synchronous

    You can do this by creating a second identical function called RetrieveAttributeSync (that's what I usually do) or by modifying the orginal function on the SDK.Metadata library.

    You can found the SDK.Metadata library in your SDK folder \SampleCode\JS\SOAPForJScript\SOAPForJScript\Scripts

    I'll have a look on your second problem

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    Department is optionset in you case. Odata returns only optionset Value. You will have to Get The Odata Value and Retrieve the Optionset attribute metadata and get the text for the value.

    guruprasadcrm.blogspot.in/.../retrieving-optionset-lable-data-using.html

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    Hi,

    Thanks for your help. it is working

    Let me know how do I get the department name.

    if I give  alert(retrieved.results[0].new_Department.Value);  it give correct value for example 100001  like but how I get the name of the department directly.  I tried as below but it is not working. So what is the right command.

     alert(retrieved.results[0].new_Department.Name);

    alert(retrieved.results[0].new_Department.Text);

    Another issue which was not resolved yet is getEventArgs().preventDefault  

    pls go through my query

    getEventArgs().preventDefault not working

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    First of all parentcustomerid has both account and contact records. So before filtering you need to check if you are filtering using the contact or account.

    Second Your filter should be contactid on contactset not parentcustomerid.

    var entityType =  Xrm.Page.getAttribute("customerid").getValue()[0].entityType;

    if(entityType == "contact")

    {

    var contactID= Xrm.Page.getAttribute("customerid").getValue()[0].id;  (Potential customer from Opportunity)

    var context = Xrm.Page.context;

               serverUrl = context.getServerUrl();  

               var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

               var retrieveResult = new XMLHttpRequest();

               retrieveResult.open("GET", ODataPath + "/ContactSet?$select=new_Department&$filter=ContactId eq guid'" + contactID + "'", false);

               retrieveResult.setRequestHeader("Accept", "application/json");

               retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               retrieveResult.send();

               if (retrieveResult.readyState == 4 )

                alert("e");

                  {

                   if (retrieveResult.status == 200)

                      {

                       var retrieved = JSON.parse(retrieveResult.responseText).d;

                       alert(retrieved.results[0].new_Department)

                   }

               }

    }

  • Moshe Hayun Profile Picture
    Moshe Hayun 365 on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    Parenthesis are not needed, it's used for grouped conditions. But it works also. :)

    [View:https://msdn.microsoft.com/en-us/library/gg309461(v=crm.7).aspx?f=255&mspperror=-2147217396#Anchor_1:750:50]

    What really made it works is the ParentCustomerId/Id

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    instead:

    retrieveResult.open("GET", ODataPath + "/ContactSet?$select=new_Department&$filter=ParentCustomerId eq guid'" + accountID + "'", false);

    try this:

    retrieveResult.open("GET", ODataPath + "/ContactSet?$select=new_Department&$filter=ParentCustomerId/Id eq (guid'" + accountID + "')", false);

  • Verified answer
    Moshe Hayun Profile Picture
    Moshe Hayun 365 on at
    RE: How to get contact information by using ParentCustomerId from Opportunity entity

    Hello,

    ParentCustomerId is an EntityReference not a GUID. So, you should write it like that 

    retrieveResult.open("GET", ODataPath + "/ContactSet?$select=new_Department&$filter=ParentCustomerId/Id eq guid'" + accountID + "'", false);


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,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans