web
You’re offline. This is a read only version of the page.
close
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 363

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)

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    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.

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

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

  • Joel D Profile Picture
    363 on at

    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)...?

  • Verified answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi Joel,

    Please use below code to get lookup guid.

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

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans