locked
how to retrieve Data From Entity Using CRM 2011 sdk RRS feed

  • Question

  • I Have 2 entity with one to many relationship (Company & city)

    I need to create plugin for checking all names of the company
    so  when i create a new company in crm the plugin check if there are any company name is already exist in the same city

    if yes will display error message

    i create new MS Dynamics plug-in library then chose Company 

    Thank you

    Wednesday, June 6, 2012 1:04 PM

Answers

  • ok Thanks all for helping I made it and its working in this code

    protected void ExecutePostCompanyCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                Entity currententity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                EntityReference contactlookup = (EntityReference)currententity.Attributes["new_city_companiesid"];
                
                
                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_city_companiesid";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(contactlookup.Id);
    
                ConditionExpression condition2 = new ConditionExpression();
                condition2.AttributeName = "new_name";
                condition2.Operator = ConditionOperator.Equal;
                condition2.Values.Add(currententity.Attributes["new_name"].ToString());
                
                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(condition);
                filter.Conditions.Add(condition2);
                filter.FilterOperator = LogicalOperator.And;
    
                QueryExpression queryExpression = new QueryExpression("new_company");
                queryExpression.ColumnSet.AddColumn("new_name");
                queryExpression.Criteria.AddFilter(filter);
                EntityCollection resultCollection = localContext.OrganizationService.RetrieveMultiple(queryExpression);
    
              
                if (resultCollection.Entities.Count > 1)
                {
    
                    throw new InvalidPluginExecutionException("Already Exist");
    
                }
    
            }

    • Marked as answer by MIDOZ Thursday, June 7, 2012 1:11 PM
    Thursday, June 7, 2012 1:11 PM

All replies

  • Hi,

    you can write a retrievemultiple function in your pre create plugin on company, where you need to query city entity based on company name, and if you will get some result, then you can throw a InvalidPluginExecutionException to stop user from creating company.http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.messages.retrievemultiplerequest.aspx


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, June 6, 2012 1:11 PM
    Moderator
  • Hi Mahender Pal

    thank you for replay but could you please explain it to me by code because i am still beginner in this field

    Wednesday, June 6, 2012 1:29 PM
  • Hi,

    Could you please share entity names and field details so that we can help you with code.


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, June 6, 2012 3:19 PM
    Moderator
  • ConditionExpression condition = new ConditionExpression(); condition.AttributeName = "sno"; condition.Operator = ConditionOperator.Equal; condition.Values.Add(1); FilterExpression filter = new FilterExpression(); filter.Conditions.Add(condition); QueryExpression queryExpression = new QueryExpression("entityName"); queryExpression.ColumnSet.AddColumn("FieldName1"); queryExpression.ColumnSet.AddColumn("FieldName1"); queryExpression.Criteria.AddFilter(filter); EntityCollection resultCollection = service.RetrieveMultiple(queryExpression); foreach (var e in resultCollection.Entities) { if (e.Attributes.Contains("FieldName1")) { valuevar= e.Attributes["FieldName1"].ToString(); } }

    Hello MIDOZ:

    Use the code example I had given. It is explaining what you want in simplest way.

    For further help, just let me know.

    Enjoy..:)


    Saurabh Gupta, MS CRM 2011 Software Development Engineer

    Wednesday, June 6, 2012 3:38 PM
  • Did you explore the OOB Duplicate detection rule to see if that helps you set up the rule.

    Sam 


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, June 6, 2012 3:45 PM
  • Hi Mahender Pal

    entity name is Company

    Fields are

    1- new_city_companiesid :- is a lockup field that contains cities
    2- new_name:- is the company name

    thank you


    • Edited by MIDOZ Thursday, June 7, 2012 10:01 AM
    Thursday, June 7, 2012 9:51 AM
  • hi SaurabhGupta_2160

    Thank you for your coding i made this code

    protected void ExecutePostCompanyCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                Entity currententity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                EntityReference contactlookup = (EntityReference)currententity.Attributes["new_city_companiesid"];

                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_city_companiesid";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(contactlookup.ToString());


                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(condition);

                QueryExpression queryExpression = new QueryExpression("Company");
                queryExpression.ColumnSet.AddColumn("new_name");
                queryExpression.Criteria.AddFilter(filter);
                EntityCollection resultCollection = localContext.OrganizationService.RetrieveMultiple(queryExpression);
                foreach (var e in resultCollection.Entities)
                {

                    if (e.Attributes.Contains("new_name"))
                    {
                        throw new InvalidPluginExecutionException(e.Attributes["new_name"].ToString());
                        
                    }
                }
            }

    in the comparison i need to retrieve all the companies name in the same city (that inserted)and compare it with the inserted company name

    fields are

    new_city_companiesid :- is a lockup field that contains cities
    new_name:- is the company name

    thank you


    • Edited by MIDOZ Thursday, June 7, 2012 11:18 AM
    Thursday, June 7, 2012 10:00 AM
  • ok Thanks all for helping I made it and its working in this code

    protected void ExecutePostCompanyCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                Entity currententity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                EntityReference contactlookup = (EntityReference)currententity.Attributes["new_city_companiesid"];
                
                
                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "new_city_companiesid";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(contactlookup.Id);
    
                ConditionExpression condition2 = new ConditionExpression();
                condition2.AttributeName = "new_name";
                condition2.Operator = ConditionOperator.Equal;
                condition2.Values.Add(currententity.Attributes["new_name"].ToString());
                
                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(condition);
                filter.Conditions.Add(condition2);
                filter.FilterOperator = LogicalOperator.And;
    
                QueryExpression queryExpression = new QueryExpression("new_company");
                queryExpression.ColumnSet.AddColumn("new_name");
                queryExpression.Criteria.AddFilter(filter);
                EntityCollection resultCollection = localContext.OrganizationService.RetrieveMultiple(queryExpression);
    
              
                if (resultCollection.Entities.Count > 1)
                {
    
                    throw new InvalidPluginExecutionException("Already Exist");
    
                }
    
            }

    • Marked as answer by MIDOZ Thursday, June 7, 2012 1:11 PM
    Thursday, June 7, 2012 1:11 PM