locked
Populated fields set to null after save RRS feed

  • Question

  • (CRM 4.0 for Outlook)

    I have some code in the OnChange event for the new_contactid field.  When I change the contact, the new_jobrolid and new_accountid appear with the correct values on the form.  However, once I save the record, the populated values disappear.  Am I missing something in my code? 

    function GetFields() 
    {
    if (crmForm.all.new_contactid.DataValue != null)
    {

    var lookUp = new Array();
    lookUp = crmForm.all.new_contactid.DataValue;
    var pId = lookUp[0].id;

    // Define the SOAP XML to access Microsoft Dynamics CRM Web service.

    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\">";

    if (typeof(GenerateAuthenticationHeader) != "undefined")
    {
     xml += GenerateAuthenticationHeader();
    }
    else
    {
    xml += "<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\">" + this._crmXmlEncode(this.org) + "</OrganizationName>" +
     "<CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
     "</CrmAuthenticationToken>" +
     "</soap:Header>";
    }
    xml += "<soap:Body>" +
     "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
     "<entityName>contact</entityName>" +
     "<id>" + pId + "</id>" +
     "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
     "<q1:Attributes>" +
      "<q1:Attribute>new_jobroleid</q1:Attribute>" + 
      "<q1:Attribute>parentcustomerid</q1:Attribute>" +
     "</q1:Attributes>" +
     "</columnSet>" +
     "</Retrieve>"+
     "</soap:Body>" +
     "</soap:Envelope>";

    // Create an instance of an XMLHTTP object.
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    // Configure the XMLHttp object for the Microsoft CRM Web services.
    xmlHttpRequest.Open("POST","/mscrmservices/2007/CrmService.asmx",false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);

    // Capture the response in XML format.

    var resultXml = xmlHttpRequest.responseXML;
    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0) {
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    else {
     var returnNode = resultXml.selectSingleNode("//q1:new_jobroleid");

     if( returnNode == null) {
      crmForm.all.new_jobroleid.DataValue = null;
     }
     else {
      var jobroleid = resultXml.selectSingleNode("q1:new_jobroleid");
      var jobrolename = resultXml.getElementsByTagName("q1:new_jobroleid")[0].getAttribute('name');
      var lookupData = new Array();
      var lookupItem = new Object();
      lookupItem.id = jobroleid;    
      lookupItem.typename = "new_jobrole";
           lookupItem.name = jobrolename;
      lookupData[0] = lookupItem;
      crmForm.all.new_jobroleid.DataValue = lookupData;

     }
     var returnNode2 = resultXml.selectSingleNode("//q1:parentcustomerid");

     if( returnNode2 == null) {
      crmForm.all.new_accountid.DataValue = null;
     }
     else {
      var accountid = resultXml.selectSingleNode("q1:parentcustomerid");
      var accountname = resultXml.getElementsByTagName("q1:parentcustomerid")[0].getAttribute('name');
      var accounttype = resultXml.getElementsByTagName("q1:parentcustomerid")[0].getAttribute('type');
      if ( accounttype == "account") {
       var lookupData2 = new Array();
       var lookupItem2 = new Object();
       lookupItem2.id = accountid;    
       lookupItem2.typename = "account";  
       lookupItem2.name = accountname;
       lookupData2[0] = lookupItem2;
       crmForm.all.new_accountid.DataValue = lookupData2;
      }
     }
    // crmForm.all.new_accountid.FireOnChange();
    // crmForm.all.new_jobroleid.FireOnChange();
    }

    }
    }

    /*
    ================================
      populate Role and Account
    ================================
    */
    GetFields();

    • Edited by J Wagner Wednesday, December 8, 2010 12:28 AM
    Tuesday, December 7, 2010 11:35 PM

Answers

  • Figured it out.  The ID value was null although the name appeared correctly.

    CURRENT CODE:

      var jobroleid = resultXml.selectSingleNode("q1:new_jobroleid");
      var jobrolename = resultXml.getElementsByTagName("q1:new_jobroleid")[0].getAttribute('name');

    CHANGE:

     var jobrolename =  returnNode.getAttribute("name");
     var jobroleid = returnNode.text;

    • Marked as answer by J Wagner Wednesday, December 8, 2010 4:07 AM
    Wednesday, December 8, 2010 4:07 AM

All replies

  • If, as I suspect, the fields are read-only, you need to force them to save, thus:

    crmForm.all.new_accountid.ForceSubmit = true;
    crmForm.all.new_jobroleid.ForceSubmit = true;
    
    

    --pogo (pat)
    Tuesday, December 7, 2010 11:56 PM
  • I had tried that.  I checked the properties as well and the 'field is read only' checkbox is not checked. 
    Wednesday, December 8, 2010 12:26 AM
  • Figured it out.  The ID value was null although the name appeared correctly.

    CURRENT CODE:

      var jobroleid = resultXml.selectSingleNode("q1:new_jobroleid");
      var jobrolename = resultXml.getElementsByTagName("q1:new_jobroleid")[0].getAttribute('name');

    CHANGE:

     var jobrolename =  returnNode.getAttribute("name");
     var jobroleid = returnNode.text;

    • Marked as answer by J Wagner Wednesday, December 8, 2010 4:07 AM
    Wednesday, December 8, 2010 4:07 AM