locked
CRM 2011: service activity read 'customers' values RRS feed

  • Question

  • Hi,

    I am trying to retrieve the values from the 'customers' field in a service activity using a SOAP retrieve request. The customers field is different from the other customer fields because it allows more than 1 customer to be selected.

    I manage to read the values of other service activity fields but I retrieve a blank value for the customers field without any error.

    Any help please?

    Thanks


    Darren Mercieca

    Tuesday, March 6, 2012 9:35 AM

All replies

  • Hello Darren,

    Can you provide code that you use?


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Tuesday, March 6, 2012 9:41 AM
    Moderator
  • Here is the code

    I am make use of this library to make the call:

    http://www.bizforward.cws-international.com/2011/03/09/useful-javascript-functions-and-methods-for-crm-2011-cws-crm-utils-js-library/

    And it contains this function which genrates the xml

    CrmService.prototype.RetrieveMultiple = function()
    {
    	try {
    	    var xmlSoapHeader = "<?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\">";
    
    	    var xmlAuthHeader = GenerateAuthenticationHeader();
    
    	    var xmlSoapBody = "<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>" + this.getEntityName() + "</q1:EntityName>";
    		
    		if(this.ColumnSet != null)
    		{
    			if(this.ColumnSet.length != 1)
    			{
    				xmlSoapBody += "<q1:ColumnSet xsi:type=\"q1:ColumnSet\"><q1:Attributes>";
    
    				for (var i=0; i<this.ColumnSet.length; i++) {
    					var column = this.ColumnSet[i];
    					xmlSoapBody = xmlSoapBody + "<q1:Attribute>" + column + "</q1:Attribute>";
    				}
    
    				xmlSoapBody += "</q1:Attributes></q1:ColumnSet>";
    			}
    			else 
    				xmlSoapBody += "<q1:ColumnSet xsi:type=\"q1:AllColumns\" />";
    		}		
    	    
    	    xmlSoapBody += "<q1:Distinct>false</q1:Distinct><q1:PageInfo><q1:PageNumber>0</q1:PageNumber><q1:Count>0</q1:Count></q1:PageInfo><q1:LinkEntities>";
    
    	    if (this.LinkedEntities.length > 0) {
    		for (var i=0; i<this.LinkedEntities.length; i++) {
    		    var linkedEntity = this.LinkedEntities[linkedEntityNumber];
    		    xmlSoapBody += "<q1:LinkEntity> ";
    		    xmlSoapBody += "<q1:LinkFromAttributeName>" + linkedEntity.LinkFromAttributeName + "</q1:LinkFromAttributeName> ";
    		    xmlSoapBody += "<q1:LinkFromEntityName>" + linkedEntity.LinkFromEntityName + "</q1:LinkFromEntityName> ";
    		    xmlSoapBody += "<q1:LinkToEntityName>" + linkedEntity.LinkToEntityName + "</q1:LinkToEntityName> ";
    		    xmlSoapBody += "<q1:LinkToAttributeName>" + linkedEntity.LinkToAttributeName + "</q1:LinkToAttributeName> ";
    		    xmlSoapBody += "<q1:JoinOperator>" + linkedEntity.JoinOperator + "</q1:JoinOperator> ";
    		    xmlSoapBody += "<q1:LinkCriteria> ";
    
    		    if (linkedEntity.FilterOperator == null)
    			throw new Error("Must specify non-null value for FilterOperator");
    
    		    xmlSoapBody += "<q1:FilterOperator>" + linkedEntity.FilterOperator + "</q1:FilterOperator> ";
    		    xmlSoapBody += "<q1:Conditions> ";
    		    for (var i=0; i<linkedEntity.Conditions.length; i++) {
    			var conditionLinked = linkedEntity.Conditions[i];
    			xmlSoapBody += "<q1:Condition> ";
    			xmlSoapBody += "<q1:AttributeName>" + conditionLinked.Field + "</q1:AttributeName> ";
    			xmlSoapBody += "<q1:Operator>" + conditionLinked.Operator + "</q1:Operator> ";
    			    
    			if(conditionLinked.Operator != LogicalOperator.Null && conditionLinked.Operator != LogicalOperator.NotNull)
    			{
    				xmlSoapBody += "<q1:Values> ";
    				xmlSoapBody += "<q1:Value xsi:type=\"xsd:string\">" + conditionLinked.Value + "</q1:Value> ";
    				xmlSoapBody += "</q1:Values> ";
    			}
    			
    			xmlSoapBody += "</q1:Condition> ";
    		    }
    		    xmlSoapBody += " </q1:Conditions> ";
    		    xmlSoapBody += " <q1:Filters /> ";
    		    xmlSoapBody += "</q1:LinkCriteria> ";
    		    xmlSoapBody += "<q1:LinkEntities />";
    		    xmlSoapBody += "</q1:LinkEntity>";
    		}
    	    }
    
    	    if (this.LogicalOperator == null)
    		throw new Error("Must specify non-null value for LogicalOperator");
    
    	    xmlSoapBody += "</q1:LinkEntities><q1:Criteria><q1:FilterOperator>" + this.LogicalOperator + "</q1:FilterOperator><q1:Conditions>  "; 
    
    	    for (var i=0; i<this.Conditions.length; i++) {
    		var condition = this.Conditions[i];
    
    		if (condition.Operator == null)
    		    throw new Error("Must specify non-null value for condition Operator");
    
    		xmlSoapBody += "<q1:Condition><q1:AttributeName>" + condition.Field + "</q1:AttributeName> <q1:Operator>" + condition.Operator + "</q1:Operator>";
    		
    		if(condition.Operator != LogicalOperator.Null && condition.Operator != LogicalOperator.NotNull)
    			xmlSoapBody += "<q1:Values><q1:Value xsi:type=\"xsd:string\">" + condition.Value + "</q1:Value></q1:Values>";
    
    		xmlSoapBody +="</q1:Condition>";
    	    }
    
    	    xmlSoapBody += "</q1:Conditions><q1:Filters /></q1:Criteria><q1:Orders /></query></RetrieveMultiple></soap:Body></soap:Envelope>";
    
    	    var xmlt = xmlSoapHeader + xmlAuthHeader + xmlSoapBody;
    	    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", xmlt.length);
    	    xmlHttpRequest.send(xmlt);
    
    	    if (xmlHttpRequest.responseXML == null || xmlHttpRequest.responseXML.xml == null || xmlHttpRequest.responseXML.xml == "") {
    		if (xmlHttpRequest.responseText != null && xmlHttpRequest.responseText != "")
    		    throw new Error(xmlHttpRequest.responseText);
    		else
    		    throw new Error("Error returning response");
    	    }
    
    	    var xmlResponse = xmlHttpRequest.responseXML.xml;
    	    if (xmlHttpRequest.responseXML.documentElement.selectNodes("//error/description").length > 0) {
    		throw new Error(xmlResponse);
    	    }
    
    	    var objNodeList = xmlHttpRequest.responseXML.documentElement.selectNodes("//BusinessEntity");
    	    
    	    var totalNodesCount = objNodeList.length;
    	    
    	    var result = new RetrieveMultipleResult(this);
    
    	    var nodeIndex = 0;
    	    var fieldTextTemp = "";
    	    var fieldText = "";
    	    if (totalNodesCount > 0) {
    		do {
    
    		    var row = result.AddRow();
    		    for (var i=0; i<this.ColumnSet.length; i++) {
    			var columnName = this.ColumnSet[i];
    			fieldText = "";
    			var valueNode = objNodeList[nodeIndex].getElementsByTagName("q1:" + columnName)[0];
    			if (valueNode != null) {
    			    fieldTextTemp = valueNode.childNodes[0].nodeValue;
    			    if (fieldTextTemp != null && fieldTextTemp != "") {
    				fieldText = fieldText + fieldTextTemp;
    			    }
    			}
    			row.AddColumn(columnName, fieldText);
    		    }
    		    nodeIndex = nodeIndex + 1;
    		}
    		while (totalNodesCount > nodeIndex)
    	    }
    	    return result;
    	}
    	catch(err) {
    	}
    	
    	return null;
    }
    
    // Author: Cornel Croitoriu
    // E-mail: cornel.croitoriu@novensys.com

    My call to RetrieveActivity then calls the above function:

    var entityName = "serviceappointment";
    var outputColumns = [new CRMField("customers")];
    var item = RetrieveActivity(entityName, outputColumns, "{08044143-5B61-E111-BAAD-00155D0ADC02}");

    alert(item.GetValue("customers"));


    Darren Mercieca

    Tuesday, March 6, 2012 9:54 AM
  • Hello,

    In this case you will have to extend function because I believe customers field is array type so additional parsing will be required.

    My suggestion - after following line

    var xmlResponse = xmlHttpRequest.responseXML.xml;

    insert code alert(xmlResponse) - it will give you possibility to find the type of response field and parse it in proper way.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Tuesday, March 6, 2012 10:59 PM
    Moderator
  • Thank you for the help below is the xmlResponse, I can see that the two customers are separate in the XML, so how can I fix the function to parse and return all the customer values?

    Darren

    <?xml version="1.0"?>
    <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:Body>
        <RetrieveMultipleResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
          <RetrieveMultipleResult EntityName="serviceappointment" MoreRecords="0" PagingCookie="&lt;cookie page=&quot;1&quot;&gt;&lt;activityid last=&quot;{08044143-5B61-E111-BAAD-00155D0ADC02}&quot; first=&quot;{08044143-5B61-E111-BAAD-00155D0ADC02}&quot; /&gt;&lt;/cookie&gt;" TotalRecordCount="-1" TotalRecordCountLimitExceeded="0">
            <BusinessEntities xmlns="http://schemas.microsoft.com/crm/2006/WebServices">
              <BusinessEntity xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:serviceappointment">
                <q1:activityid>{08044143-5B61-E111-BAAD-00155D0ADC02}</q1:activityid>
                <q1:resources>
                  <q1:activityparty xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:activityparty">
                    <q1:ispartydeleted name="No">0</q1:ispartydeleted>
                    <q1:activityid>{08044143-5B61-E111-BAAD-00155D0ADC02}</q1:activityid>
                    <q1:participationtypemask name="Resource">10</q1:participationtypemask>
                    <q1:effort formattedvalue="1.00">1</q1:effort>
                    <q1:resourcespecid name="Selection Rule" type="resourcespec">{EE9D1F03-BB50-E111-9F46-00155D0ADC02}</q1:resourcespecid>
                    <q1:scheduledend date="2/27/2012" time="6:00 PM">2012-02-27T18:00:00+01:00</q1:scheduledend>
                    <q1:ownerid type="systemuser">{3EEEE1A3-D348-E111-BB04-00155D0ADC02}</q1:ownerid>
                    <q1:partyid type="equipment" name="John Zammit">{2D4F02CE-B950-E111-9F46-00155D0ADC02}</q1:partyid>
                    <q1:activitypartyid>{0A044143-5B61-E111-BAAD-00155D0ADC02}</q1:activitypartyid>
                    <q1:instancetypecode name="Not Recurring">0</q1:instancetypecode>
                    <q1:scheduledstart date="2/27/2012" time="5:00 PM">2012-02-27T17:00:00+01:00</q1:scheduledstart>
                  </q1:activityparty>
                  <q1:activityparty xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:activityparty">
                    <q1:ispartydeleted name="No">0</q1:ispartydeleted>
                    <q1:activityid>{08044143-5B61-E111-BAAD-00155D0ADC02}</q1:activityid>
                    <q1:participationtypemask name="Resource">10</q1:participationtypemask>
                    <q1:effort formattedvalue="1.00">1</q1:effort>
                    <q1:resourcespecid name="Selection Rule" type="resourcespec">{602DBBF2-CD50-E111-9F46-00155D0ADC02}</q1:resourcespecid>
                    <q1:scheduledend date="2/27/2012" time="6:00 PM">2012-02-27T18:00:00+01:00</q1:scheduledend>
                    <q1:ownerid type="systemuser">{3EEEE1A3-D348-E111-BB04-00155D0ADC02}</q1:ownerid>
                    <q1:partyid type="equipment" name="Peugeot 508  AGY123">{05E24FD4-B750-E111-9F46-00155D0ADC02}</q1:partyid>
                    <q1:activitypartyid>{09044143-5B61-E111-BAAD-00155D0ADC02}</q1:activitypartyid>
                    <q1:instancetypecode name="Not Recurring">0</q1:instancetypecode>
                    <q1:scheduledstart date="2/27/2012" time="5:00 PM">2012-02-27T17:00:00+01:00</q1:scheduledstart>
                  </q1:activityparty>
                </q1:resources>
                <q1:customers>
                  <q1:activityparty xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:activityparty">
                    <q1:ispartydeleted name="No">0</q1:ispartydeleted>
                    <q1:participationtypemask name="Customer">11</q1:participationtypemask>
                    <q1:donotemail name="Allow">0</q1:donotemail>
                    <q1:scheduledend date="2/27/2012" time="6:00 PM">2012-02-27T18:00:00+01:00</q1:scheduledend>
                    <q1:activityid>{08044143-5B61-E111-BAAD-00155D0ADC02}</q1:activityid>
                    <q1:donotfax name="Allow">0</q1:donotfax>
                    <q1:donotpostalmail name="Allow">0</q1:donotpostalmail>
                    <q1:ownerid type="systemuser">{3EEEE1A3-D348-E111-BB04-00155D0ADC02}</q1:ownerid>
                    <q1:partyid type="account" name="acme">{BD528696-7451-E111-9F46-00155D0ADC02}</q1:partyid>
                    <q1:activitypartyid>{0B044143-5B61-E111-BAAD-00155D0ADC02}</q1:activitypartyid>
                    <q1:instancetypecode name="Not Recurring">0</q1:instancetypecode>
                    <q1:scheduledstart date="2/27/2012" time="5:00 PM">2012-02-27T17:00:00+01:00</q1:scheduledstart>
                    <q1:donotphone name="Allow">0</q1:donotphone>
                  </q1:activityparty>
                  <q1:activityparty xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:activityparty">
                    <q1:ispartydeleted name="No">0</q1:ispartydeleted>
                    <q1:participationtypemask name="Customer">11</q1:participationtypemask>
                    <q1:donotemail name="Allow">0</q1:donotemail>
                    <q1:scheduledend date="2/27/2012" time="6:00 PM">2012-02-27T18:00:00+01:00</q1:scheduledend>
                    <q1:activityid>{08044143-5B61-E111-BAAD-00155D0ADC02}</q1:activityid>
                    <q1:donotfax name="Allow">0</q1:donotfax>
                    <q1:donotpostalmail name="Allow">0</q1:donotpostalmail>
                    <q1:ownerid type="systemuser">{3EEEE1A3-D348-E111-BB04-00155D0ADC02}</q1:ownerid>
                    <q1:partyid type="account" name="diamondme">{AACCC2F8-4252-E111-9F46-00155D0ADC02}</q1:partyid>
                    <q1:activitypartyid>{142426C1-6F68-E111-BAAD-00155D0ADC02}</q1:activitypartyid>
                    <q1:instancetypecode name="Not Recurring">0</q1:instancetypecode>
                    <q1:scheduledstart date="2/27/2012" time="5:00 PM">2012-02-27T17:00:00+01:00</q1:scheduledstart>
                    <q1:donotphone name="Allow">0</q1:donotphone>
                  </q1:activityparty>
                </q1:customers>
              </BusinessEntity>
            </BusinessEntities>
          </RetrieveMultipleResult>
        </RetrieveMultipleResponse>
      </soap:Body>
    </soap:Envelope>


    Darren Mercieca

    Wednesday, March 7, 2012 4:13 PM