none
CRM内js+Webservices的使用 RRS feed

  • 问题

  • 各位前辈好,我在CRM的同一个表单的onload里用了如下调用,请前辈看一下有什么问题:

    //试剂1.................................................................................lookup 筛选
    var field = crmForm.all.new_nameid1;
        var stauls="2";
        var name="Tris base";
         // Ensure that search box is not visible in a lookup dialog
        field.lookupbrowse = 1;
      // Pass fetch xml through search value parameter
        field.AddParam("search",
         "<fetch mapping='logical'><entity name='new_reagent'>"
        + "<filter><condition attribute='new_stauls' operator='eq' value='"
        + stauls
        + "' /><condition attribute='new_name' operator='eq' value='"
        + name
        + "' /></filter></entity></fetch>");
    //................................................................lookup赋初值
    // Prepare variables to fetch accounts.
    var fetchMapping = "logical";
    var entityName = "new_reagent";
    var firstColumn = "new_reagentid";
    var secondColumn = "new_name";
    var linkEntity = "systemuser";
    var linkEntityTo ="owninguser";
    var filterType = "and";
    var conditionAttribute1 = "new_name";
    var operator1 = "eq";
    var value1 = "Tris base";
    var conditionAttribute2 = "new_stauls";
    var operator2= "eq";
    var value2 = "2";
    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>&lt;fetch mapping='"+fetchMapping+"'&gt;"+
    "&lt;entity name='"+entityName+"'&gt;"+
    "&lt;attribute name='"+firstColumn+"'/&gt;"+
    "&lt;attribute name='"+secondColumn+"'/&gt;"+
    //"&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
    //"&lt;filter type='"+filterType+"'&gt;"+
    "&lt;filter&gt;"+
    "&lt;condition attribute='"+conditionAttribute1+"'"+
    " operator='"+operator1+"' value='"+value1+"'/&gt;"+
    "&lt;condition attribute='"+conditionAttribute2+"'"+
    " operator='"+operator2+"' value='"+value2+"'/&gt;"+
    "&lt;/filter&gt;"+
    //"&lt;/link-entity&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;</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('&lt;','<');
    resultSet.replace('&gt;','>');

    // 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');

            for (i=0;i < results.length;i++)
        {
         var idValue = results[i].selectSingleNode('./new_reagentid').nodeTypedValue;
         var name = results[i].selectSingleNode('./new_name').nodeTypedValue;
        }

    var start_id= results[0].selectSingleNode('./new_reagentid').nodeTypedValue;
    var start_name=results[0].selectSingleNode('./new_name').nodeTypedValue;
    if(results.length>1)
    {
    alert('试剂Tris base,不惟一,请选择!');
    }
        //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
       var lookupItem= new Object();
    //Set the id, typename, and name properties to the object.
       lookupItem.id = start_id;
       lookupItem.typename = 'new_reagent';
       lookupItem.name = start_name;
    // Add the object to the array.
       lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
       crmForm.all.new_nameid1.DataValue = lookupData;
    }

    //.....................................................................retrieve和试剂有关的信息
    // Prepare variables for a contact to retrieve.
    var new_reagentid = start_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>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>new_reagent</entityName>"+
    "<id>"+new_reagentid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_number</q1:Attribute>"+
    "<q1:Attribute>new_unite</q1:Attribute>"+
    "<q1:Attribute>new_uniteprice</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</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/Retrieve");
    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;
    var number=resultXml.selectSingleNode("//q1:new_number").nodeTypedValue;
    crmForm.all.new_number1.DataValue=number;
    var unite=resultXml.selectSingleNode("//q1:new_unite").nodeTypedValue;
    crmForm.all.new_unite1.DataValue=unite;
    var uniteprice=resultXml.selectSingleNode("//q1:new_uniteprice").nodeTypedValue;
    crmForm.all.new_uniteprice1.DataValue=Number(uniteprice);
    //试剂2.................................................................................lookup 筛选
    var field = crmForm.all.new_nameid2;
        var stauls="2";
        var name="EDTA";
         // Ensure that search box is not visible in a lookup dialog
        field.lookupbrowse = 1;
      // Pass fetch xml through search value parameter
        field.AddParam("search",
         "<fetch mapping='logical'><entity name='new_reagent'>"
        + "<filter><condition attribute='new_stauls' operator='eq' value='"
        + stauls
        + "' /><condition attribute='new_name' operator='eq' value='"
        + name
        + "' /></filter></entity></fetch>");

    //................................................................lookup赋初值
    // Prepare variables to fetch accounts.
    var fetchMapping = "logical";
    var entityName = "new_reagent";
    var firstColumn = "new_reagentid";
    var secondColumn = "new_name";
    var linkEntity = "systemuser";
    var linkEntityTo ="owninguser";
    var filterType = "and";
    var conditionAttribute1 = "new_name";
    var operator1 = "eq";
    var value1 = "EDTA";
    var conditionAttribute2 = "new_stauls";
    var operator2= "eq";
    var value2 = "2";
    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>&lt;fetch mapping='"+fetchMapping+"'&gt;"+
    "&lt;entity name='"+entityName+"'&gt;"+
    "&lt;attribute name='"+firstColumn+"'/&gt;"+
    "&lt;attribute name='"+secondColumn+"'/&gt;"+
    //"&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
    //"&lt;filter type='"+filterType+"'&gt;"+
    "&lt;filter&gt;"+
    "&lt;condition attribute='"+conditionAttribute1+"'"+
    " operator='"+operator1+"' value='"+value1+"'/&gt;"+
    "&lt;condition attribute='"+conditionAttribute2+"'"+
    " operator='"+operator2+"' value='"+value2+"'/&gt;"+
    "&lt;/filter&gt;"+
    //"&lt;/link-entity&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;</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('&lt;','<');
    resultSet.replace('&gt;','>');

    // 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');

            for (i=0;i < results.length;i++)
        {
         var idValue = results[i].selectSingleNode('./new_reagentid').nodeTypedValue;
         var name = results[i].selectSingleNode('./new_name').nodeTypedValue;
        }
    var start_id= results[0].selectSingleNode('./new_reagentid').nodeTypedValue;
    var start_name=results[0].selectSingleNode('./new_name').nodeTypedValue;
    if(results.length>1)
    {
    alert('试剂EDTA,不惟一,请选择!');
    }
        //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
       var lookupItem= new Object();
    //Set the id, typename, and name properties to the object.
       lookupItem.id = start_id;
       lookupItem.typename = 'new_reagent';
       lookupItem.name = start_name;
    // Add the object to the array.
       lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
       crmForm.all.new_nameid2.DataValue = lookupData;
    }

    //.....................................................................retrieve和试剂有关的信息
    // Prepare variables for a contact to retrieve.
    var new_reagentid = start_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>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>new_reagent</entityName>"+
    "<id>"+new_reagentid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_number</q1:Attribute>"+
    "<q1:Attribute>new_unite</q1:Attribute>"+
    "<q1:Attribute>new_uniteprice</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</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/Retrieve");
    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;
    var number=resultXml.selectSingleNode("//q1:new_number").nodeTypedValue;
    crmForm.all.new_number2.DataValue=number;
    var unite=resultXml.selectSingleNode("//q1:new_unite").nodeTypedValue;
    crmForm.all.new_unite2.DataValue=unite;
    var uniteprice=resultXml.selectSingleNode("//q1:new_uniteprice").nodeTypedValue;
    crmForm.all.new_uniteprice2.DataValue=Number(uniteprice);

    2011年2月17日 9:21

