web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Syntax Difficulty in C# Plugin Code - Specifying Entity Name

(0) ShareShare
ReportReport
Posted on by 1,589

I started coding a plugin (using the following as a guide https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.queryexpression(v=crm.5).aspx)  to perform the objective listed in the Objective section below but ran into a syntax issue I am hoping someone can help me with.
 
I am currently on Item 1) in the Objective section, and am getting errors in 3 places. I know the errors are all related to the same root problem but I am not sure what it is.
 
The specific errors are shown in comments below the respective line of code in the Specific Errors section immediately below.  
 
Specific Errors
EntityName = new_s_licenseinformation.EntityLogicalName,  //Error    3      The name 'new_s_licenseinformation' does not exist in the current context       
 
foreach (new_s_licenseinformation new_s_licenseinformation in LicenseEntityData) //Error 6      The type or namespace name 'new_s_licenseinformation' could not be found (are you missing a using directive or an assembly reference?)       

Console.WriteLine("new_licenseinformationid: {0}", new_s_licenseinformation.new_licenseinformationId); //Error   1      'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinformationId' and no extension method 'new_licenseinformationId' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)  

The last line shows up several times, but it’s the exact same problem on a different field so I didn’t post it above knowing that the same fix will apply to it.
 
I am hoping that someone can look at the three lines in my code shown in the Specific Errors section and tell me what I am doing incorrectly as well as what I might do to fix it.
 
I also included the entire class file in the section below labeled COMPLETE CODE BEGINS HERE so that you can see where the errors are within the context of the larger code file.

Any help would be greatly appreciated.
 

OBJECTIVE

 

1) I need to write a CRM 2011 plugin that fires on the creation of a record in a certain Entity ETY B. The plugin must check the system date SYSDATE, then look a records in a completely different entity, ETY A, to see if any records exist in ETY A that have SYSDATE as value in a particular field FIELD X

 

2) If one or more records exist in ETY A that has SYSDATE as the value of FIELD X, the plugin must do no more. 

 

3) However, if no records exist in ETY A, with SYSDATE as the value of FIELD X the plugin must update FIELD Z in two specific records in

completely different entity ETY C

 

4) The two specific records (Identified by GUID?) in ETY C, whose FIELD Z will be updated by the plugin may be different each time, but each

will always be identified by a unique value in FIELD G and FIELD H 

  
COMPLETE CODE BEGINS HERE
 
namespace ANumUpdate.Plugins

{

    using System;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Discovery;

    using Microsoft.Xrm.Sdk.Linq;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Metadata;

    using Microsoft.Xrm.Sdk.XmlNamespaces;

 

    /// <summary>

    /// PreValidateTestEntityCreate Plugin.

    /// </summary>    

    public class PreValidateTestEntityCreate : Plugin

    {

        /// <summary>

        /// Initializes a new instance of the <see cref="PreValidateTestEntityCreate"/> class.

        /// </summary>

        public PreValidateTestEntityCreate()

            : base(typeof(PreValidateTestEntityCreate))

        {

            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(10, "Create", "new_s_testentity", new Action<LocalPluginContext>(ExecutePreValidateTestEntityCreate)));

 

            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.

            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.

        }

 

        /// <summary>

        /// Executes the plug-in.

        /// </summary>

        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the

        /// <see cref="IPluginExecutionContext"/>,

        /// <see cref="IOrganizationService"/>

        /// and <see cref="ITracingService"/>

        /// </param>

        /// <remarks>

        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.

        /// The plug-in's Execute method should be written to be stateless as the constructor

        /// is not called for every invocation of the plug-in. Also, multiple system threads

        /// could execute the plug-in at the same time. All per invocation state information

        /// is stored in the context. This means that you should not use global variables in plug-ins.

        /// </remarks>

        protected void ExecutePreValidateTestEntityCreate(LocalPluginContext localContext)

