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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Unable to cast object of type 'System.Guid' to type 'System.String'

(0) ShareShare
ReportReport
Posted on by 1,589

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. 

*This post is locked for comments

I have the same question (0)
  • ACECORP Profile Picture
    1,589 on at
    RE: Unable to cast object of type 'System.Guid' to type 'System.String'

    Thanks lot Hemant!

    Your suggestion was head on! 

    Here is the exact code that fixed it:

    var TranscationID = PassedEntity.GetAttributeValue<Guid>(PassedField);
    var TransactionGUID = TranscationID.ToString();


    Thanks for your help!

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at
    RE: Unable to cast object of type 'System.Guid' to type 'System.String'

    Hi Jim,

    Does PassedField contains the guid of record in string formate.

    Please try the below code:

    var TranscationID = new Guid(PassedEntity.GetAttributeValue<string>(PassedField));

    or

    var TranscationID = PassedEntity.GetAttributeValue<Guid>(PassedField);

    I hope this helps you.

    Thanks

    Hemant

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans