locked
Why regardingobjectid.DataValue[0].name is different for Leads and Accounts RRS feed

  • Question

  • Hi,

    Does anyone know if it's possible to made the following object the same for both the Account entiry and the Lead entity:

    regardingobjectid.DataValue[0].name

    Under the Account entity the above retrieves the Company Name, but under the Lead entity it retrieves the Contact Name.

    I want to append the company name of either the Account or the Lead to the subject field when adding an Appointment, I've got this to work on on an OnSave event from the form, but as I mentioned if I add an appointment to an Account it appends the company name as needed, but if the appointment is added to a Lead, it appends the contact name.

    If someone could explain why there a difference I'd be grateful... be interesting to know.

    Dave C


    Dave C
    Saturday, March 20, 2010 3:22 PM

Answers

  • The regardingobjectid field can point to different entities.

    This lookup always displays the primary attribute value.

    • for accounts, the primary attribute is the name of the company
    • for leads, the primary attribute is the fullname of the contact in the lead 

    You can't change this behavior in a customization way

     

    If you want to change this behavior, you will need to develop a plugin that will change the value stored in the "name" property of the lookup field.


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Saturday, March 20, 2010 7:25 PM
    Moderator

All replies

  • The regardingobjectid field can point to different entities.

    This lookup always displays the primary attribute value.

    • for accounts, the primary attribute is the name of the company
    • for leads, the primary attribute is the fullname of the contact in the lead 

    You can't change this behavior in a customization way

     

    If you want to change this behavior, you will need to develop a plugin that will change the value stored in the "name" property of the lookup field.


    My blog : http://mscrmtools.blogspot.com
    You will find:
    Bulk Delete LauncherView Layout replicator
    ISV.Config Manager Form Javascript Manager
    Assembly RecoveryAnd others (use tool tag on my blog)
    Saturday, March 20, 2010 7:25 PM
    Moderator
  • You can use regardingobjectid.DataValue[0].type (entity type code) or regardingobjectid.DataValue[0].typename (entity name) to figure out which entity it is pointing to.  If it is an Account you can just use regardingobjectid.DataValue[0].name to append your Company Name and if it is a Lead then you can do a web service call to retrieve the Company Name attribute from the Lead record.

    Sunday, March 21, 2010 4:35 AM
  • Many thanks for the response... sorry to sound ignorant, but what would a web service call look like?

    I understand what you're suggesting, but not being a programmer I don't know where to start working this out. I understand that I first need to check what entity type the appointment is being opened against, i.e an Accout or a Lead. if it's an Account we're ok as I already have the code for this and it works just fine... however, if it's a Lead, this would be where the web service call comes in.

    Any chance someone could have a go and piutting something together for this, bit sure where I need to start.

    Many thanks in advance...

    Dave C


    Dave C
    Sunday, March 21, 2010 7:56 PM
  • The code below is for when you know it is a lead record.  I took it from the CRM SDK and modified it for your scenario but I haven't tested.  It will do a RetrieveMultiple request for the companyname attribute of the lead record by leadid.  It uses the GenerateAuthenticationHeader method that should already be defined natively on the Appointment form.

    Let me know if it doesn't work or if you have any questions.

     

    var leadid = crmForm.all.regardingobjectid.DataValue[0].id;
    // Use the GenerateAuthenticationHeader() method 
    // available from the CRM form to generate the Soap header text.
    var authenticationHeader = GenerateAuthenticationHeader();
    
    // Define the SOAP XML to access Microsoft Dynamics CRM Web service.
    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
    	"<soap:Envelope xmlns:soap="+
    	"\"http://schemas.xmlsoap.org/soap/envelope/\" "+
    	"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+
    	"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 
    	authenticationHeader+
    	"<soap:Body>" + 
    	// Specify the RetrieveMultiple message.
    	"<RetrieveMultiple xmlns="+
    	"\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
    	// Specify that this is a QueryByAttribute query.
    	"<query xmlns:q1="+
    	"\"http://schemas.microsoft.com/crm/2006/Query\" "+
    	"xsi:type=\"q1:QueryByAttribute\">" + 
    	// Query the lead entity.
    	"<q1:EntityName>lead</q1:EntityName>" + 
    	// Set the columns you want to return.
    	"<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
    	"<q1:Attributes>" + 
    	"<q1:Attribute>companyname</q1:Attribute>" + 
    	"</q1:Attributes>" + 
    	"</q1:ColumnSet>" + 
    	// Specify the attribute that you are querying on.
    	"<q1:Attributes>" + 
    	"<q1:Attribute>leadid</q1:Attribute>" + 
    	"</q1:Attributes>" + 
    	// Set the value of the attribute using the leadid 
    	"<q1:Values>" + 
    	"<q1:Value xsi:type=\"xsd:string\">"+
    	leadid+
    	"</q1:Value>" + 
    	"</q1:Values>" + 
    	"</query>" + 
    	"</RetrieveMultiple>" + 
    	"</soap:Body>" + 
    	"</soap:Envelope>";
    
    // Create an instance of an XMLHTTP object.
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    // Configure the XMLHttp object for the 
    // Microsoft CRM Web services.
    xmlHttpRequest.Open(
      "POST", 
      "/mscrmservices/2007/CrmService.asmx", 
      false
      );
    xmlHttpRequest.setRequestHeader(
      "SOAPAction",
      "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"
      );
    xmlHttpRequest.setRequestHeader(
      "Content-Type", "text/xml; charset=utf-8"
      );
    xmlHttpRequest.setRequestHeader(
      "Content-Length", xml.length
      );
    // Send the XMLHttp request.
    xmlHttpRequest.send(xml);
    // Capture the XMLHttp response in XML format.
    var resultXml = xmlHttpRequest.responseXML;
    
    // Create an XML document that you can parse.
    var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    oXmlDoc.async = false; 
    // Load the document that has the results.
    oXmlDoc.loadXML(resultXml.xml);
    // Get only the BusinessEntity results.
    var businessEntities = oXmlDoc.getElementsByTagName('BusinessEntity'); 
    if (businessEntities.length > 0)
    {
        var CompanyNameElement = businessEntities[0].selectSingleNode('./q1:companyname');
        // Check whether the value is null before setting it.
        var CompanyName =(CompanyNameElement == null)? " ": CompanyNameElement.text; 
    }

     

    Sunday, March 21, 2010 8:41 PM
  • Many thanks for this, but I have to say this seems a lot of code just to extract the Company Name associated with a Lead. Is all this really necessary?

    Dave C


    Dave C
    Wednesday, March 24, 2010 9:26 PM
  • If you want to call the CRM Service from JavaScript then it is necessary.  It is only a lot of code because it is building out the SOAP request to call the web service.  Another option would be to write a plugin on pre-create and pre-update to populate the subject.
    Wednesday, March 24, 2010 11:20 PM