        {

            if (localContext == null)

            {

                throw new ArgumentNullException("localContext");

            }

 

            // TODO: Implement your custom Plug-in business logic.

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            IOrganizationService service = localContext.OrganizationService;

 

            string Last2DigitsOfyear = getCurrentYearAndStartTheProcess();

            if (getTheRecords(Last2DigitsOfyear) == "NO")

            {

                //Insert Business Logic to Perform Records Update on Other Entities Here

            }

            else 

            { 

          

            //End the plugin, no update neeeded. Business as usual

                return;

            }

 

        }

 

        

        //CUSTOM FUNCTIONS USED BY BUSINESS LOGIC ABOVE

                

        protected string getCurrentYearAndStartTheProcess()

        {

            DateTime DateOfToday = DateTime.Today;

            string lastTwoDigitsOfYear = DateOfToday.ToString("yy");

            return lastTwoDigitsOfYear;

        }

 

        protected string getTheRecords(string year)

        {       IOrganizationService _service;

                QueryExpression query = new QueryExpression()

            {

                Distinct = false,

                EntityName =  new_s_licenseinformation.EntityLogicalName,  

               //Error      5         The name 'new_s_licenseinformation' does not exist in the current context  

 

                ColumnSet = new ColumnSet("new_licenseinformationId","new_licenseinfoid", "new_licenseyear"),

               //new_licenseinformationID is the record's GUID

               //new_licenseinfoid is the primary key field and stores data like B-15-8-00001-R or B-15-00002-P among other combinations

               //new_licenseyear is a text field containing the last 2 digits of a year

                Criteria =

                {

                    Filters = 

                        {

                                new FilterExpression

                                {

                                    FilterOperator = LogicalOperator.And,

                                    Conditions = 

                                    {

                                        new ConditionExpression("new_licenseyear", ConditionOperator.Equal, year),

                                      //need to select only those records, if any exist, where new_licenseyear 

//is equal to the two digits passed in the year parameter

                                    },

                                },

                        }

                }

            };

 

            DataCollection<Entity> LicenseEntityData = _service.RetrieveMultiple(query).Entities;

            //am I getting the results back right with this?

 

            // Display the results.

            Console.WriteLine("List all Licenses (Entity Name: new_s_licenseinformation) matching specified parameters");

            Console.WriteLine("===============================================");

           

            foreach (new_s_licenseinformation new_s_licenseinformation in LicenseEntityData)

            //Error         6         The type or namespace name 'new_s_licenseinformation' could not be found (are you missing a using directive or an assembly reference?) 

 

            {

                Console.WriteLine("new_licenseinformationid: {0}", new_s_licenseinformation.new_licenseinformationId);

               //Error      1         'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinformationId' and no extension method 'new_licenseinformationId' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)   

 

                Console.WriteLine("new_licenseinfoid: {0}", new_s_licenseinformation.new_licenseinfoid);

               ///Error     2         'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinfoid' and no extension method 'new_licenseinfoid' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?) 

 

                Console.WriteLine("new_licenseyear: {0}", new_s_licenseinformation.new_licenseyear);

               //Error      3         'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseyear' and no extension method 'new_licenseyear' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)  

 

            }

            Console.WriteLine("<End of Listing>");

            Console.WriteLine();

 

            if (LicenseEntityData != null)

            {

                string myYesResponse = "YES";

                return myYesResponse;

            }

            else {

                string myresponse = "NO"; 

                return myresponse; }

        }

    }

 }

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Royal King Profile Picture
    27,686 on at

    It seems you did not add c# class generated suing crmsvc util tool  to your plugin project thats why you are receiving all these error messages.  If you have not generated early bind class use crmsvc utill tool to generate the c# class and add it to your plugin project that will resolve this problem or use late binding

  • ACECORP Profile Picture
    1,589 on at

    Thank you for the information. Generating those classes using the crmsvc util tool a more complex route than I wanted to go. I ended up doing it another way, but I know I will eventually need to use the crmsvc tool to auto generate those early bind classes. I was not aware of that tool or it use until your answer and then I researched it based on your response above.

    Again thanks so much for the information!

  • Community Member Profile Picture
    on at

    I have an error in LocalPluginContext

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans