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
    Pawar Pravin 5,231 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
    Joel D 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
    Pawar Pravin 5,231 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
    Adrian Begovich 1,019 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans