newjeep19
asked on
C# Dynamic CRM error
I created a plug-in Dynamic CRM 2011 that gets some values from a stored proc then inserts those values in an attribute/field on a CRM Entity. When I run the workflow that calls or runs the plug-in I get the error below. I have serarched the web and have come up short. Please help. Thanks.
Error:
EXCEPTION=EXCEPTION=System .FormatExc eption: Unrecognized Guid format.
at System.Guid.GuidResult.Set Failure(Pa rseFailure Kind failure, String failureMessageID, Object failureMessageFormatArgume nt, String failureArgumentName, Exception innerException)
at System.Guid.TryParseGuid(S tring g, GuidStyles flags, GuidResult& result)
at System.Guid..ctor(String g)
at AddAEToCommits.AddAEToComm its.Execut e(CodeActi vityContex t executionContext)
Please let me know if you need the code as well.
Error:
EXCEPTION=EXCEPTION=System
at System.Guid.GuidResult.Set
at System.Guid.TryParseGuid(S
at System.Guid..ctor(String g)
at AddAEToCommits.AddAEToComm
Please let me know if you need the code as well.
ASKER
Thank you for the reply. However I am now getting this error
EXCEPTION=EXCEPTION=System .NullRefer enceExcept ion: Object reference not set to an instance of an object.
at AddAEToCommits.AddAEToComm its.Execut e(CodeActi vityContex t executionContext)
Code :
Guid opportunityId = Guid.Empty;
//Then update the Opportunity < -- syntax that is causing the above error
_opportunity.Id = opportunityId;
EXCEPTION=EXCEPTION=System
at AddAEToCommits.AddAEToComm
Code :
Guid opportunityId = Guid.Empty;
//Then update the Opportunity < -- syntax that is causing the above error
_opportunity.Id = opportunityId;
Why assigning an empty Guid? Any reason behind it? Also check your org context for null.
ASKER
When I run my workflow/ practice in CRM I get the error below:
Workflow paused due to error: Unhandled Exception: System.Activities.InvalidW orkflowExc eption: The following errors were encountered while processing the workflow tree:
'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error: Compiler error(s) encountered processing expression "DirectCast(CustomActivity Step4_5_co nverted, Microsoft.Xrm.Sdk.EntityRe ference)". Invalid L-value expression.:Reference expressions cannot end with Conversion. The provided expression's type must exactly match the type T of VisualBasicReference<T> or LambdaReference<T>.
at System.Activities.Hosting. WorkflowIn stance.Val idateWorkf low(Workfl owInstance ExtensionM anager extensionManager)
at System.Activities.Workflow Applicatio n.EnsureIn itialized( )
at System.Activities.Workflow Applicatio n.Enqueue( InstanceOp eration operation, Boolean push)
at System.Activities.Workflow Applicatio n.WaitForT urn(Instan ceOperatio n operation, TimeSpan timeout)
at System.Activities.Workflow Applicatio n.Internal Run(TimeSp an timeout, Boolean isUserRun)
at Microsoft.Crm.Workflow.Act ivityHost. StartWorkf lowExecuti on(Activit y workflow, ICommonWorkflowContext context)
at Microsoft.Crm.Workflow.Act ivityHostB ase.StartW orkflow(IC ommonWorkf lowContext context, Activity preLoadedActivity)
Here is my full C# code for the plug-in
Workflow paused due to error: Unhandled Exception: System.Activities.InvalidW
'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error: Compiler error(s) encountered processing expression "DirectCast(CustomActivity
at System.Activities.Hosting.
at System.Activities.Workflow
at System.Activities.Workflow
at System.Activities.Workflow
at System.Activities.Workflow
at Microsoft.Crm.Workflow.Act
at Microsoft.Crm.Workflow.Act
Here is my full C# code for the plug-in
using System;
using System.Activities;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Crm.Sdk.Messages;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Linq;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Web;
using System.Web.Services.Protocols;
using System.Xml;
using GTRI;
using GTRI.DataAccess;
using System.Configuration;
using System.Web.Services;
using System.Net.Mail;
namespace AddAEToCommits
{
public sealed class AddAEToCommits : CodeActivity
{
#region Connections
private static string connectionString = connection info here
#endregion
#region Globals
Entity opportunity = null;
Entity _opportunity = null;
string AECustomerReader = string.Empty;
Guid accountExecutiveId;
DateTime date;
Guid CustomEntityName;
Guid AECommitId;
#endregion
#region SQL
string sqlQuery = String.Empty;
private Guid monthlyCommitId;
string monthlyCommitName = String.Empty;
private Guid AECustomerGuid;
#endregion
public void Write(string arg1)
{
_Write(arg1, String.Empty);
}
public void Write(Exception ex)
{
_Write(ex.ToString(), "");
}
public void Write(Exception ex, string arg1)
{
_Write(ex.ToString(), arg1);
}
private void _Write(string s, string ss)
{
try
{
string fileDate = DateTime.Now.ToShortDateString().Replace(":", "").Replace("/", "") + DateTime.Now.ToShortTimeString().Replace(":", "").Replace(" ", "");
using (System.IO.TextWriter T = new System.IO.StreamWriter(@"C:\Logs\AEWorkflow\ErrorLog_" + fileDate + ".txt", true))
{
T.WriteLine("Adds To Commit Workflow Exception");
T.WriteLine("");
T.WriteLine("EXCEPTION=" + s.ToString());
T.WriteLine("");
T.WriteLine("EXCEPTION=" + ss.ToString()); ;
T.WriteLine("");
T.WriteLine("Created By: " + accountExecutiveId);
T.WriteLine("Estimated Closedate: " + date);
T.WriteLine("Input field: " + AECustomerReader);
T.WriteLine("Opportunity: " + CustomEntityName);
}
}
catch (Exception e)
{
e.InnerException.Message.ToString();
}
}
// Initialize the input parameter
[Input("Account Executive")]
[ReferenceTarget("systemuser")]
public InArgument<EntityReference> accountExecutiveReference { get; set; }
[Input("Date to Close")]
public InArgument<DateTime> dateToCloseReference { get; set; }
[Input("Opportunity")]
[ReferenceTarget("opportunity")]
public InOutArgument<EntityReference> opportunityReference { get; set; }
[Output("Monthly Commit Id")]
[ReferenceTarget("opportunity")]
public OutArgument<EntityReference> commitOutReference { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
try
{
//Create the IWorkflowContext and the
//IOrganizationService for communication with CRM
IWorkflowContext workflowContext = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
EntityReference systemuser = accountExecutiveReference.Get<EntityReference>(executionContext);
// Access the input parameter declared above
Guid accountExecutiveId = new Guid();
accountExecutiveId = accountExecutiveReference.Get(executionContext).Id;
DateTime date = new DateTime();
date = dateToCloseReference.Get(executionContext);
//Then update the Opportunity
Guid CustomEntityName = new Guid();
CustomEntityName = opportunityReference.Get(executionContext).Id;
_opportunity.Id = CustomEntityName;
//Execute Stored Proc
using (SqlConnection oConn = new SqlConnection(connectionString))
{
oConn.Open();
//Delete Commit link SQL statement
using (SqlCommand cmdAECommit = new SqlCommand())
{
cmdAECommit.Connection = oConn;
cmdAECommit.CommandType = CommandType.StoredProcedure;
cmdAECommit.CommandText = "dbo.AddOppInfoToCommits";
cmdAECommit.Parameters.AddWithValue("@estimatedclosedate", date);
cmdAECommit.Parameters.AddWithValue("@createdby", accountExecutiveId);
using (SqlDataReader reader = cmdAECommit.ExecuteReader())
{
int intCounter = 0;
while (reader.Read()) //opens the SQL
{
if (intCounter == 0)
{
AECustomerReader = reader["GTRI_name"].ToString();
}
intCounter++; //end of reader
}
reader.Close();
}
}
oConn.Close();
}
Entity updateOpportunity = new Entity(_opportunity.LogicalName);
updateOpportunity.Attributes["gtri_monthlycommitid"] = AECustomerReader;
service.Update(updateOpportunity);
Write("Created By: " + accountExecutiveId);
Write("Estimated Closedate: " + date);
Write("Input field: " + AECustomerReader);
Write("Opportunity: " + CustomEntityName);
}
catch (Exception ex)
{
}
}
}
}
ASKER
Please help I need an answer soon thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I wa able to workout the issue by writing the errors out to a log and searching for examples on the internet
You probably are assigning an ID (Guid type) attribute a wrong data type value or without converting it to System.Guid. Convert the value to Guid and you are good to go.