Solved

Retrieve related data field via CRM lookup field

Posted on 2012-03-15
3
1,350 Views
Last Modified: 2012-03-29
Hello Experts,

Environment:  Microsoft Dynamics CRM 4.0
Entity:  Contracts

When opening an existing Contract, I want to display in an ALERT, the state where the Customer is located.  So, I open a existing Contract and bam, "FL" shows up in an ALERT statement for Clients where the CustomerId lookup field's corresponding Account record has "FL" for the value in the "address1_stateorprovince" field.  

What's the best way to do this?  I've been able to successfully alert the CustomerID, Name, & Typename as shown in the link below, under the lookup value section for example #1.

http://sites.google.com/site/ranjitclub/working-with-datatypes-in-javascript
0
Comment
Question by:2_under_par
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
feridun earned 500 total points
Comment Utility
To get the address1_stateorprovince field you will need to call the CRM webservices to retrieve the record for the customer.

In the contract form you can use JavaScript (as you have found) to alert the contents of any field that is shown on the form. However, the state is not on the form but is in the record for the customer.

The CRM 4.0 SDK has some information "Accessing Microsoft Dynamics CRM Web Services".
0
 
LVL 29

Assisted Solution

by:feridun
feridun earned 500 total points
Comment Utility
This article may help you get started: http://go4answers.webhost4life.com/Example/access-lookup-fields-data-javascript-117618.aspx

You'll need to amend to suit your instance; the structure is as follows:

an authentication section
a query that specifies the columns to return (in your case just the state)
construct and send the request to the web services
processing the result
0
 

Author Closing Comment

by:2_under_par
Comment Utility
Thanks!  I got busy and forgot to follow up with this thread.  There is a little bit extra posted below, in that I retrieved more fields than I needed, but this got it done.  Var named 'Mess' displays in an alert.

if(crmForm.FormType == 4 ) 
 {    // Create Form = 1

var Mess ='';
  var AccountLookUp = crmForm.all.customerid.DataValue;
 var authenticationHeader = window.opener.GenerateAuthenticationHeader();

if (AccountLookUp != null && AccountLookUp != "undefined")
{
// Get the value for the accountid from the array and set it to a variable
var AccountID = AccountLookUp [0].id;
var Name=AccountLookUp [0].name;

var Accountxml =  [  
        "<?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>",  
        "   <RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",  
       "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:QueryByAttribute\'>",
// Query the customeraddress entity.
 "<q1:EntityName>account</q1:EntityName>",
 // Set the columns you want to return.
"<q1:ColumnSet xsi:type='q1:ColumnSet'>",
 "<q1:Attributes>",
 "<q1:Attribute>address1_name</q1:Attribute>",
 "<q1:Attribute>address1_line1</q1:Attribute>",
"<q1:Attribute>address1_line2</q1:Attribute>",
 "<q1:Attribute>address1_line3</q1:Attribute>",
"<q1:Attribute>address1_city</q1:Attribute>",
 "<q1:Attribute>address1_stateorprovince</q1:Attribute>",
"<q1:Attribute>address1_postalcode</q1:Attribute>",
 "<q1:Attribute>address1_country</q1:Attribute>",
"<q1:Attribute>address1_telephone1</q1:Attribute>",
"<q1:Attribute>address1_addresstypecode</q1:Attribute>",
 "<q1:Attribute>address1_shippingmethodcode</q1:Attribute>",
"<q1:Attribute>address1_freighttermscode</q1:Attribute>",
 "<q1:Attribute>telephone1</q1:Attribute>",
 "</q1:Attributes>",
 "</q1:ColumnSet>", 
 // Specify the attribute that you are querying on.
 "<q1:Attributes>", 
 "<q1:Attribute>name</q1:Attribute>",
 "</q1:Attributes>", 
 // Set the value of the attribute using the customerid 
 // value of the case record.
 "<q1:Values>",
 "<q1:Value xsi:type='xsd:string'>",
Name ,
 "</q1:Value>",
 "</q1:Values>",
 "</query>" , 
"   </RetrieveMultiple>",  
        "</soap:Body>",  
        "</soap:Envelope>" 
        ].join("");  

// 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", Accountxml .length
  );
// Send the XMLHttp request.
 xmlHttpRequest.send(Accountxml );
// Capture the XMLHttp response in XML format.
 var resultAccountxml  = 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(resultAccountxml.xml);
 // Get only the BusinessEntity results.

 var businessEntities = oXmlDoc.getElementsByTagName('BusinessEntity');

  var errorCount = resultAccountxml.selectNodes('//error').length; 
    if (errorCount != 0) 
    { 
     var msg = resultAccountxml.selectSingleNode('//description').nodeTypedValue; 
     alert(msg); 
    }  


for (i=0;i < businessEntities.length;i++)
 {
if (businessEntities[i].selectSingleNode('./q1:address1_name') != null)
{
var name =businessEntities[i].selectSingleNode('./q1:address1_name').nodeTypedValue;
crmForm.all.address1_name.DataValue=name;
}

if(businessEntities[i].selectSingleNode('./q1:address1_stateorprovince')!=null)
{
var Mess =businessEntities[i].selectSingleNode('./q1:address1_stateorprovince').nodeTypedValue;
alert(Mess);
}



}
}
}

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello All, In previous article we used Hortonworks sandbox to work with Hadoop. Now, lets think to create own single node Hadoop on Linux. Here we Install and Configure Apache Hadoop on UI based Oracle Linux. I assume, you have VMware installe…
Salesforce.com’s Console is a great tool to view activities, leads, contacts, accounts and opportunities all in one screen. It is particularly effective during call blocks and working numerous activities at a time in a quick, repetitive fashion (suc…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now