locked
CRM plugin Retrive-RetriveMulitple Message for Account-Contact Entity RRS feed

  • Question

  • I have a requirement to count number of active contacts associated with Account and display that number into custom field into account. we also wants to display that count on Main Account list.

    I am planning to create plugin on contact create/update/delete/Merge message and update that custom field on parent account.

    Does Contact Retrive/RetriveMultiple message   or   Account Retrive/RetriveMultiple message is option here??

    Please assist. 

    Monday, July 14, 2014 1:57 PM

Answers

  • Hi,

    You can write a plugin to fulfill your requirement and that should be triggered on Contact Create and Update Post actions.

    In the plugin, from the AccountId of contact you can get the number of contacts associated with it and update the same in the account.

     QueryExpression queryExpression = new QueryExpression();
                queryExpression.EntityName = "contact";
                queryExpression.ColumnSet = new ColumnSet("contactid");
                queryExpression.Criteria.Conditions.Add(new ConditionExpression("accountid", ConditionOperator.Equal, new Guid("<AccountGUID>")));
                EntityCollection objContacts = Service.RetrieveMultiple(queryExpression);
                if(objContacts != null && objContacts.Entities.Count > 0)
                {
                    // Update the Account with objContacts.Entities.Count
                }
               

    --

    Thanks

    Gopinath

    http://mscrmtechie.blogspot.com

    • Marked as answer by Nicksoft2011 Monday, July 14, 2014 2:23 PM
    • Unmarked as answer by Nicksoft2011 Monday, July 14, 2014 2:23 PM
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 2:05 PM
  • Does Contact Retrive/RetriveMultiple message   or   Account Retrive/RetriveMultiple message is option here??
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 2:10 PM
  • No... You could follow the steps from my previous and achieve the requirement very easily..
    Monday, July 14, 2014 2:12 PM
  • Gopinath thanks for the update.
    Do I need to worry about Contact
    Merge/Assign/SetState/SetDynamnicState messages??? I mean contact
    create/update/delete is good enough to cover all scenario??

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 9:16 PM
  • Hi,

    You are most welcome.

    I think, you should also consider SetState and Delete Messages too. So that the count can be accurate.

    And one more thing, If you want to calculate Count of only active contacts then you should add one more Condition Expression to get the Active contacts.

    --

    Regards,

    Gopinath.

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 5:29 AM
  • Thanks Gopinath again for assistance here. Well, i am covering/handeling the Statecode and Assign both scenario into Update plugin. So i guess i should not create separate plugin for setstate/setstatedynamic/assign, am i correct??

    I mean in this situation, even we activate/deactivate/assign the contact, it fires update plugin, so why don't i just use it instead of creating a separate plugin??

    can you please assist whether it is bad practice or doesn't matter??
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 2:13 PM
  • I am glad it helped you...

    Yes, you can go ahead and use a single plugin to fulfill all the scenarios. And even it will make maintenance easy.

    --

    Regards,

    Gopinath.

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 4:20 PM

All replies

  • Hi,

    You can write a plugin to fulfill your requirement and that should be triggered on Contact Create and Update Post actions.

    In the plugin, from the AccountId of contact you can get the number of contacts associated with it and update the same in the account.

     QueryExpression queryExpression = new QueryExpression();
                queryExpression.EntityName = "contact";
                queryExpression.ColumnSet = new ColumnSet("contactid");
                queryExpression.Criteria.Conditions.Add(new ConditionExpression("accountid", ConditionOperator.Equal, new Guid("<AccountGUID>")));
                EntityCollection objContacts = Service.RetrieveMultiple(queryExpression);
                if(objContacts != null && objContacts.Entities.Count > 0)
                {
                    // Update the Account with objContacts.Entities.Count
                }
               

    --

    Thanks

    Gopinath

    http://mscrmtechie.blogspot.com

    • Marked as answer by Nicksoft2011 Monday, July 14, 2014 2:23 PM
    • Unmarked as answer by Nicksoft2011 Monday, July 14, 2014 2:23 PM
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 2:05 PM
  • Does Contact Retrive/RetriveMultiple message   or   Account Retrive/RetriveMultiple message is option here??
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 2:10 PM
  • No... You could follow the steps from my previous and achieve the requirement very easily..
    Monday, July 14, 2014 2:12 PM
  • Gopinath thanks for the update.
    Do I need to worry about Contact
    Merge/Assign/SetState/SetDynamnicState messages??? I mean contact
    create/update/delete is good enough to cover all scenario??

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Monday, July 14, 2014 9:16 PM
  • Hi,

    You are most welcome.

    I think, you should also consider SetState and Delete Messages too. So that the count can be accurate.

    And one more thing, If you want to calculate Count of only active contacts then you should add one more Condition Expression to get the Active contacts.

    --

    Regards,

    Gopinath.

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 5:29 AM
  • Thanks Gopinath again for assistance here. Well, i am covering/handeling the Statecode and Assign both scenario into Update plugin. So i guess i should not create separate plugin for setstate/setstatedynamic/assign, am i correct??

    I mean in this situation, even we activate/deactivate/assign the contact, it fires update plugin, so why don't i just use it instead of creating a separate plugin??

    can you please assist whether it is bad practice or doesn't matter??
    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 2:13 PM
  • I am glad it helped you...

    Yes, you can go ahead and use a single plugin to fulfill all the scenarios. And even it will make maintenance easy.

    --

    Regards,

    Gopinath.

    • Marked as answer by Nicksoft2011 Tuesday, July 15, 2014 5:51 PM
    Tuesday, July 15, 2014 4:20 PM