locked
Copy lookup data to from one entity to another RRS feed

  • Question

  • Hi,

    I have a custom entity called Application and the other entity is Contact.  I have created the relationship between (N:1) and called it "Application Contact".  I put this "Application Contact" field on the Application form itself, which now of course makes it a lookup.  Basically, my goal is that when I click "New" for a new Application and I look up the "Application Contact" in that field, if the person already exists, I would like the other fields on the Application form to automatically fill in with the person's information (First Name, Last Name, Address, etc.).  After doing some research, I believe it has to do with web services (WS), but this is a brand new concept to me.  Can anyone help me acheive this?  Thanks

    Thursday, December 16, 2010 4:10 PM

Answers

  • Hi,

    you can use below code on applicationcontact field's onchange, Please remember to change field names 

    if(crmForm.all.<LookupFieldName>.DataValue!=null)
    {
        var contactid = crmForm.all.<LookupFieldName>.DataValue[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>contact</entityName>" +
    "<id>" + contactid + "</id>" +
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
    "<q1:Attributes>" +
    "<q1:Attribute>fullname</q1:Attribute>" +   //add more field that you want to fetch
    "</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 {
       crmForm.all.<FieldName>.DataValue=resultXml.selectSingleNode("//q1:fullname").nodeTypedValue); //you can all field like this
        }
    }

     


    Mahain : My Dynamics CRM Blog
    Thursday, December 16, 2010 9:17 PM
    Moderator

All replies

  • you can use retrieve method of crm webservic you can use JS code to fetch data

    refer

    http://msdn.microsoft.com/en-us/library/cc677076.aspx

    this is the sample code to fetch contact data you can change entity name and field accordingly.

    Please let us know if you need any other help on this


    Mahain : My Dynamics CRM Blog
    Thursday, December 16, 2010 4:27 PM
    Moderator
  • Go to Setting  then Customization then Customize entity and open either contact or Application entity. If you open contact entity and click on 1:N relationship, you will find a relationship where Application entity is related entity. Double click to open this relation and on LHS you will find mapping. One by one you will add all the fields which you want to transfer from contact to application. After doing so publish the entity and test. Please remember that mapping field should have same data type and length.


    Regards Faisal
    Thursday, December 16, 2010 6:31 PM
  • Mahender Pal--I think this is what I need to do, but I am not sure how to implement.  I am assuming that this code will go on the Applicant Contact field.  I need to be able to pull in any and all Contact data to the Application entity based on whichever person is chosen in the lookup.  So this could dynamically change if I realize I have picked the wrong person.  Can you give any more details on how to acheive this? 

    Thursday, December 16, 2010 8:44 PM
  • Hi,

    you can use below code on applicationcontact field's onchange, Please remember to change field names 

    if(crmForm.all.<LookupFieldName>.DataValue!=null)
    {
        var contactid = crmForm.all.<LookupFieldName>.DataValue[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>contact</entityName>" +
    "<id>" + contactid + "</id>" +
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
    "<q1:Attributes>" +
    "<q1:Attribute>fullname</q1:Attribute>" +   //add more field that you want to fetch
    "</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 {
       crmForm.all.<FieldName>.DataValue=resultXml.selectSingleNode("//q1:fullname").nodeTypedValue); //you can all field like this
        }
    }

     


    Mahain : My Dynamics CRM Blog
    Thursday, December 16, 2010 9:17 PM
    Moderator
  • Mahender Pal--How will these web services work with respect to radio button values, dropdown lists, etc.?  I take it that those values require some slightly different code.  Is it possible to fetch these different data types?  Thanks
    Monday, December 20, 2010 6:59 PM
  • yes you can fetch these values, you need to check attribute node in resultXML node, best way will be debug your code and get required syntax.


    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 7:53 PM
    Moderator
  • This was very useful BUT please note that you had an extra parentheses here at the end of this line which causes the entire thing to break:

    crmForm.all.<FieldName>.DataValue=resultXml.selectSingleNode("//q1:fullname").nodeTypedValue);
    

    There should not be a parentheses after 'nodeTypedValue'.  This is correct:

    crmForm.all.<FieldName>.DataValue=resultXml.selectSingleNode("//q1:fullname").nodeTypedValue;
    
    

     

    Thanks for all your help!  Beautiful solution!


    http://radiateeverything.blogspot.com/
    Thursday, March 17, 2011 8:14 PM