locked
2nd Level lookup via REST RRS feed

  • Question

  • I'm running into trouble trying to do what I'd refer to as a "2nd level lookup"...let me elaborate...

    On the Appointment entity there's a lookup field named 'RequiredAttendees' which could either be an account, contact, lead, etc.  Generally our sales reps will initiate a new appointment from the Social Pane that will populate this field with whatever entity they were coming from.  What I'm attempting to do, is when the form loads, is look up this entity and populate the subject field with the Company Name and Contact name, and the Location field with the address of the entity in the RequiredAttendees field.

    I'm not having an issue getting any information off the first level entity...in other words, if the first entity in RequiredAttendees is a Contact, I can get the contact name and their address by getting the GUID and doing a lookup.  What I can't figure out...is how to get 2nd level information.  For example, If the entity is a Contact, and I want to get the name of the Account that is referenced in Contact.ParentCompanyId, I'm not able to figure out how to get it to pass me a GUID that I can look up the account.

    Am I making sense...and does anyone have an idea what I'm missing?  I realize I could probably do this after the appointment is saved via a workflow...but I was looking to populate this when the form loads.

    Here's the code I'm using to do what I'm doing so far. For example if you look at the 'else' block where the entity is a Contact, entity.ParentCustomerId is not giving me back a GUID as I would expect

    function setLocation(){
        var isNew = Xrm.Page.data.entity.attributes.get("subject").getValue();
        if (!isNew){
          entityAddress();
         }
    }
    
    function getGuid(field,index){
      /*  Return the guid of the value of the first item in the field that
          is passed in for the given index. This is necessary as some party
          fields may have multiple lookup objects */
          var objGuid = Xrm.Page.data.entity.attributes.get(field).getValue()[index].id;
          return objGuid;
    }
    
    function entityAddress(){
      /*  When the appointment form loads, we want to check if it's a new
          appointment, and if so we want to grab the address of the first
          object in the 'Required' field (if it's there).  */      
          var objGuid = "";
          objGuid = getGuid("requiredattendees",0);
                     
          if (objGuid){
              /*if the object exists, we're going to look up the object and 
                get the address information */
                if(lookupEntity("requiredattendees") == "account"){
                    SDK.REST.retrieveRecord(
                           objGuid,
                           "Account",
                           null,null,
                           function (entity) {
                            
                              if(entity){
                                  var objLocation = "";
                                  var objAddr1 = "";
                                  var objAddr2 = "";
                                  var objCity = "";
                                  var objState = "";
                                  var objZip = "";
                                  
                                  objAddr1 = entity.Address1_Line1;
                                  objAddr2 = entity.Address1_Line2;
                                  objCity = entity.Address1_City;
                                  objState = entity.Address1_StateOrProvince;
                                  if(objAddr1){objLocation += objAddr1;}
                                  if(objAddr2){objLocation += " " + objAddr2;}
                                  if(objCity){objLocation += ", " + objCity;}
                                  if(objState){objLocation += ", " + objState;}
                                  var locationField = Xrm.Page.data.entity.attributes.get("location");
                                  locationField.setValue(objLocation);
                                  var subjectField = Xrm.Page.data.entity.attributes.get("subject");
                                  subjectField.setValue(entity.Name);
                                  
                                  }             
                           },
                           function (error) {
                            alert(error.message);
                            }
                      );
                  }
                  else if (lookupEntity("requiredattendees") == "contact") {
                    SDK.REST.retrieveRecord(
                           objGuid,
                           "Contact",
                           null,null,
                           function (entity) {
                            
                              if(entity){
                                  var objLocation = "";
                                  var objAddr1 = "";
                                  var objAddr2 = "";
                                  var objCity = "";
                                  var objState = "";
                                  var objZip = "";
                                  
                                  var contactName = "";
                                  
                                 
                                  objAddr1 = entity.Address1_Line1;
                                  objAddr2 = entity.Address1_Line2;
                                  objCity = entity.Address1_City;
                                  objState = entity.Address1_StateOrProvince;
                                  if(objAddr1){objLocation += objAddr1;}
                                  if(objAddr2){objLocation += " " + objAddr2;}
                                  if(objCity){objLocation += ", " + objCity;}
                                  if(objState){objLocation += ", " + objState;}
                                  contactName = entity.FullName;
                                  var locationField = Xrm.Page.data.entity.attributes.get("location");
                                  locationField.setValue(objLocation);
                                  //var subjectField = Xrm.Page.data.entity.attributes.get("subject");
                                  //subjectField.setValue(contactName);
                                  //alert(getCompanyName(entity.ParentCustomerId));
                                  
                                  
                                  
                                  }             
                           },
                           function (error) {
                            alert(error.message);
                            }
                      );
                  }
                  
             }
    } 
    
    function lookupEntity(entity){
        //will return the entity that is stored in a lookup field
        var obj = Xrm.Page.getAttribute(entity);
        var objValue = obj.getValue();
        if(objValue){
          return objValue[0].entityType;
        }
    
    }


    • Edited by John-G-M Wednesday, April 1, 2015 4:32 PM
    Wednesday, April 1, 2015 1:36 PM

Answers

  • I figured this out.  I'm fairly new to CRM development and didn't realize I needed to construct an OData query to pass in that would essentially be the equivalent of a join.
    Monday, April 13, 2015 4:07 PM