Solved

Retrieve related data field via CRM lookup field

Posted on 2012-03-15
3
1,382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Feridun Kadir earned 500 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 29

Assisted Solution

by:Feridun Kadir
Feridun Kadir earned 500 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

739 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