- Opportunity Product Category from Product Category
- Posted by Chris Brown on June 13th, 2008
Hello,
I am new to using the webservice and I am trying to pull the category from
the Product when I select a Product in Opportunity Product. Here is a start
to the code, but it is not working. Does anyone have any suggestions?
{
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>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>productid</q1:Attribute>" +
" <q1:Attribute>dv_productcategory</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>productid</q1:Operator>" +
" </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;
var entityNodes =
resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
for (var i = 0; i < entityNodes.length; i++) {
// Access the current array element
var entityNode = entityNodes[i];
// Attributes are child nodes of the BusinessEntity node. Use
selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used
before returns all
// matching nodes, but as an attribute name only occurs once,
selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var productidNode = entityNode.selectSingleNode("q1
roductid");
var dv_productcategoryNode =
entityNode.selectSingleNode("q1:dv_productcategory ");
// Always check for null values. If an attribute is set to null, it is
not contained in the
// resulting XML. And accessing productidNode.text when productidNode is
null leads to
// a runtime error.
var productid = (productidNode == null) ? null : productidNode.text;
var dv_productcategory = (dv_productcategoryNode == null) ? null :
dv_productcategoryNode.text;}
}
Thanks,
Chris Brown
- Posted by Michael Höhne on June 14th, 2008
You have specified the systemuser entity, which doesn't seem correct:
"<q1:EntityName>systemuser</q1:EntityName>" +
You can use http://www.stunnware.com/crm2/topic.aspx?id=Framework2 and
http://www.stunnware.com/crm2/topic....=JSWebService2 to create the
correct code, but maybe changing systemuser to product does the trick
already.
--
Michael Höhne, Microsoft Dynamics CRM MVP
CRM Blog: http://www.stunnware.com/?area=blog
----------------------------------------------------------
"Chris Brown" <ChrisBrown@discussions.microsoft.com> schrieb im Newsbeitrag
news:CD2DF051-43A4-40D4-85D1-5DF5C9638269@microsoft.com...
- Posted by Chris Brown on June 14th, 2008
Hi Michael,
That was a dumb mistake on my part. I changed systemuser to product and no
luck. I think the mistake is somewhere in this code. (I'm guessing where it
is supposed to filter.
Any suggestions?
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>productid</q1:Operator>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
Thanks,
Chris Brown
"Michael Höhne" wrote:
- Posted by Chris Brown on June 14th, 2008
So here is the new code, but still doesn't work. Can anyone help me out? I am
not sure how to tell it to narrow the product entity to the actual product
selected on the Opportunity Product Page. Thanks Chris.
{
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>product</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>productid</q1:Attribute>" +
" <q1:Attribute>dv_productcategory</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>productid</q1:Operator>" +
" </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;
var entityNodes =
resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
for (var i = 0; i < entityNodes.length; i++) {
// Access the current array element
var entityNode = entityNodes[i];
// Attributes are child nodes of the BusinessEntity node. Use
selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used
before returns all
// matching nodes, but as an attribute name only occurs once,
selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var productidNode = entityNode.selectSingleNode("q1
roductid");
var dv_productcategoryNode =
entityNode.selectSingleNode("q1:dv_productcategory ");
// Always check for null values. If an attribute is set to null, it is
not contained in the
// resulting XML. And accessing productidNode.text when productidNode is
null leads to
// a runtime error.
var productid = (productidNode == null) ? null : productidNode.text;
var dv_productcategory = (dv_productcategoryNode == null) ? null :
dv_productcategoryNode.text;}
}
"Chris Brown" wrote:
- Posted by Michael Höhne on June 14th, 2008
You can still look at my tools to get it done. On the other hand, your XML
contains a very obvious error:
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>productid</q1:Operator>" +
" </q1:Condition>" +
It reads "WHERE productid productid". That doesn't make any sense, right?
What you need is "WHERE productid = '{guid of product}'". Maybe this one is
better:
var the_productid = crmForm.all.productid.DataValue[0].id;
....
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + the_productid + " </q1:Value>" +
" </q1:Values>"+
" </q1:Condition>" +
I haven't tested the above though. Just wrote it down.
--
Michael Höhne, Microsoft Dynamics CRM MVP
CRM Blog: http://www.stunnware.com/?area=blog
----------------------------------------------------------
"Chris Brown" <ChrisBrown@discussions.microsoft.com> schrieb im Newsbeitrag
news
41FB988-3102-40CC-A0B7-3F364DE31561@microsoft.com...
- Posted by Chris Brown on June 14th, 2008
Hi Michael,
Thanks for your reply. Honestly I am very new to this, I don't know how to
really write XML or use your tool. I am trying to learn as I go and it is
very helpful learning from you and others. I will try you suggestion and see
if it works. Do you have any suggestion on getting started using your tool
and writing XML?
Is the tool your refering to called "JavaScript Web Service Calls 4.0"? I
have it downloaded and have it configured to work in my environment, but I
haven't figured out how to use it yet.
Thanks for your patience and help!
Respectfully,
Chris Brown
"Michael Höhne" wrote:
- Posted by Chris Brown on June 14th, 2008
Hi Michael,
I tried your suggestion, but no luck. I am sure it is really close now
though with your suggestion. So if you have any suggestions on getting me
started to really learn this, please let me know. For now this is what the
code looks like.
var the_productid = crmForm.all.productid.DataValue[0].id;
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>systemuser</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>productid</q1:Attribute>" +
" <q1:Attribute>dv_productcategory</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">" + the_productid + " </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;
var entityNodes =
resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
for (var i = 0; i < entityNodes.length; i++) {
// Access the current array element
var entityNode = entityNodes[i];
// Attributes are child nodes of the BusinessEntity node. Use
selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used
before returns all
// matching nodes, but as an attribute name only occurs once,
selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var productidNode = entityNode.selectSingleNode("q1
roductid");
var dv_productcategoryNode =
entityNode.selectSingleNode("q1:dv_productcategory ");
// Always check for null values. If an attribute is set to null, it is
not contained in the
// resulting XML. And accessing productidNode.text when productidNode is
null leads to
// a runtime error.
var productid = (productidNode == null) ? null : productidNode.text;
var dv_productcategory = (dv_productcategoryNode == null) ? null :
dv_productcategoryNode.text;}
Thanks,
Chris Brown
"Chris Brown" wrote:
- Posted by Michael Höhne on June 14th, 2008
Chris,
There are two tools. http://www.stunnware.com/crm2/topic.aspx?id=Framework2
gives you a query builder (the FetchXml wizard) and one of the outputs is a
piece of C# code. You can copy this code into the second tool,
http://www.stunnware.com/crm2/topic....=JSWebService2. When executing
the application you get the JavaScript code you need to do the same from a
CRM form script. I don't have more to say than already contained in these
two articles. However, there are some related articles (see "Related
Articles" in the left navigation bar, just as in CRM) that may give you some
better understanding. You have spent a lot of time so far in getting it
working. The tools provide a way to get the final result in 10 minutes, so
it's worth checking them anyway.
--
Michael Höhne, Microsoft Dynamics CRM MVP
CRM Blog: http://www.stunnware.com/?area=blog
----------------------------------------------------------
"Chris Brown" <ChrisBrown@discussions.microsoft.com> schrieb im Newsbeitrag
news:F53E1CB6-2036-4CDA-A90B-18669CBB3464@microsoft.com...
- Posted by Chris Brown on June 16th, 2008
Hi Michael,
Thanks for your suggestions. I have used your tools, and they are great. I
am starting to get the hang of it. But I still have 1 problem. The query is
looking for a GUID from the lookup field, and it is getting an error saying
that that I am not supplying it a GUID. If I manually put in a GUID it works.
Here is the code I am trying to use.
var OpportunityProduct = crmForm.all.productid.DataValue[0].id;
alert(OpportunityProduct);
{
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\">" +
" <soap:Header>" +
" <CrmAuthenticationToken
xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <AuthenticationType
xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
" <OrganizationName
xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">DeltaValve</OrganizationName>" +
" <CallerId
xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
" </CrmAuthenticationToken>" +
" </soap:Header>" +
" <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>product</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>productid</q1:Attribute>" +
" <q1:Attribute>dv_productcategory</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1
istinct>false</q1
istinct>" +
" <q1:PageInfo>" +
" <q1:PageNumber>1</q1:PageNumber>" +
" <q1:Count>1</q1:Count>" +
" </q1:PageInfo>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>productid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value
xsi:type=\"xsd:string\">OpportunityProduct</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;
alert(resultXml.xml);
var entityNodes =
resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
for (var i = 0; i < entityNodes.length; i++) {
// Access the current array element
var entityNode = entityNodes[i];
// Attributes are child nodes of the BusinessEntity node. Use
selectSingleNode to return
// the first occurrence of a named node. The selectNodes method we used
before returns all
// matching nodes, but as an attribute name only occurs once,
selectSingleNode is easier to use.
// Use the same namespace alias (q1) you have specified in the query.
var productidNode = entityNode.selectSingleNode("q1
roductid");
var dv_productcategoryNode =
entityNode.selectSingleNode("q1:dv_productcategory ");
// Always check for null values. If an attribute is set to null, it is
not contained in the
// resulting XML. And accessing productidNode.text when productidNode is
null leads to
// a runtime error.
var productid = (productidNode == null) ? null : productidNode.text;
var dv_productcategory = (dv_productcategoryNode == null) ? null :
dv_productcategoryNode.text;}
alert(dv_productcategory);
}
Do you have any suggestions?
Thanks again for your help!
Respectfully,
Chris Brown
"Michael Höhne" wrote:
- Posted by Chris Brown on June 16th, 2008
Sorry Michael,
I just needed to put this in the query. " + Opportunity Product + "
Now it works!
Thanks,
Chris
"Chris Brown" wrote: