Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How to compare a lookup ID with a Guid?

(0) ShareShare
ReportReport
Posted on by 355

I created a CWA and need to compare two GUID: the first one is the id of the input parameter, the second one is the lookup id of a record retrieved with a query expression.

I'm retrieving both GUIDs by using the following instruction:

myEntity.Attributes[entityId];

One of them (the id of the entity passed as an input parameter) is recognized as a GUID, the other one (the id of the related entity, i.e. the lookup id) is shown as an object {Microsoft.Xrm.Sdk.Reference}, so I can't compare them in the if statement to check whether they are equal or not.

As I need to check whether the related entity of the retrieved record is the same as the input one, I suppose I need to cast a lookup ID to GUID or to find another way to compare them.

This is the complete code, see line 19: while debugging, the variable tempCollectionId has the value {Microsoft.Xrm.Sdk.EntityReference}, whereas the variable myCollectionId has value {d4800847-7e4e-eb11-bf68-000d3a9c7067}:

public static void ExecuteBusinessLogic(IOrganizationService context, Entity myCollection)
        {
            try
            {
                QueryExpression query = new QueryExpression("movie");
                query.ColumnSet = new ColumnSet(true);

                LinkEntity linkEntity = new LinkEntity("collection", "movie", "collectionid", "collectionid", JoinOperator.Inner);

                query.LinkEntities.Add(linkEntity);
                query.Distinct = true;
                List moviesInTheCollection = context.RetrieveMultiple(query).Entities.ToList();

                foreach (Entity movie in moviesInTheCollection)
                {
                    var tempCollectionId = movie.Attributes["collectionid"];
                    var myCollectionid = mycollection.Attributes["collectionid"];

                    if (tempCollectionId == myCollectionid)
                    {
                        movie.Attributes["statecode"] = new OptionSetValue(1);
                    }

                }
            }
            catch (Exception ex)
            {
                var message = $"Error:{ex.Message} - {ex.StackTrace}";
                throw new InvalidWorkflowException(message, ex);
            }
        }

(The general purpose is to test a CWA which will be called from the form of a specific collection to deactivate all the movies related to it)

  • Verified answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: How to compare a lookup ID with a Guid?

    Hi Joel,

    Please use below code to get lookup guid.

    Guid tempCollectionId = movie.Attributes.Contains("collectionid") ? movie.GetAttributeValue<EntityReference>("collectionid").Id : Guid.Empty;

  • Joel D Profile Picture
    355 on at
    RE: How to compare a lookup ID with a Guid?

    Thank you for replying. How can I access the Id value?
    I'm trying to access it with the dot operator:

     var tempCollectionGuid = tempCollectionId.Id;
    

    but I can't get it this way because "Object does not contain a definition for 'Id'".

    I guess the problem is that the variable tempCollectionId is recognized as an Object (not an Entity Reference)...?

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: How to compare a lookup ID with a Guid?

    In addition of Adrian suggestion, you can pass Guid parameter to filter your query expression so that explicitly you need not to filter recordcollection.

  • Suggested answer
    Adrian Begovich Profile Picture
    1,025 Super User 2025 Season 1 on at
    RE: How to compare a lookup ID with a Guid?

    Hi Joel D,

    Entity References contain a property called Id. The property value of the EntityReference.Id Property is a GUID representing the ID of the record. You will be able to compare this value against the other GUID value inside an if statement.

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Holly Huffman Profile Picture

Holly Huffman 103

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 96 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 51 Most Valuable Professional

Product updates

Dynamics 365 release plans