locked
JScript to automatcally set Regarding on an appointment, possible? RRS feed

  • Question

  • Hello,

    I have been trying to figure out how to accomplish this:

    I would like to on an appointment set the Regarding(regardingobjectid) field with the parent account of the contact (or account) selected to the Required (requiredattendees). The script would run on the OnChange-event of the required field.

    I am able to get the fields to polpulate with data that lies inside the current record but cannot quite figure out how to manage to get the data from the related record of the Required-field. Is it possible, surely it has to be?

    I have been looking into web pages on REST, I suppose that is the way to go, but as I said, I haven't managed to figure it out quite yet.

    Does anyone have links or tips that would help to figure this out?

    Br,

    Janne

    Thursday, February 5, 2015 10:08 PM

All replies

  • Dear Janne,

    As per your post above , my understanding is that whenever you select contact or Account  in Required Field of the Appoint , It auto populates parent account of contact or account in the Regarding Field.

    Is It your requirement ? Please confirm me

    Friday, February 6, 2015 6:04 AM
  • Hello Janne,

    Recheck following article - https://msdn.microsoft.com/en-us/library/gg309549.aspx

    It describes how to use OData calls to get required information.



    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog


    Friday, February 6, 2015 6:06 AM
    Moderator
  • Hello KF,

    That was exactly what I was trying to do. I would obviously add a check for if the Regarding-field is empty and only then run the auto populate. But yes, you got me right.

    Br,

    Janne 

    Friday, February 6, 2015 6:51 AM
  • Thank you, Andrii.

    I will read through this!

    BR,

    Janne

    Friday, February 6, 2015 6:54 AM
  • Dear Janne,

    Try below code :

    function SetRegardingObjectId () {
     
    
    var requiredattendees = Xrm.Page.getAttribute("requiredattendees");
    
    if(requiredattendees.getValue() !=null)
     {
     var RequireAttendessType = Xrm.Page.getAttribute("requiredattendees").getValue()[0].entityType;
     if(RequireAttendessType =="contact")
     {
     var serverUrl  ;
     if(Xrm.Page.context.getClientUrl)//Post UR 12
     {
         serverUrl = Xrm.Page.context.getClientUrl();
     }
     else//Pre UR 12
     {
         serverUrl = Xrm.Page.context.getServerUrl();
     }
     
        var requiredAttendeesId = requiredattendees.getValue()[0].id;
    
         var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
         var oDataFilter ="/ContactSet?$select=ParentCustomerId&$filter=ContactId eq guid'" + requiredAttendeesId + "'";
    
         var userRequest = new XMLHttpRequest(); 
         userRequest.open("GET", ODataPath + oDataFilter , true); 
         userRequest.setRequestHeader("Accept", "application/json"); 
         userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    
      userRequest.onreadystatechange = function () {
       if (this.readyState == 4) {
        if (this.status == 200) {
         var retrievedRecords = JSON.parse(userRequest.responseText).d;
         if(retrievedRecords.results.length > 0)
          {
           var parentaccount = retrievedRecords.results[0];
           var lookup = new Array();
           lookup[0] = new Object();
           lookup[0].id = parentaccount.ParentCustomerId.Id;
           lookup[0].name = parentaccount.ParentCustomerId.Name;
           lookup[0].entityType = parentaccount.ParentCustomerId.LogicalName;
           Xrm.Page.getAttribute("regardingobjectid").setValue(lookup);
          }
              }
             }
                 };
    userRequest.send();
      }
      else
    {
    
           
           var lookup = new Array();
           lookup[0] = new Object();
           lookup[0].id = requiredattendees.getValue()[0].id;
           lookup[0].name = requiredattendees.getValue()[0].name;
           lookup[0].entityType = requiredattendees.getValue()[0].entityType;
           Xrm.Page.getAttribute("regardingobjectid").setValue(lookup);
    }
    }
    
    }

    • Proposed as answer by Mohd Saad Monday, February 9, 2015 10:37 AM
    Monday, February 9, 2015 10:36 AM