locked
Plugins -Get ParentCustomerId for a contact RRS feed

  • Question

  • Hi Experts,

    I want build a Plugin to retrive the parentcustomerid for  contact when save the contact record .

    Can you please write how I will do this?

    Friday, November 23, 2012 6:09 AM

Answers

  • hi bunty,

     Entity entity = (Entity)context.InputParameters["Target"];
    
                    // TODO Test for an entity type and message supported by your plug-in.
                    if (context.PrimaryEntityName != "contact") { return; }
                    if (context.MessageName != "Create") { return; }
    
    if (entity.Attributes.Contains("parentcustomerid"))
                        {
                            EntityReference parentCustomer= (EntityReference)entity.Attributes["parentcustomerid"];
    Guid parentCustomerGuid= parentCustomer.Id;
    }


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.



    • Edited by Ravitheja J Friday, November 23, 2012 6:21 AM
    • Proposed as answer by Ravitheja J Friday, November 23, 2012 6:21 AM
    • Marked as answer by Ashis_K Friday, November 23, 2012 6:41 AM
    Friday, November 23, 2012 6:19 AM
  • Hi CRMG,

    I am not getting what is Values ={_AccountID};

    I just passed the parentCustomerGuid to _AccountID but it is showing the count is -1;


    • Edited by Ashis_K Friday, November 23, 2012 7:05 AM
    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:03 AM
  • hi,

    _AccountID is  Guid of account record, of which you wanted to find the number of contacts associated with.

    QueryExpression _Query = new QueryExpression
                {
                    EntityName = "contact",
                    ColumnSet = new ColumnSet(true),
                    Criteria =
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName="parentcustomerid",
                                    Operator=ConditionOperator.Equal,
                                    Values={_AccountID}
    
                                }
                            }
                    }
    
                };
    
                EntityCollection _Entities = service.RetrieveMultiple(_Query);
    
                int count= _Entities.TotalRecordCount;

    here totalRecordCount will give you the count. check this and even msdn link.


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:17 AM
  • Thank you CRMIG,

    Finally I rectified my Error.

    just placed 

     int count = _Entities.Entities.Count; 

    It is working fine.

    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:41 AM

All replies

  • hi bunty,

     Entity entity = (Entity)context.InputParameters["Target"];
    
                    // TODO Test for an entity type and message supported by your plug-in.
                    if (context.PrimaryEntityName != "contact") { return; }
                    if (context.MessageName != "Create") { return; }
    
    if (entity.Attributes.Contains("parentcustomerid"))
                        {
                            EntityReference parentCustomer= (EntityReference)entity.Attributes["parentcustomerid"];
    Guid parentCustomerGuid= parentCustomer.Id;
    }


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.



    • Edited by Ravitheja J Friday, November 23, 2012 6:21 AM
    • Proposed as answer by Ravitheja J Friday, November 23, 2012 6:21 AM
    • Marked as answer by Ashis_K Friday, November 23, 2012 6:41 AM
    Friday, November 23, 2012 6:19 AM
  • Hi CRMIG,

    Thank you very much for reply.

    That code working fine.

    I have a next requirement I want to find no of contacts associated with an account using plugins

    How I will do this can you please suggests?


    • Edited by Ashis_K Friday, November 23, 2012 6:42 AM
    Friday, November 23, 2012 6:40 AM
  • hi bunty.k

    i replied to this question. 

    check this post. let me know, what problem you are having with my reply in this post.

    so that, i can suggest you the solution.


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, November 23, 2012 6:48 AM
  • Hi CRMG,

    I am not getting what is Values ={_AccountID};

    I just passed the parentCustomerGuid to _AccountID but it is showing the count is -1;


    • Edited by Ashis_K Friday, November 23, 2012 7:05 AM
    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:03 AM
  • hi,

    _AccountID is  Guid of account record, of which you wanted to find the number of contacts associated with.

    QueryExpression _Query = new QueryExpression
                {
                    EntityName = "contact",
                    ColumnSet = new ColumnSet(true),
                    Criteria =
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName="parentcustomerid",
                                    Operator=ConditionOperator.Equal,
                                    Values={_AccountID}
    
                                }
                            }
                    }
    
                };
    
                EntityCollection _Entities = service.RetrieveMultiple(_Query);
    
                int count= _Entities.TotalRecordCount;

    here totalRecordCount will give you the count. check this and even msdn link.


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:17 AM
  • Thank you CRMIG,

    Finally I rectified my Error.

    just placed 

     int count = _Entities.Entities.Count; 

    It is working fine.

    • Marked as answer by Ashis_K Friday, November 23, 2012 7:41 AM
    Friday, November 23, 2012 7:41 AM