简体   繁体   中英

CRM 2011 :LINQ query not getting executed

i implemented my first LINQ query to check for duplicate records while the user adds a new record but it is not getting fired

I am working on CRM2011 and i wrote the plugin using LINQ and registered it with Plugin Registration Tool

Below is my code

 if (context.Depth == 1)
        {
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                target =(Entity)context.InputParameters["Target"];
                if (target != null)
                {
                    householdname = target.GetAttributeValue<string>("mcg_HouseholdName");
                }
            }
            OrganizationServiceContext orgcontext = new OrganizationServiceContext(service);
            {
                var query = (from c in orgcontext.CreateQuery<mcg_household>()
                             where c.mcg_HouseholdName == householdname
                             select c
                            );
                List<mcg_household> householdlist = query.ToList<mcg_household>();
                if (householdlist.Count > 0)
                {
                    throw new InvalidPluginExecutionException("Record Already Exists!");
                }
            }
        }

i think the problem is with getattribute because when i check it with some hardcoded value it runs. please tell me in what stage i should register this plugin and if there is anything wrong with the code.

If your code works with hardcoded example it is probably problem with stage of execution. You have to register your plugin step in Pre-Operation stage of execution and Synchronous mode. Check this article for details .

Also, check if "mcg_HouseholdName" is correct string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM