Reputation: 55
I am new to CRM and trying to write a plugin.
It's should be simple, but it's not.
For some reason I can't retrieve entity. I know for sure that it is exists in database and ID is correct.
The code below....
Does anyone has any ideas why it is not working? Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
using System.Collections;
namespace Copy_field
{
public class Copy_field : IPlugin
{
/// <summary>
/// A plugin copyies fields from Contact Entity to Case Entity. This allows display
/// information about the client on case Entity and change it directly from Case Entity
/// </summary>
/// <remarks>Register this plug-in on the Create case, update case and update of contact
/// </remarks>
///
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity entity;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "incident") { return; }
}
else
{
return;
}
// tracer.Trace("1. THIS IS an/a: " + entity.LogicalName);
// tracer.Trace("2. Id: " + entity.Id);
// tracer.Trace("2.2 output parametersId: " + context.OutputParameters["id"].ToString());
// if record exists - retrieve the entity
///////////////////////////////////////////////////////////////////
ColumnSet cols = new ColumnSet(true);
Entity yahhooo = service.Retrieve(entity.LogicalName, entity.Id, cols);
}
}
}
Upvotes: 0
Views: 1227
Reputation: 41
don't forget that in update message , you only access to filed's that you update(change) it... you can write as in the following method to trace your code(line to line)
public static void LogFile(string log)
{
try
{
TextWriter tw = new StreamWriter(@"C:\Inetpub\wwwroot\inventorylog_" + Guid.NewGuid() + ".txt");
tw.Write(log);
tw.Close();
}
catch
{
}
}
Be successful
Upvotes: 1
Reputation: 362
Well, you could give us a little bit more of information about it like, is it throwing any exception? My guess is that you are getting the following error: "The "yourentity" with id "yourentityid" does not exist in the database", if so you may be trying to retrieve a record on it's create on Pre-Operation pipeline. Anyway, help us help you giving us more information.
Upvotes: 0