I have a plugin where one of the C# methods is to query a record and retrieve the GUID of the record.
I retrieve the GUID but I need to convert it to a string value so that I can return it for use elsewhere in my code.
PassedField is the field that contains the GUID.
I am fairly certain the problem is either following line of code from within the GetTransaction method
var TranscationID = PassedEntity.GetAttributeValue<string>(PassedField);
or the actual method itself as it is called from the top level area of my code which is
var TransactionGUID = GetTransactionID(service, localContext, new_s_cashmanagemement, "new_s_cashmanagemementid");
Unfortunately, I am getting the error message Unable to cast object of type 'System.Guid' to type 'System.String'. between the time I retrieve the GUID and the time I need to update It which narrows it down to one or both of the two lines of code shown above.
I did some preliminary research and found this Stack Exchange Article, but it doesn't seem to apply to my problem, or I am not yet skilled enough at development to understand how to apply it to my situation.
What happens in my code is that I call the GetTransactionID method and pass it several paremeters. Specifically the parameter field named new_s_cashmanagementid is what houses the entity’s GUID (primary key field) whose value I am seeking.
//Get GUID of Transaction Record
var TransactionGUID = GetTransactionID(service, localContext, new_s_cashmanagemement, "new_s_cashmanagemementid");
If I use the same method, but pass it a non-GUID text field as the field whose value I want, it works great – so I know my code is structured correctly. The problem only happens when specify a GUID field, so I am confident that this is a syntax problem with respect to converting the GUID to a string.
The entire GetTransactionID method is shown below.
protected string GetTransactionID(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
try
{
var TranscationID = PassedEntity.GetAttributeValue<string>(PassedField).ToString();
if (String.IsNullOrEmpty(TranscationID))
{
try
{
var result = "The ID is null or empty";
return result;
}
catch (Exception ex) { throw new Exception("Error in IF/TRY TransactionID Check: " + ex.Message); }
}
else
{
try
{
var result2 = TranscationID;
return result2;
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY TransactionID Check: " + ex.Message); }
}
}
catch (Exception ex) { throw new Exception("Error in GetTransactionMethod: " + ex.Message); }
}
return null;
}
The error itself displays above this method, in my top level code, so I am confident that the problem is either following line of code from within the GetTransaction method which is
var TranscationID = PassedEntity.GetAttributeValue<string>(PassedField);
or the actual method itself as it is called from the top level area of my code which is
var TransactionGUID = GetTransactionID(service, localContext, new_s_cashmanagemement, "new_s_cashmanagemementid");
I am hoping that someone looking at this knows exactly what I am doing incorrectly and can help me re-format the code in such as way so that I can get the GUID but successfully convert it to a string variable, and then return it as a string value for access in my top level code.
Again, this method works great if I grab any other field that’s not a GUID so we can be certain the issue is solely with the conversion.
Any help would be greatly appreciated.