?
Solved

Retrieve related data field via CRM lookup field

Posted on 2012-03-15
3
Medium Priority
?
1,408 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 30

Accepted Solution

by:
Feridun Kadir earned 2000 total points
ID: 37726046
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 30

Assisted Solution

by:Feridun Kadir
Feridun Kadir earned 2000 total points
ID: 37726054
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
ID: 37784419
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Desired Skill Set for Microsoft Dynamics CRM Technical Resources – Part II
Desired Skill Set for Microsoft Dynamics CRM Technical Resources – Part III
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 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