locked
code error RRS feed

  • Question

  • Hi,I've wrote down this code and get an error:    field:crmForm

                                                                          event:onsave

                                                                          error:Object required

    ----------------------------------------------------------------------------------------------------------------------
      var lookupitem3 = crmForm.all.new_contract.DataValue;
      if (lookupitem3 != null)
      {
        var contract_=lookupitem3[0].name;
        var contract__id=lookupitem3[0].id;
      }

    var authenticationHeader3 = GenerateAuthenticationHeader();

    var xml3 = "<?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'>"+
    authenticationHeader3+
    "<soap:Body>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>contract</entityName>"+
    "<id>"+contract__id+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>contractnumber</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</soap:Body>"+
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    var xHReq3 = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq3.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq3.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq3.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq3.setRequestHeader("Content-Length", xml3.length);
    xHReq3.send(xml3);
    // Capture the result.
    var resultXml3 = xHReq3.responseXML;

    // Check for errors.
    var errorCount3 = resultXml3.selectNodes('//error').length;
    if (errorCount3 != 0)
    {
     var msg3 = resultXml3.selectSingleNode('//description').nodeTypedValue;
    // alert(msg3);
    }
    // Display the retrieved value.
    else
    {
     crmForm.all.new_contract_number.DataValue= resultXml3.selectSingleNode("//q1:contractnumber").nodeTypedValue;
    //alert(crmForm.all.new_contract_number.DataValue);

    }

    Can you help me to understand,what kinde of mistake I've made?

    Thanks!

     

    Thursday, February 24, 2011 10:52 AM

Answers

  • As Yatin said, try this:

    var lookupitem2 = crmForm.all.customerid.DataValue;
    var account_ = null;
    var account__id = null;
      if (lookupitem2 != null)
      {
        account_=lookupitem2[0].name;
        account__id=lookupitem2[0].id;
      }



    When you're declaring the variables within the if-condition, it cannot be acessed "outside the braces". Declare the variables on the outside, assign them on the inside and you will  be able to refer to them later on on the outside as well.

    /Emil

    • Proposed as answer by Emil Mattsson Thursday, February 24, 2011 12:42 PM
    • Marked as answer by gMary Friday, February 25, 2011 7:54 AM
    Thursday, February 24, 2011 12:41 PM
  • try this... it should work

     

    if

     

    (crmForm.FormType == 1 || crmForm.FormType == 2)

    {

     

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

    var lookupitem1 = crmForm.all.new_bank_detial.DataValue;

     

    if (lookupitem1 != null)

    {

     

    var bank_detial = lookupitem1[0].name;

     

    var bank_detial_id = lookupitem1[0].id;

     

    var authenticationHeader1 = GenerateAuthenticationHeader();

     

    var xml1 = "<?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'>" +

    authenticationHeader1 +

     

    "<soap:Body>" +

     

    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +

     

    "<entityName>new_bank_details</entityName>" +

     

    "<id>" + bank_detial_id + "</id>" +

     

    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +

     

    "<q1:Attributes>" +

     

    "<q1:Attribute>new_bank_n</q1:Attribute>" +

     

    "</q1:Attributes>" +

     

    "</columnSet>" +

     

    "</Retrieve>" +

     

    "</soap:Body>" +

     

    "</soap:Envelope>";

     

    // Prepare the xmlHttpObject and send the request.

     

    var xHReq1 = new ActiveXObject("Msxml2.XMLHTTP");

    xHReq1.Open(

    "POST", "/mscrmservices/2007/CrmService.asmx", false);

    xHReq1.setRequestHeader(

    "SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");

    xHReq1.setRequestHeader(

    "Content-Type", "text/xml; charset=utf-8");

    xHReq1.setRequestHeader(

    "Content-Length", xml1.length);

    xHReq1.send(xml1);

     

    // Capture the result.

     

    var resultXml1 = xHReq1.responseXML;

     

    // Check for errors.

     

    var errorCount1 = resultXml1.selectNodes('//error').length;

     

    if (errorCount1 != 0)

    {

     

    var msg1 = resultXml1.selectSingleNode('//description').nodeTypedValue;

     

    // alert(msg1);

    }

     

    // Display the retrieved value.

     

    else

    {

    crmForm.all.new_bank_n.DataValue = resultXml1.selectSingleNode(

    "//q1:new_bank_n").nodeTypedValue;

    }

    }

     

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////second part

     

    var lookupitem2 = crmForm.all.customerid.DataValue;

     

    if (lookupitem2 != null)

    {

     

    var account_ = lookupitem2[0].name;

     

    var account__id = lookupitem2[0].id;

     

    var authenticationHeader2 = GenerateAuthenticationHeader();

     

    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'>" +

    authenticationHeader2 +

     

    "<soap:Body>" +

     

    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +

     

    "<entityName>account</entityName>" +

     

    "<id>" + account__id + "</id>" +

     

    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +

     

    "<q1:Attributes>" +

     

    "<q1:Attribute>new_bank</q1:Attribute>" +

     

    "<q1:Attribute>new_bank_account</q1:Attribute>" +

     

    "<q1:Attribute>address1_line1</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;

     

    // Check for errors.

     

    var errorCount2 = resultXml.selectNodes('//error').length;

     

    if (errorCount2 != 0)

    {

     

    var msg2 = resultXml.selectSingleNode('//description').nodeTypedValue;

    alert(msg2);

    }

     

    // Display the retrieved value.

     

    else

    {

    crmForm.all.new_shipto_banking_detials.DataValue = resultXml.selectSingleNode(

    "//q1:new_bank").nodeTypedValue;

    crmForm.all.new_shipto_bank_n.DataValue = resultXml.selectSingleNode(

    "//q1:new_bank_account").nodeTypedValue;

    crmForm.all.shipto_line1.DataValue = resultXml.selectSingleNode(

    "//q1:address1_line1").nodeTypedValue;

    }

    }

    }

    • Proposed as answer by Yatin Babaria Thursday, February 24, 2011 12:45 PM
    • Marked as answer by gMary Friday, February 25, 2011 7:54 AM
    Thursday, February 24, 2011 12:43 PM

