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)