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; }
}
}
}