locked
CRM 4.0 JavaScript Related Entity Attribute RRS feed

  • Question

  • I have a Contract entity with an RespondWithinHrs attribute.  I have a Case entity with a lookup that points to the Contract entity.

    On the Case form, I want to put a RespondWithinHrs attribute, that gets automatically completed when the Contract is selected.

    How can I refer to a related entity attribute in JavaScript?  Something like:
     
    crmForm.all.new_contractid.DataValue[0].new_respondwithinhrs.DataValue? 
    Wednesday, February 11, 2009 8:55 AM

Answers

  • Hi,

    Call crm web service from javascript to find the RespondWithinHrs attribute value for the contract selected.Please refer the code below.

    //Prepare variables for a contact to retrieve.
    var lookupItem = new Array;

    // Get the lookup for the primarycontactid attribute on the account form.
    lookupItem = crmForm.all.new_contractid.DataValue;

    var contractid = lookupItem[0].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_contractid</entityName>"+
    "<id>"+contractid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>RespondWithinHrs</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 errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
    }
    //Display the retrieved value.
    else
    {
    alert(resultXml.selectSingleNode("//q1:RespondWithinHrs").nodeTypedValue);
    }
    Wednesday, February 11, 2009 9:54 AM

All replies

  • Hi,

    Call crm web service from javascript to find the RespondWithinHrs attribute value for the contract selected.Please refer the code below.

    //Prepare variables for a contact to retrieve.
    var lookupItem = new Array;

    // Get the lookup for the primarycontactid attribute on the account form.
    lookupItem = crmForm.all.new_contractid.DataValue;

    var contractid = lookupItem[0].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_contractid</entityName>"+
    "<id>"+contractid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>RespondWithinHrs</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 errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
    }
    //Display the retrieved value.
    else
    {
    alert(resultXml.selectSingleNode("//q1:RespondWithinHrs").nodeTypedValue);
    }
    Wednesday, February 11, 2009 9:54 AM
  • Hi,

    Call crm web service from javascript to find the RespondWithinHrs attribute value for the contract selected.Please refer the code below.

    //Prepare variables for a contact to retrieve.
    var lookupItem = new Array;

    // Get the lookup for the primarycontactid attribute on the account form.
    lookupItem = crmForm.all.new_contractid.DataValue;

    var contractid = lookupItem[0].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_contractid</entityName>"+
    "<id>"+contractid+"</id>"+
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
    "<q1:Attributes>"+
    "<q1:Attribute>RespondWithinHrs</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 errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
    var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    alert(msg);
    }
    //Display the retrieved value.
    else
    {
    alert(resultXml.selectSingleNode("//q1:RespondWithinHrs").nodeTypedValue);
    }

    Can i use this code in the onchange event of a lookup. I cretead a look up field in quote inorder to access user. When i select the user i need his last name to be displayed in a text box. Please help.
    Wednesday, December 9, 2009 1:05 PM
  • http://crmentropy.blogspot.com/2009/11/easy-to-use-javascript-retrieve-and.html

    Shamless self-plugging with Javascript functions that I wrote for this purpose.  You can use either set of code on the onChange event for the lookup, and provide dynamic information to the SOAP message.  My "RetrieveRecord" function works great for this, because it's easy to call with simply a GUID, record type, and attribute array:

    var contact = RetrieveRecord('contact', crmForm.all.contactid.DataValue[0].id, new Array("lastname"));
    alert(contact[0]);
    


    Dave Berry
    Wednesday, December 9, 2009 8:03 PM
    Moderator