locked
CRM 2011 Entity to entity values passing RRS feed

  • Question

  • Hi

    I have entity called as Address : in this consists of User Policy No address1, address2,city, contact information etc…

     

     I have one more entity called as Policy : while giving the policy Number..have to dynamically fill the address entity information (address1, address2,city etc..) in policy page..

     

     How can we achieve this in CRM 2011?


    Rammohan

    Tuesday, April 10, 2012 9:58 AM

Answers

  • I understand your problem like this way,

    once you select policyNumber policy form has to be filled,including of city name.

    am i correct?

    if yes im done,can you please check and confirm,

    i modified your code


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    • Edited by san Sanz Tuesday, April 17, 2012 7:42 AM
    • Proposed as answer by san Sanz Tuesday, April 17, 2012 7:43 AM
    • Marked as answer by Rammohan Ammiti Tuesday, April 17, 2012 7:50 AM
    Tuesday, April 17, 2012 7:41 AM

All replies

  • What is the relationship between Adresses and Policies?

    Regards Faisal

    Tuesday, April 10, 2012 10:08 AM
  • Adress entity

    PolicyHolder Name and Number will be there.

    In policy entity while enter the Policy Number remaining data have to populate adutomtically..


    Rammohan

    Tuesday, April 10, 2012 10:10 AM
  • Hi Rammohan,

    you can retrieve data from address entity based on policy number.

    you can refer http://mahenderpal.wordpress.com/2012/01/24/setting-default-unit-in-product-form-using-javascript/

    you need to modify it with your entity name and attribute name.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by nmathur Monday, April 16, 2012 6:52 AM
    Tuesday, April 10, 2012 10:37 AM
    Moderator
  • Hi Mahendrer,

     Thanks for your reply...

    I have seen the  ur blog...

    can you pls explain where i have to call this three functions etc.. am bit new for this crm

    function SetDefaultUnit()

    {

    if(Xrm.Page.ui.getFormType() == 1) //Check form type

    {

    var number = 1;
    var filter = “/UoMScheduleSet?$select=Name,UoMScheduleId&$filter=Name eq ‘Default Unit’”; //Make sure you have created ‘Default Unit’ record
    RetrieveUnit(filter);
    }

    }
    function GetODataPath() {

    return Xrm.Page.context.getServerUrl() + “/xrmservices/2011/organizationdata.svc”;
    }
    function RetrieveUnit(filter) {

    var retrieveRecordsReq = new XMLHttpRequest();
    retrieveRecordsReq.open(“GET”, GetODataPath() + filter, false);
    retrieveRecordsReq.setRequestHeader(“Accept”, “application/json”);
    retrieveRecordsReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8″);
    var temp= retrieveRecordsReq.send();

    //check status
    if (retrieveRecordsReq.readyState == 4 && retrieveRecordsReq.status == 200) {
    var retrievedRecords = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
    var Result = retrievedRecords.results[0];

    //Set lookup
    var lookup = new Object();
    var lookupValue = new Array();
    lookup.id = Result.UoMScheduleId;
    lookup.entityType = “uomschedule”;
    lookup.name = Result.Name;
    lookupValue[0] = lookup;
    Xrm.Page.getAttribute(“defaultuomscheduleid”).setValue(lookupValue);
    }
    }


    Rammohan

    Tuesday, April 10, 2012 11:17 AM
  • On change of Policy Number in Policy Entity write your Logic(using web Resource).

    First of all get the Policy Number.

    After that retrieve the Address record using this Policy number.

    And use below code to get desire record using .

    how to call Function--->
    ---------------------------
            var AddressRec = PerformRetrieveMultiple("Address", new Array("address1","address2",etc), new Array("policynumber=" + policyno));
            

    function PerformRetrieveMultiple(entityName, columns, constraints) {
        var authenticationHeader = GenerateAuthenticationHeader();
        var query = "<?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>" +
    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
    " xsi:type='q1:QueryExpression'>" +
    "<q1:EntityName>" + entityName + "</q1:EntityName>";

        query += "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">";
        query += " <q1:Attributes>";

        for (var i = 0; i < columns.length; i++) {
            query += "<q1:Attribute>" + columns[i] + "</q1:Attribute>";
        }

        query += " </q1:Attributes>";
        query += "</q1:ColumnSet>";

        query += "<q1:Distinct>false</q1:Distinct>";

        query += "<q1:Criteria>";
        query += "<q1:FilterOperator>And</q1:FilterOperator>";
        query += "<q1:Conditions>";
        for (var i = 0; i < constraints.length; i++) {
            var splitResult = constraints[i].split("=");
            if (splitResult.length > 1) {
                query += "<q1:Condition>";
                query += "<q1:AttributeName>" + splitResult[0] + "</q1:AttributeName>";
                query += "<q1:Operator>Like</q1:Operator>";
                query += "<q1:Values>";
                query += "<q1:Value xsi:type=\"xsd:string\">" + splitResult[1] + "</q1:Value>";
                query += "</q1:Values>";
                query += "</q1:Condition>";
            }
        }
        query += "</q1:Conditions>";
        query += "</q1:Criteria>";

        query += "</query>" +
        "</RetrieveMultiple>" +
        "</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/RetrieveMultiple");
        xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xHReq.setRequestHeader("Content-Length", query.length);
        xHReq.send(query);
        var resultXml = xHReq.responseXML;
        var results = resultXml.getElementsByTagName('BusinessEntity');



        var MultiArray = new Array();


        for (j = 0; j < results.length; j++) {
            var ar = new Array();
            for (var i = 0; i < columns.length; i++) {
                var columnName = columns[i];
                ar[columnName] = results[j].selectSingleNode('./q1:' + columns[i])
            }
            MultiArray[j] = ar;
        }
        
        return MultiArray;
    }

    now you will get all the fields in an array which you need from Address Entity.

    and get this value and fill all values in Policy Record.

    Take a try on this. (I am assuming this policy number is not a lookup field. it is single line of text field).

    if the response answered your question, please take a minute and mark the response as an answer.

    • Proposed as answer by nmathur Monday, April 16, 2012 6:49 AM
    • Unproposed as answer by Rammohan Ammiti Monday, April 16, 2012 6:55 AM
    Tuesday, April 10, 2012 12:55 PM
  • Hi Manuj,

     I have tried with you code am not getting exactly

    can you pls see my crm link

    Link : https://ammitir.crm.dynamics.com/main.aspx

      Username: nanna_rammohan_hyd@hotmail.com

     Password: Computers123@

    Enity Names:1. Policy entity

                          2.Adress

     I have  created the web resouce file Name : Parameterpassing

    Adress field Policy Number is textfield  , adressline 1 ,adressline 2 , city will be thre..

    In policy page if i select the policy number i have fill the above details.. so pls help me out

        


    Rammohan


    • Edited by Rammohan Ammiti Wednesday, April 11, 2012 6:47 AM data not added completely
    Wednesday, April 11, 2012 6:46 AM
  • Hi Rammohan,

    Ok.Let me take a try on this.

    Wednesday, April 11, 2012 7:11 AM
  •  you can  use plugin...


    sand

    Wednesday, April 11, 2012 7:23 AM
  • Hi RamMohan,

    Take a look into your org.I think now it is working fine.

    I have added mine code and commented your code.

    call PerformCalculation function on the change event of PolicyNumber.

    Below is the code which I added.

    function PerformCalculation() {
        var PolicyNumber = Xrm.Page.getAttribute("new_policynumber").getValue();
        if (PolicyNumber != null) {
            var PolicyNumberId = PolicyNumber[0].id;
            var Fields = PerformRetrieve("new_adress", PolicyNumberId)
            if (Fields != null) {
                var Address1 = Fields.selectSingleNode("//q1:" + 'new_adressline1').nodeTypedValue;
                var Address2 = Fields.selectSingleNode("//q1:" + 'new_adressline2').nodeTypedValue;
            }
        }

    }

    function PerformRetrieve(entityName, EntityGuid) {
        var res = null;
        var authenticationHeader = 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'>" +
    authenticationHeader +
    "<soap:Body>" +
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    "<entityName>" + entityName + "</entityName>" +
    "<id>" + EntityGuid + "</id>" +
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
    "<q1:Attributes>" +
    "<q1:Attribute>" + "new_adressline1" + "</q1:Attribute>" +
    "<q1:Attribute>" + "new_adressline2" + "</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 {
            //res = resultXml.selectSingleNode("//q1:" + columns).nodeTypedValue;
            res = resultXml;
        }

        return res;
    }




    // var AddressRec = PerformRetrieveMultiple("new_adress", new Array("new_adressline1","new_adressline2","new_city"

    // new Array("new_policy=" + policyno));         

    //function PerformRetrieveMultiple(entityName, columns, constraints) {
    //    var authenticationHeader = GenerateAuthenticationHeader();
    //    var query = "<?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>" +
    //"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    //"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
    //" xsi:type='q1:QueryExpression'>" +
    //"<q1:EntityName>" + entityName + "</q1:EntityName>";

    //    query += "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">";
    //    query += " <q1:Attributes>";

    //    for (var i = 0; i < columns.length; i++) {
    //        query += "<q1:Attribute>" + columns[i] + "</q1:Attribute>";
    //    }

    //    query += " </q1:Attributes>";
    //    query += "</q1:ColumnSet>";

    //    query += "<q1:Distinct>false</q1:Distinct>";

    //    query += "<q1:Criteria>";
    //    query += "<q1:FilterOperator>And</q1:FilterOperator>";
    //    query += "<q1:Conditions>";
    //    for (var i = 0; i < constraints.length; i++) {
    //        var splitResult = constraints[i].split("=");
    //        if (splitResult.length > 1) {
    //            query += "<q1:Condition>";
    //            query += "<q1:AttributeName>" + splitResult[0] + "</q1:AttributeName>";
    //            query += "<q1:Operator>Like</q1:Operator>";
    //            query += "<q1:Values>";
    //            query += "<q1:Value xsi:type=\"xsd:string\">" + splitResult[1] + "</q1:Value>";
    //            query += "</q1:Values>";
    //            query += "</q1:Condition>";
    //        }
    //    }
    //    query += "</q1:Conditions>";
    //    query += "</q1:Criteria>";

    //    query += "</query>" +
    //    "</RetrieveMultiple>" +
    //    "</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/RetrieveMultiple");
    //    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    //    xHReq.setRequestHeader("Content-Length", query.length);
    //    xHReq.send(query);
    //    var resultXml = xHReq.responseXML;
    //    var results = resultXml.getElementsByTagName('BusinessEntity');



    //    var MultiArray = new Array();


    //    for (j = 0; j < results.length; j++) {
    //        var ar = new Array();
    //        for (var i = 0; i < columns.length; i++) {
    //            var columnName = columns[i];
    //            ar[columnName] = results[j].selectSingleNode('./q1:' + columns[i])
    //        }
    //        MultiArray[j] = ar;
    //    }
    //    
    //    return MultiArray;
    //} 

    Now it is able to give you address1 and address2 value from address entity.

    if the response answered your question, please take a minute and mark the response as an answer.

    Wednesday, April 11, 2012 8:05 AM
  • Hi Rammohan,

    are you able to get this?

    If it is working please mark this post as answered.

    Friday, April 13, 2012 8:17 AM
  • Hi Manuj,

    Thanks for your suggestions

      I have called the function   PerformCalculation() in PoilicyNumber lookup @policyentity and Page load as well but values are not getting.. can you please check it once..

    -Regards 


    Rammohan

    Monday, April 16, 2012 5:46 AM
  • Hi Manuj,

    Can you pls check the following organization ..

    Link : https://ammitir.crm.dynamics.com/main.aspx

      Username: nanna_rammohan_hyd@hotmail.com

     Password: Computers123@

    Enity Names:1. Policy entity

                          2.Adress

    I'm not getting the values.. i have debugged all values are getting undefined...

    values are not loaded( i have traced through f12 with stat debugger)..


    Rammohan

    Monday, April 16, 2012 6:58 AM
  • Hello Rammohan,

    If you don't mind can I check your code?


    -- NMathur

    Monday, April 16, 2012 7:01 AM
  • yes you can check my souce code...

    Rammohan

    Monday, April 16, 2012 7:06 AM
  • ok, I am checking it.


    -- NMathur

    Monday, April 16, 2012 7:09 AM
  • Hello Rammohan,

    Your code is working fine now. Very important part was missing from your code. In your code field values were fetched successfully but they were not set to fileds of policy entity:

    Xrm.Page.getAttribute("new_adressline1").setValue(Address1);
    Xrm.Page.getAttribute("new_adressline2").setValue(Address2);

    Please check your code, and now you can also write code to set value for city field (optionsetvalue). Please feel free to ask for any query.


    -- NMathur


    • Marked as answer by Rammohan Ammiti Monday, April 16, 2012 7:59 AM
    • Edited by nmathur Monday, April 16, 2012 8:50 AM
    • Unmarked as answer by Rammohan Ammiti Tuesday, April 17, 2012 6:27 AM
    Monday, April 16, 2012 7:44 AM
  • Hi  NMathur,

     Thanks for your help.. for text box values is passing  working nicely..

    while passing the optionset text it's not getting

    Xrm.Page.getAttribute("new_citys").setValue(city);

    values are getting 1,2,3 but i want Text like India, US, UK etc..

    can you pls suggest how can i bring that one here..


    Rammohan

    Tuesday, April 17, 2012 6:29 AM
  • Hey Ram,

    check Your Earlier Post


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, April 17, 2012 6:53 AM
  • Hi sanz,

     i'm not understand what you have mentioned 

    function test()
    {
    var att=document.getElementById("customertypecode");
    for(i=0;i<att.options.length;i++)
    {
    if(att.options[i].innerText=="Competitor")
    {
    att
    .options[i].innerText="sandeep"
    }
    }
    }

     I think you are not understand my requirement.

    Let me explain again.. I have address is an entity and policy is aother entity..

    wht ever information policyid related address have to pass the policy entity.

     I am able to set and pass the values related to Text it's fine.

    Now i have picklist.. picklist names are not comming only values are comming for getting name Text how to do it?

    any specific method is available.. for ur mentioned method how can i write another entity... check above code and see the scenario where i have required exactly..


    Rammohan

    Tuesday, April 17, 2012 7:01 AM
  • ok Rammohan, I am again accessing your account (hope password is not changed yet). Will be back in 10 mins.

    -- NMathur

    Tuesday, April 17, 2012 7:21 AM
  • hey ram can you come to live messenger i send an invitaion already

    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, April 17, 2012 7:25 AM
  • Hi,

    Can i take a look into this..?

    If nobody is working on that org..

    Tuesday, April 17, 2012 7:38 AM
  • I understand your problem like this way,

    once you select policyNumber policy form has to be filled,including of city name.

    am i correct?

    if yes im done,can you please check and confirm,

    i modified your code


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    • Edited by san Sanz Tuesday, April 17, 2012 7:42 AM
    • Proposed as answer by san Sanz Tuesday, April 17, 2012 7:43 AM
    • Marked as answer by Rammohan Ammiti Tuesday, April 17, 2012 7:50 AM
    Tuesday, April 17, 2012 7:41 AM
  • hi Manuj,

    yes you can access it.

    Actually we have to get value for name attribute for City from result xml. But as I am also new to CRM development I don't know how to do that and searching on google for help.

    This is the soap:

    <q1:new_city name="India">1</q1:new_city>

    Thanks.


    -- NMathur

    Tuesday, April 17, 2012 7:42 AM
  • Hi Mathur,

    This is what you need.

    	var city = Fields.selectSingleNode("//q1:" + 'new_city');
                var cityText=city.getAttribute("name");
    var cityValue=city .nodeTypedValue;



    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, April 17, 2012 7:46 AM
  • let me check it..

    i will update you on this...


    • Edited by Manuj Gupta Tuesday, April 17, 2012 7:57 AM
    Tuesday, April 17, 2012 7:47 AM
  • Hi All,

     Thanks for your help it's working fine now...

    i will utilize my orginal organization now..

    once again thanks for ur help guys..

    Note: sam sanz , I am not able to come Live communicator.. coz office hours can't able to open (blocked)


    Rammohan

    Tuesday, April 17, 2012 7:53 AM
  • Manuj,

    You no need to retrieve metadata dude,

    you can use this code

    var options= Xrm.Page.getAttribute("new_city").getOptions()
    for(var i=0;i<options.length;i++)
    {
    if(options[i].text==cityText)
    {
    Xrm.Page.getAttribute("new_city").setValue(options[i].value);
    }
    }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by Manuj Gupta Tuesday, April 17, 2012 9:09 AM
    • Unproposed as answer by Rammohan Ammiti Thursday, July 26, 2012 7:32 AM
    Tuesday, April 17, 2012 7:54 AM
  • is there anybody working..?
    Tuesday, April 17, 2012 8:01 AM
  • Hi Ram,

    Now you can check your org. i think now it is working perfectly.

    if the response answered your question, please take a minute and mark the response as an answer.

    Tuesday, April 17, 2012 8:03 AM
  • Hi Sanz and Manuj,

    Thanks for your help.

    And thanks Ram, because of you I learned something new.


    -- NMathur

    Tuesday, April 17, 2012 8:06 AM
  • Yes. I got it. Thats y I removed that reply. :)

    Thanks a lot Sandeep...


    • Edited by Manuj Gupta Tuesday, April 17, 2012 9:09 AM
    Tuesday, April 17, 2012 9:08 AM
  • Hi

      i have small issue now.. i'm trying to pass Lookup values at that time it's not getting ..

     can you please suggest it how can i do this..

    -Regards,

     Ammiti.


    Rammohan

    Thursday, July 26, 2012 7:33 AM
  • hi can you please post some code,

    exactly where the problem occurs?


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Thursday, July 26, 2012 9:31 AM
  • Hi sanz,

         Code is working fine for passing  Text box value to TextBox and dropdown value to TextBox.

     But it's not working Lookup to Lookup.

    I tried above code it's getting GUID value.. but i want Name of the lookup.. here i have to change code like

    function PerformCalculation() {
        var PolicyNumber = Xrm.Page.getAttribute("new_policynumber").getValue();
        if (PolicyNumber != null) {
            var PolicyNumberId = PolicyNumber[0].id;
            var Fields = PerformRetrieve("new_adress", PolicyNumberId)
            if (Fields != null) {
                var Address1 = Fields.selectSingleNode("//q1:" + 'new_adressline1').nodeTypedValue;
                var Address2 = Fields.selectSingleNode("//q1:" + 'new_adressline2').nodeTypedValue;
            }
        }

    }


    Rammohan

    Thursday, July 26, 2012 10:56 AM