Answered by:
How can i get account or contact number?

Question
-
HI expert,
In the CRM case, we can choose account or contact. How can i know it is account or contact by javascript? If we can know that how can i get account or contact number?
Kindly, please guide to me.
Best Regards,
Yukon
Monday, January 10, 2011 2:32 PM
Answers
-
I have tested the following code. Just change attribute names according to your envoirnment. Put this code on change of customer.
if (crmForm.all.customerid.DataValue != null) { var oEntity = crmForm.all.customerid.DataValue[0].typename; var oId = crmForm.all.customerid.DataValue[0].id; if (oEntity == 'account') { //do something on account 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\">" + GenerateAuthenticationHeader() + " <soap:Body>" + " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + " <q1:EntityName>account</q1:EntityName>" + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + " <q1:Attributes>" + " <q1:Attribute>accountid</q1:Attribute>" + " <q1:Attribute>accountnumber</q1:Attribute>" + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct>" + " <q1:Criteria>" + " <q1:FilterOperator>And</q1:FilterOperator>" + " <q1:Conditions>" + " <q1:Condition>" + " <q1:AttributeName>accountid</q1:AttributeName>" + " <q1:Operator>Equal</q1:Operator>" + " <q1:Values>" + " <q1:Value xsi:type=\"xsd:string\">" + oId + "</q1:Value>" + " </q1:Values>" + " </q1:Condition>" + " </q1:Conditions>" + " </q1:Criteria>" + " </query>" + " </RetrieveMultiple>" + " </soap:Body>" + "</soap:Envelope>"; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 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); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML.xml; // alert(resultXml ); if (resultXml != null || resultXml != "") { var xmlDocument = new ActiveXObject("Microsoft.XMLDOM"); var bLoaded = xmlDocument.loadXML(resultXml); if (bLoaded) try { var oAccountNo = xmlDocument.selectSingleNode("//BusinessEntity/q1:accountnumber").text; crmForm.all.new_number.DataValue = oAccountNo; } catch (err) { alert("Account No is Missing"); } } } else if (oEntity == 'contact') { //do something on contact 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\">" + GenerateAuthenticationHeader() + " <soap:Body>" + " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + " <q1:EntityName>contact</q1:EntityName>" + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + " <q1:Attributes>" + " <q1:Attribute>contactid</q1:Attribute>" + " <q1:Attribute>new_contactnumber</q1:Attribute>" + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct>" + " <q1:Criteria>" + " <q1:FilterOperator>And</q1:FilterOperator>" + " <q1:Conditions>" + " <q1:Condition>" + " <q1:AttributeName>contactid</q1:AttributeName>" + " <q1:Operator>Equal</q1:Operator>" + " <q1:Values>" + " <q1:Value xsi:type=\"xsd:string\">" + oId + "</q1:Value>" + " </q1:Values>" + " </q1:Condition>" + " </q1:Conditions>" + " </q1:Criteria>" + " </query>" + " </RetrieveMultiple>" + " </soap:Body>" + "</soap:Envelope>"; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 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); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML.xml; // alert(resultXml ); if (resultXml != null || resultXml != "") { var xmlDocument = new ActiveXObject("Microsoft.XMLDOM"); var bLoaded = xmlDocument.loadXML(resultXml); if (bLoaded) try { var oContactNo = xmlDocument.selectSingleNode("//BusinessEntity/q1:new_contactnumber").text; crmForm.all.new_number.DataValue = oContactNo; } catch (err) { alert("Contact No is Missing"); } } } }
Regards Faisal- Marked as answer by Yukonn Wednesday, January 19, 2011 6:42 AM
Thursday, January 13, 2011 12:32 AM -
You are always welcome.
Yes you can store new_number as hidden field using following code onload of form:-
crmForm.all.new_number_c.style.display = 'none' ;
crmForm.all.new_number_d.style.display = 'none' ;
Regards Faisal- Marked as answer by Yukonn Friday, January 21, 2011 3:29 AM
Wednesday, January 19, 2011 8:49 AM
All replies
-
you can write a JS code on lookup onchange to check lookup "typename" based on that you can use ms crm retrieve methode to fetch phone number.
you check type like below
if (crmForm.all.customerid.DataValue!= null)
{
alert(lcrmForm.all.customerid.DataValue[0].typename);
}refer http://msdn.microsoft.com/en-us/library/cc677076.aspx
Mahain : My Dynamics CRM Blog- Proposed as answer by HIMBAPModerator Monday, January 10, 2011 3:05 PM
Monday, January 10, 2011 3:04 PMModerator -
if (crmForm.all.customerid.DataValue!= null)
{
if (crmForm.all.customerid.DataValue[0].typename == 'Account')
{
//do something on account
}
else if (crmForm.all.customerid.DataValue[0].typename == 'Contact')
{
//do something on contact
}}
Regards FaisalMonday, January 10, 2011 5:22 PM -
Hi Yukonn,
As Faisal says, you can identify if the customer is a Contact or an Account by using the prperty "typename" or the property "type" (1=Ccount / 2=Contact).
In the other hand, when you refer "account or contact number", are you referring to the telephone number??? or another attribute???.
To get that attribute can be a little difficult. You have to invoke a WebService. The JScript might be something like this: (REPLACE [NUMBER] BY THE ATTRIBUTE THAT YOU WANT TO GET)var guidName;
if (crmForm.all.customerid.DataValue!= null)
{
if (crmForm.all.customerid.DataValue[0].typename == 'Account')
{
//do something on account
guidName="accountid";
}
else if (crmForm.all.customerid.DataValue[0].typename == 'Contact')
{
//do something on contact
guidName="contactid";
}}
//Prepare variables.
var customerid = crmForm.all.customerid.DataValue[0].id;
var authenticationHeader = GenerateAuthenticationHeader();//Prepare the SOAP message.
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>"+
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<fetchXml><fetch mapping='logical'>"+
"<entity name='"+crmForm.all.customerid.DataValue[0].typename"'>"+
"<attribute name='[NUMBER]'/>"+
"<filter type='and'>"+
"<condition attribute='"+ guidName+"' operator='eq' value='" + customerid + "'/>"+
"</filter>"+
"</entity>"+
"</fetch></fetchXml>"+
"</Fetch>"+
"</soap:Body>"+
"</soap:Envelope>";
//Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
//Capture the result
var resultXml = xHReq.responseXML;//Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
//Process and display the results.
else
{//Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<');
resultSet.replace('>','>');// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);
//Display the results.
var results = oXmlDoc.getElementsByTagName('result');
var number = "";
for (i=0;i < results.length;i++)
{
if(results[i].selectSingleNode('./[NUMBER]') != null)
number = results[i].selectSingleNode('./[NUMBER]').nodeTypedValue;
}
alert(number);
}
JulioMonday, January 10, 2011 7:46 PM -
Hi all,
Specially thanks for all. Your replies is useful for me. Kindly, please let me try first base on your solution. If i got problem, i will let you know again.
Julio, Yes! I wann to get account number or contact number attribute. Your code sample can be solve my problem?
Best Regards,
Yukon
Tuesday, January 11, 2011 1:22 AM -
Hi Yukon,
I haven't tested it but I believe that it's correct. You only have to replace [NUMBER] by the attribute that you want to get.
JulioWednesday, January 12, 2011 6:35 PM -
I have tested the following code. Just change attribute names according to your envoirnment. Put this code on change of customer.
if (crmForm.all.customerid.DataValue != null) { var oEntity = crmForm.all.customerid.DataValue[0].typename; var oId = crmForm.all.customerid.DataValue[0].id; if (oEntity == 'account') { //do something on account 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\">" + GenerateAuthenticationHeader() + " <soap:Body>" + " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + " <q1:EntityName>account</q1:EntityName>" + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + " <q1:Attributes>" + " <q1:Attribute>accountid</q1:Attribute>" + " <q1:Attribute>accountnumber</q1:Attribute>" + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct>" + " <q1:Criteria>" + " <q1:FilterOperator>And</q1:FilterOperator>" + " <q1:Conditions>" + " <q1:Condition>" + " <q1:AttributeName>accountid</q1:AttributeName>" + " <q1:Operator>Equal</q1:Operator>" + " <q1:Values>" + " <q1:Value xsi:type=\"xsd:string\">" + oId + "</q1:Value>" + " </q1:Values>" + " </q1:Condition>" + " </q1:Conditions>" + " </q1:Criteria>" + " </query>" + " </RetrieveMultiple>" + " </soap:Body>" + "</soap:Envelope>"; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 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); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML.xml; // alert(resultXml ); if (resultXml != null || resultXml != "") { var xmlDocument = new ActiveXObject("Microsoft.XMLDOM"); var bLoaded = xmlDocument.loadXML(resultXml); if (bLoaded) try { var oAccountNo = xmlDocument.selectSingleNode("//BusinessEntity/q1:accountnumber").text; crmForm.all.new_number.DataValue = oAccountNo; } catch (err) { alert("Account No is Missing"); } } } else if (oEntity == 'contact') { //do something on contact 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\">" + GenerateAuthenticationHeader() + " <soap:Body>" + " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + " <q1:EntityName>contact</q1:EntityName>" + " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + " <q1:Attributes>" + " <q1:Attribute>contactid</q1:Attribute>" + " <q1:Attribute>new_contactnumber</q1:Attribute>" + " </q1:Attributes>" + " </q1:ColumnSet>" + " <q1:Distinct>false</q1:Distinct>" + " <q1:Criteria>" + " <q1:FilterOperator>And</q1:FilterOperator>" + " <q1:Conditions>" + " <q1:Condition>" + " <q1:AttributeName>contactid</q1:AttributeName>" + " <q1:Operator>Equal</q1:Operator>" + " <q1:Values>" + " <q1:Value xsi:type=\"xsd:string\">" + oId + "</q1:Value>" + " </q1:Values>" + " </q1:Condition>" + " </q1:Conditions>" + " </q1:Criteria>" + " </query>" + " </RetrieveMultiple>" + " </soap:Body>" + "</soap:Envelope>"; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 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); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML.xml; // alert(resultXml ); if (resultXml != null || resultXml != "") { var xmlDocument = new ActiveXObject("Microsoft.XMLDOM"); var bLoaded = xmlDocument.loadXML(resultXml); if (bLoaded) try { var oContactNo = xmlDocument.selectSingleNode("//BusinessEntity/q1:new_contactnumber").text; crmForm.all.new_number.DataValue = oContactNo; } catch (err) { alert("Contact No is Missing"); } } } }
Regards Faisal- Marked as answer by Yukonn Wednesday, January 19, 2011 6:42 AM
Thursday, January 13, 2011 12:32 AM -
Hi All,
Thanks for your help! Esp: thx to Faisal Fiaz. It is work for me.
One more thing! Can i save acc/contact number to new_number. But i don't to show on screen. It means hidden field or background update to new_number. Because of this value no need to show user. Purpose for my own other process.
I take your golden time. Sorry that!
Best Regards,
Yukon
Make Simple & EasyWednesday, January 19, 2011 6:45 AM -
You are always welcome.
Yes you can store new_number as hidden field using following code onload of form:-
crmForm.all.new_number_c.style.display = 'none' ;
crmForm.all.new_number_d.style.display = 'none' ;
Regards Faisal- Marked as answer by Yukonn Friday, January 21, 2011 3:29 AM
Wednesday, January 19, 2011 8:49 AM -
Hi Faisal,
Thx. It works!
Best Regards,
Yukon
Make Simple & EasyFriday, January 21, 2011 3:29 AM -
For 2011, if you want to create entity records, fast, I recommend the following post: http://bizforward.blogspot.com/2011/01/call-crm-service-function.html . It also provides an example of how to create a new contact and a new note/attachment.
Cornel Croitoriu - Senior Software Developer - http://bizforward.blogspot.comWednesday, January 26, 2011 8:43 PM