locked
N:N Manual Recursive Relationship New Record. RRS feed

  • Question

  • Hello.

    I have created a manual N:N recursive relationship on an entity. I've added the subgrids of both sides of the relationship on the entity's form, one for the "Parent" records (which are my parents), and another for the "Children" records (which are my children).

    Can anyone tell me, how can I identify from which subgrid am I creating a new record of the manual N:N relationship?, that is, if I create a new parent, I need to disable the child lookup and clear the parent lookup so that the user can select the corresponding parent, or if I create a new child, I need to disable the Parent lookup and clear the child lookup so the user can select the child.

    I hope I'm clear enough.


    Monday, July 29, 2013 2:51 PM

Answers

  • Hmm this is a good question. I'm often faced with this sort of problem, but I've never had to do it with a manual N:N self-referential relationship.

    I don't believe there is any way to make the out-of-the-box sub-grid add existing buttons do what you want, as you may have noticed the URL's are exactly the same whether you create the relationship from the parent or child grid.

    With that being said, there are some other ways you could do this.

    My preferred option would be to add 2 ribbon buttons to your main form: 'Add Parent' and 'Add Child'. On click of these buttons you would want to pop open a new form for your intercept entity, and pass in the ID from your main form into the parent or child lookup depending on which button was clicked. If you're using rollup 8 or later, you can do something like:

    function addChild() { 
        var parameters = {};
        //Set the Parent field value to the current record.
        parameters["new_parentid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["new_parentidname"] = Xrm.Page.getAttribute("new_name").getValue(); //the name field of primary entity
        parameters["new_parentidtype"] = "new_primaryentityname"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_intercept", null, parameters);
    }

    You'd probably also want to hide the 'Add New' button from your intercept entity if you do this to prevent users from accidently adding them the wrong way.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism





    Tuesday, July 30, 2013 7:44 AM
  • I think it has something to do with the fact that it's a custom lookup field, as I'm having the same problem when trying this.

    There is a workaround described here: http://www.crmnerd.com/customizations/using-xrm.utility.openentityform-with-custom-parameters/

    Which basically involves passing custom params to the form and then extracting them manually on your intercept entity.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Tuesday, July 30, 2013 10:57 PM
  • Hello.

    I've done it!, the solution is as described in:

    Using Xrm.Utility.openEntityForm with Custom Parameters

    using a parameter set for the parent and child, and on the relationship entity form, on the onload event, validating which parameter set is passed by each button, and disabling the corresponding control:

    function AddChild() {
        var parameters = {};
    
        //Set the Parent field value to the current record.
        parameters["parameter_parentid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["parameter_parentname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
        parameters["parameter_parenttype"] = "territory"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
    }
    
    function AddParent() {
        var parameters = {};
    
        //Set the Parent field value to the current record.
        parameters["parameter_childid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["parameter_childname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
        parameters["parameter_childtype"] = "territory"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
    }

    on the onload event of the relationsip entity form:

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == CRMFORMTYPE_CREATE) {
            var param = Xrm.Page.context.getQueryStringParameters();
            if (param["parameter_parentid"] != null) {
                var parentId = param["parameter_parentid"];
                var parentName = param["parameter_parentname"];
                var parentType = param["parameter_parenttype"];
                Xrm.Page.getAttribute("new_zoneid").setValue([{ id: parentId, name: parentName, entityType: parentType}]);
                Xrm.Page.getControl("new_zoneid").setDisabled(true);
    
            }
            if (param["parameter_childid"] != null) {
                var childId = param["parameter_childid"];
                var childName = param["parameter_childname"];
                var childType = param["parameter_childtype"];
                Xrm.Page.getAttribute("new_subzoneid").setValue([{ id: childId, name: childName, entityType: childType}]);
    
                Xrm.Page.getControl("new_subzoneid").setDisabled(true);
            }
        }
    }

    I hope this solution helps colleagues in the future.


    Tuesday, July 30, 2013 11:00 PM

All replies

  • Hmm this is a good question. I'm often faced with this sort of problem, but I've never had to do it with a manual N:N self-referential relationship.

    I don't believe there is any way to make the out-of-the-box sub-grid add existing buttons do what you want, as you may have noticed the URL's are exactly the same whether you create the relationship from the parent or child grid.

    With that being said, there are some other ways you could do this.

    My preferred option would be to add 2 ribbon buttons to your main form: 'Add Parent' and 'Add Child'. On click of these buttons you would want to pop open a new form for your intercept entity, and pass in the ID from your main form into the parent or child lookup depending on which button was clicked. If you're using rollup 8 or later, you can do something like:

    function addChild() { 
        var parameters = {};
        //Set the Parent field value to the current record.
        parameters["new_parentid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["new_parentidname"] = Xrm.Page.getAttribute("new_name").getValue(); //the name field of primary entity
        parameters["new_parentidtype"] = "new_primaryentityname"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_intercept", null, parameters);
    }

    You'd probably also want to hide the 'Add New' button from your intercept entity if you do this to prevent users from accidently adding them the wrong way.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism





    Tuesday, July 30, 2013 7:44 AM
  • Great idea, I'll pass it on to the client for their consideration.

    Thank you very much!

    Tuesday, July 30, 2013 2:16 PM
  • I think it has something to do with the fact that it's a custom lookup field, as I'm having the same problem when trying this.

    There is a workaround described here: http://www.crmnerd.com/customizations/using-xrm.utility.openentityform-with-custom-parameters/

    Which basically involves passing custom params to the form and then extracting them manually on your intercept entity.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Tuesday, July 30, 2013 10:57 PM
  • Hello.

    I've done it!, the solution is as described in:

    Using Xrm.Utility.openEntityForm with Custom Parameters

    using a parameter set for the parent and child, and on the relationship entity form, on the onload event, validating which parameter set is passed by each button, and disabling the corresponding control:

    function AddChild() {
        var parameters = {};
    
        //Set the Parent field value to the current record.
        parameters["parameter_parentid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["parameter_parentname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
        parameters["parameter_parenttype"] = "territory"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
    }
    
    function AddParent() {
        var parameters = {};
    
        //Set the Parent field value to the current record.
        parameters["parameter_childid"] = Xrm.Page.data.entity.getId(); //id of primary entity
        parameters["parameter_childname"] = Xrm.Page.getAttribute("name").getValue(); //the name field of primary entity
        parameters["parameter_childtype"] = "territory"; //the primary entity schema name
    
        // Open the window.
        Xrm.Utility.openEntityForm("new_zonesubzones", null, parameters);
    }

    on the onload event of the relationsip entity form:

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == CRMFORMTYPE_CREATE) {
            var param = Xrm.Page.context.getQueryStringParameters();
            if (param["parameter_parentid"] != null) {
                var parentId = param["parameter_parentid"];
                var parentName = param["parameter_parentname"];
                var parentType = param["parameter_parenttype"];
                Xrm.Page.getAttribute("new_zoneid").setValue([{ id: parentId, name: parentName, entityType: parentType}]);
                Xrm.Page.getControl("new_zoneid").setDisabled(true);
    
            }
            if (param["parameter_childid"] != null) {
                var childId = param["parameter_childid"];
                var childName = param["parameter_childname"];
                var childType = param["parameter_childtype"];
                Xrm.Page.getAttribute("new_subzoneid").setValue([{ id: childId, name: childName, entityType: childType}]);
    
                Xrm.Page.getControl("new_subzoneid").setDisabled(true);
            }
        }
    }

    I hope this solution helps colleagues in the future.


    Tuesday, July 30, 2013 11:00 PM
  • I was answering the question when your replied :), thank you very much!!!
    Tuesday, July 30, 2013 11:03 PM