locked
how to check if parentsubject id is null RRS feed

  • Question

  • Hi all,

     

    suppose that i have the below code sample to retrieve the parent subject id of the selected subject inserted in the onchange event of lookupfield:

     

     var title = crmForm.all.subjectid.DataValue[0].name;
     var parentSubjectId;
     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>"+ 
     "<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>subject</q1:EntityName>"+ 
     "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ 
     "<q1:Attributes>"+ 
     "<q1:Attribute>parentsubject</q1:Attribute>"+ 
     "</q1:Attributes>"+ 
     "</q1:ColumnSet>"+ 
     "<q1:Distinct>false</q1:Distinct>"+ 
     "<q1:Criteria>"+ 
     "<q1:FilterOperator>And</q1:FilterOperator>"+ 
     "<q1:Conditions>"+ 
     "<q1:Condition>"+ 
     "<q1:AttributeName>title</q1:AttributeName>"+ 
     "<q1:Operator>Like</q1:Operator>"+ 
     "<q1:Values>"+ 
     "<q1:Value xsi:type='xsd:string'>"+title+"</q1:Value>"+ 
     "</q1:Values>"+ 
     "</q1:Condition>"+ 
     "</q1:Conditions>"+ 
     "</q1:Criteria>"+ 
     "</query>"+ 
     "</RetrieveMultiple>"+ 
     "</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/RetrieveMultiple");
     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);
     }
     // Parse and display the results.
     else
     {
      var results = resultXml.getElementsByTagName('BusinessEntity');
      var msg = "";
      alert(results.length);
      if (results.length == 0)
      {
      msg = "null";
      alert(msg);
      return;
      }
      else
      {
      for (i=0;i < results.length;i++)
      {
       parentSubjectId = results[i].selectSingleNode('./q1:parentsubject').nodeTypedValue;
      }
      alert(parentSubjectId);
      }
     }
    

     

    how to check if the parent subject id is null ??, i.e. if the selected subject is at the top of the subject tree ??

     

    thanks in advance...


    Tuesday, May 10, 2011 11:32 AM

Answers

  • function retriveText(InputNode) {
      var text = "";
    
      if (InputNode != null) {
        //alert(InputNode[0].text);
        if (InputNode[0] != null) {
          text = InputNode[0].text;
        }
      }
    
      return text;
    }
    
    
    var parentsubjectId = retriveText(resultXml.selectNodes("//BusinessEntity/q1:parentsubject"));
    
    //if parentsubjectId is not empty then it is found otherwise not
    if (parentsubjectId != "") 
    {
    // write code to use parentsubjectId here
    }
    

    Please use above exmple code to satisfy your requirements,

    hope this helps..

     


    MayankP
    My Blog
    Follow Me on Twitter
    Tuesday, May 10, 2011 1:19 PM
    Answerer