All replies

  • contract__id is throwing error as it is delcared within if condition and you are trying to access it outside of the scope in fetchxml query at statement {<id>"+contract__id+"</id>"+}

    • Proposed as answer by Yatin Babaria Thursday, February 24, 2011 11:06 AM
    Thursday, February 24, 2011 11:04 AM
  • Hi Yatin,to be a frank I don't understand what you mean.So what I should do to work this code ?The same mistake I get when I write this code:

    if(crmForm.FormType == 1 || crmForm.FormType == 2)
    {

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      var lookupitem1 = crmForm.all.new_bank_detial.DataValue;
      if (lookupitem1 != null)
      {
        var bank_detial=lookupitem1[0].name;
        var bank_detial_id=lookupitem1[0].id;
      }

    var authenticationHeader1 = GenerateAuthenticationHeader();

    var xml1 = "<?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'>"+
    authenticationHeader1+
    "<soap:Body>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>new_bank_details</entityName>"+
    "<id>"+bank_detial_id+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_bank_n</q1:Attribute>"+
    "</q1:Attributes>"+
    "</columnSet>"+
    "</Retrieve>"+
    "</soap:Body>"+
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    var xHReq1 = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq1.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq1.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq1.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq1.setRequestHeader("Content-Length", xml1.length);
    xHReq1.send(xml1);
    // Capture the result.
    var resultXml1 = xHReq1.responseXML;

    // Check for errors.
    var errorCount1 = resultXml1.selectNodes('//error').length;
    if (errorCount1 != 0)
    {
     var msg1 = resultXml1.selectSingleNode('//description').nodeTypedValue;
    // alert(msg1);
    }
    // Display the retrieved value.
    else
    {
     crmForm.all.new_bank_n.DataValue= resultXml1.selectSingleNode("//q1:new_bank_n").nodeTypedValue;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////second part
      var lookupitem2 = crmForm.all.customerid.DataValue;
      if (lookupitem2 != null)
      {
        var account_=lookupitem2[0].name;
        var account__id=lookupitem2[0].id;
      }

    var authenticationHeader2 = GenerateAuthenticationHeader();

    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'>"+
    authenticationHeader2+
    "<soap:Body>"+
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
    "<entityName>account</entityName>"+
    "<id>"+account__id+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>new_bank</q1:Attribute>"+
    "<q1:Attribute>new_bank_account</q1:Attribute>"+
    "<q1:Attribute>address1_line1</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;

    // Check for errors.
    var errorCount2 = resultXml.selectNodes('//error').length;
    if (errorCount2 != 0)
    {
     var msg2 = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg2);
    }
    // Display the retrieved value.
    else
    {
     crmForm.all.new_shipto_banking_detials.DataValue= resultXml.selectSingleNode("//q1:new_bank").nodeTypedValue;

     crmForm.all.new_shipto_bank_n.DataValue= resultXml.selectSingleNode("//q1:new_bank_account").nodeTypedValue;

    crmForm.all.shipto_line1.DataValue= resultXml.selectSingleNode("//q1:address1_line1").nodeTypedValue;
    }

     

    At the first time it worked,but now not(I get error from second part).

    Thursday, February 24, 2011 12:04 PM
  •  var lookupitem2 = crmForm.all.customerid.DataValue;

    what's customerid?

    Is it a custom field?

    check if that field exists.

     

     

    Thursday, February 24, 2011 12:31 PM
  • As Yatin said, try this:

    var lookupitem2 = crmForm.all.customerid.DataValue;
    var account_ = null;
    var account__id = null;
      if (lookupitem2 != null)
      {
        account_=lookupitem2[0].name;
        account__id=lookupitem2[0].id;
      }



    When you're declaring the variables within the if-condition, it cannot be acessed "outside the braces". Declare the variables on the outside, assign them on the inside and you will  be able to refer to them later on on the outside as well.

    /Emil

    • Proposed as answer by Emil Mattsson Thursday, February 24, 2011 12:42 PM
    • Marked as answer by gMary Friday, February 25, 2011 7:54 AM
    Thursday, February 24, 2011 12:41 PM
  • try this... it should work

     

    if

     

    (crmForm.FormType == 1 || crmForm.FormType == 2)

    {

     

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

    var lookupitem1 = crmForm.all.new_bank_detial.DataValue;

     

    if (lookupitem1 != null)

    {

     

    var bank_detial = lookupitem1[0].name;

     

    var bank_detial_id = lookupitem1[0].id;

     

    var authenticationHeader1 = GenerateAuthenticationHeader();

     

    var xml1 = "<?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'>" +

    authenticationHeader1 +

     

    "<soap:Body>" +

     

    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +

     

    "<entityName>new_bank_details</entityName>" +

     

    "<id>" + bank_detial_id + "</id>" +

     

    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +

     

    "<q1:Attributes>" +

     

    "<q1:Attribute>new_bank_n</q1:Attribute>" +

     

    "</q1:Attributes>" +

     

    "</columnSet>" +

     

    "</Retrieve>" +

     

    "</soap:Body>" +

     

    "</soap:Envelope>";

     

    // Prepare the xmlHttpObject and send the request.

     

    var xHReq1 = new ActiveXObject("Msxml2.XMLHTTP");

    xHReq1.Open(

    "POST", "/mscrmservices/2007/CrmService.asmx", false);

    xHReq1.setRequestHeader(

    "SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");

    xHReq1.setRequestHeader(

    "Content-Type", "text/xml; charset=utf-8");

    xHReq1.setRequestHeader(

    "Content-Length", xml1.length);

    xHReq1.send(xml1);

     

    // Capture the result.

     

    var resultXml1 = xHReq1.responseXML;

     

    // Check for errors.

     

    var errorCount1 = resultXml1.selectNodes('//error').length;

     

    if (errorCount1 != 0)

    {

     

    var msg1 = resultXml1.selectSingleNode('//description').nodeTypedValue;

     

    // alert(msg1);

    }

     

    // Display the retrieved value.

     

    else

    {

    crmForm.all.new_bank_n.DataValue = resultXml1.selectSingleNode(

    "//q1:new_bank_n").nodeTypedValue;

    }

    }

     

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////second part

     

    var lookupitem2 = crmForm.all.customerid.DataValue;

     

    if (lookupitem2 != null)

    {

     

    var account_ = lookupitem2[0].name;

     

    var account__id = lookupitem2[0].id;

     

    var authenticationHeader2 = GenerateAuthenticationHeader();

     

    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'>" +

    authenticationHeader2 +

     

    "<soap:Body>" +

     

    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +

     

    "<entityName>account</entityName>" +

     

    "<id>" + account__id + "</id>" +

     

    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +

     

    "<q1:Attributes>" +

     

    "<q1:Attribute>new_bank</q1:Attribute>" +

     

    "<q1:Attribute>new_bank_account</q1:Attribute>" +

     

    "<q1:Attribute>address1_line1</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;

     

    // Check for errors.

     

    var errorCount2 = resultXml.selectNodes('//error').length;

     

    if (errorCount2 != 0)

    {

     

    var msg2 = resultXml.selectSingleNode('//description').nodeTypedValue;

    alert(msg2);

    }

     

    // Display the retrieved value.

     

    else

    {

    crmForm.all.new_shipto_banking_detials.DataValue = resultXml.selectSingleNode(

    "//q1:new_bank").nodeTypedValue;

    crmForm.all.new_shipto_bank_n.DataValue = resultXml.selectSingleNode(

    "//q1:new_bank_account").nodeTypedValue;

    crmForm.all.shipto_line1.DataValue = resultXml.selectSingleNode(

    "//q1:address1_line1").nodeTypedValue;

    }

    }

    }

    • Proposed as answer by Yatin Babaria Thursday, February 24, 2011 12:45 PM
    • Marked as answer by gMary Friday, February 25, 2011 7:54 AM
    Thursday, February 24, 2011 12:43 PM
  • Thanks for your advices,now everything is working.
    Friday, February 25, 2011 7:00 AM