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)

set related entity on form event

(0) ShareShare
ReportReport
Posted on by

Hi what I would like to so is this:

On a new opportunity, when the user selects an account, I want to automatically select the primary contact for that account into the opportunity account field.

I have a form even working on the onChange event for Account field on the opportunity field. I am successfully getting the primaryContact entity data back via a json call. However, when I assign the contactID to the opportunity form field parentcontactid nothing happens. I have updated the code to enter the business telephone as the opportunity title to prove that it is working.

do I need to do something different to set an entity instead of a simple field? the below code is called onChange for the Account field parentaccountid). getContact is the method first invoked.

Also is there a simple way to detect if you are inserting? I only want to run the code on inserts.

thanks in advance

james

function getResident() {


   //Getting the Lookup object from the CRM Page
   var ProjectObject = Xrm.Page.getAttribute("parentaccountid").getValue();


   // Getting the GUID for the Project record
   var ProjectNoID= ProjectObject[0].id;

   //Stripping out the curly brackets
   ProjectNoID = ProjectNoID.replace('{', '').replace('}', '');

   //Checking if we have a project GUID Value
   if (ProjectNoID != null) {

      //Let’s create the Web Service URL
      oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";

      //Call the project retrieve function
//commented out for themo
      RetrieveProjectRecord(ProjectNoID, oDataPath);
   }
}


  

function RetrieveProjectRecord(Id, ODataPath) {

   var retrieveReq = new XMLHttpRequest();
   retrieveReq.open("GET", ODataPath + "/AccountSet(guid'" + Id + "')/account_primary_contact", true);  
   //retrieveReq.open("GET",  ODataPath + "/AccountSet(guid'" + Id + "')", true);
   retrieveReq.setRequestHeader("Accept", "application/json");
   retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

   retrieveReq.onreadystatechange = function () {
      retrieveProjectReqCallBack(this);
   };
   retrieveReq.send();
}
 
function retrieveProjectReqCallBack(retrieveProjectReq) {
   if (retrieveProjectReq.status == 200) {
      var retrievedProject = JSON.parse(retrieveProjectReq.responseText).d;

      var ProjectName = retrievedProject.Telephone1;

      var theID = retrievedProject.ContactId;

alert(ProjectName + ' id= ' + theID);
      Xrm.Page.getAttribute("name").setValue(ProjectName);
      Xrm.Page.getAttribute("parentcontactid").setValue('{'+theID+'}');

      Xrm.Page.getAttribute("description").setValue('hiya');

   } else {
      alert("Error in Fetching data");
   }
}


*This post is locked for comments

I have the same question (0)
  • Verified answer
    ChangeFrenzoId Profile Picture
    465 on at

    HI,


    Could you update your line 50...


    Xrm.Page.getAttribute("parentcontactid").setValue( [{id: theID, entityType: 'contact'}]);
    




    As you know parentcontactid is a lookup field,
    so while setting a lookup field via setValue - you need to pass the object of the target record as a parameter in setValue function.


  • Community Member Profile Picture
    on at

    Hi, thanks for the help. I have used Xrm.Page.getAttribute("parentcontactid").setValue( [{id: theID, entityType: 'contact'}]);

    it sets the contact entity, but it is "undefined". Any ideas? thanks james

  • Verified answer
    P. R. M Profile Picture
    739 on at

    Hi James,

    You need to set Name of the contact also. So retrieve the name of primarycontact of an account and set the value as below:

    Xrm.Page.getAttribute("parentcontactid").setValue( [{id: theID, name: contactName, entityType: 'contact'}]);

    Hope this helps you :)

  • Community Member Profile Picture
    on at

    adding the name in has worked. thanks Prasad and Piyush

  • Verified answer
    Luke Sartain Profile Picture
    1,266 on at

    Hi James

    Whilst you're amending your code it may also be worth you changing line 12 as .getServerUrl is now depreciated and will stop working in 2016.  Change:

    oDataPath = Xrm.Page.context.getServerUrl()

    to

    oDataPath = Xrm.Page.context.getClientUrl()

    msdn.microsoft.com/.../gg334511.aspx

    Luke

  • Community Member Profile Picture
    on at

    thanks for the tip luke!

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