全部回复

  • 同学,请写出报错信息,代码太多了 你可以在代码的外围加入try……catch语句模块,以便找到准确错误。
    daemon lin
    2011年2月19日 7:16
  • 一小段一小段来,不要一下子就写了很多代码
    刀客 | MSN:caims@techsun.com | BLOG:http://caims.cnblogs.com | 提供微软CRM高级开发顾问外包
    2011年2月21日 5:19
    版主
  • 这里没有错误,我就是感觉这里有很多重复不知道是否可以优化一下,这是两个完全一样的代码模块,实现的功能是查找,筛选,赋值。
    2011年2月23日 1:46
  • 写一个共用的function就可以去掉一下重复的东东了
    jeff.han
    2011年2月23日 8:54
  • 谢谢,我好象明白一些了。虽然具体还不知道怎么做。非常感谢。
    2011年2月23日 10:53
  • //试剂1.................................................................................lookup 筛选
    var field = crmForm.all.new_nameid1;
        var stauls="2";
        var name="Tris base";
         // Ensure that search box is not visible in a lookup dialog
        field.lookupbrowse = 1;
      // Pass fetch xml through search value parameter
        field.AddParam("search",
         "<fetch mapping='logical'><entity name='new_reagent'>"
        + "<filter><condition attribute='new_stauls' operator='eq' value='"
        + stauls
        + "' /><condition attribute='new_name' operator='eq' value='"
        + name
        + "' /></filter></entity></fetch>");
    //................................................................lookup赋初值
    // Prepare variables to fetch accounts.
    var fetchMapping = "logical";
    var entityName = "new_reagent";
    var firstColumn = "new_reagentid";
    var secondColumn = "new_name";
    var linkEntity = "systemuser";
    var linkEntityTo ="owninguser";
    var filterType = "and";
    var conditionAttribute1 = "new_name";
    var operator1 = "eq";
    var value1 = "Tris base";
    var conditionAttribute2 = "new_stauls";
    var operator2= "eq";
    var value2 = "2";
    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>&lt;fetch mapping='"+fetchMapping+"'&gt;"+
    "&lt;entity name='"+entityName+"'&gt;"+
    "&lt;attribute name='"+firstColumn+"'/&gt;"+
    "&lt;attribute name='"+secondColumn+"'/&gt;"+
    //"&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
    //"&lt;filter type='"+filterType+"'&gt;"+
    "&lt;filter&gt;"+
    "&lt;condition attribute='"+conditionAttribute1+"'"+
    " operator='"+operator1+"' value='"+value1+"'/&gt;"+
    "&lt;condition attribute='"+conditionAttribute2+"'"+
    " operator='"+operator2+"' value='"+value2+"'/&gt;"+
    "&lt;/filter&gt;"+
    //"&lt;/link-entity&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;</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;

    比如这一段先debug


    jeff.han
    2011年2月24日 16:01