locked
CRM 2011 - Plugin for Preventing Record Association RRS feed

  • Question

  • Hi,

    I have a couple of entities A and B with 1:N relationship. I can add new/add existing record(s) of entity B from entity A. I want to prevent this record association when the status (custom status) of record of entity A is 'Closed'. How do I achieve this in CRM 2011 ? Can I use the Associate message and register the plugin on the primary entity A ?

     

    Please help.

     

    Monday, September 26, 2011 2:24 PM

Answers

  • No, you would have to register the plugin on the child entities (since the parent is not updated when you add a child). However, you can use the same plugin code, for all entities. In other words, you need the same plugin, but you must register multiple steps. I hope that helps you.

    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, September 26, 2011 6:14 PM
    Moderator

All replies

  • The AssociateEntities message does not support plugins. What you can do is to register a plugin on "update" of B, whenever the lookup to A is updated you do the validation. When you associate A and B, the lookup field on B will always get updated so your plugin can be triggered.

    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, September 26, 2011 2:27 PM
    Moderator
  • Just browsing through some forums and it looks like I can't register the Associate message on a specific entity ? Do I have to retrieve the record(s) like this ?

    // Get Primary Entity
    2	EntityReference target = (EntityReference)context.InputParameters["Target"];
    3	 
    4	if (target.LogicalName == "new_parententity" || target.LogicalName == "new_tutoringattendance")
    5	{
    6	    Relationship relationShip = (Relationship)context.InputParameters["Relationship"];
    7	 
    8	    // Get Related Entities
    9	    EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
    10	    foreach (EntityReference rel in relatedentities)
    11	    {
    12	        // Check Related Entity Logical & Schema name
    13	        if (rel.LogicalName == "contact" && relationShip.SchemaName == "new_new_schemaname_contact")
    14	        {
    15	            Entity parentEntity = service.Retrieve("new_parent", target.Id, new ColumnSet(true));
    16	            Entity relatedEntity = service.Retrieve("contact", rel.Id, new ColumnSet(true));
    17	 
    18	            // Do some work...
    19	        }
    20	    }
    21	}


    http://orwin.ca/2011/07/05/crm-2011-plugin-triggering-off-associate-message/

     

     


    Monday, September 26, 2011 2:34 PM
  • If your relationship is 1:N (instead of N:N) then you don't need to worry about the Associate message, you can achieve it with registering a plugin on "Update" of the child entity in the 1:N relationship. For example, let's say you want a plugin to be triggered each time a contact is associated to an account (parent customer field). Then you just need to register the plugin on update of conact, and select "parent account" in the list of filtering attributes in the step (in the Plugin Registration tool).

    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, September 26, 2011 2:40 PM
    Moderator
  • Thank you Gonzalo,

    I'll try that. I have multiple entities that have N:1 relationship with entity A, so I was wondering if I could do it in one shot by registering the plugin on the primary entity A.

    Monday, September 26, 2011 2:50 PM
  • No, you would have to register the plugin on the child entities (since the parent is not updated when you add a child). However, you can use the same plugin code, for all entities. In other words, you need the same plugin, but you must register multiple steps. I hope that helps you.

    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, September 26, 2011 6:14 PM
    Moderator