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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Trying to Get GUID of PreImage lookup field

(0) ShareShare
ReportReport
Posted on by
Hi, 
I am trying to get Guid of the Preimage lookup field.
 
The concept here is, I am having a Student entity that is related to Domain entity (1:N) and there is a subgrid of Domain in student form. now when the Domain record is deleted
i am fetching that records Student field value(Lookup or preImage) once i get the lookup field value i am writng a query expression to match Domains lookup student field with the preImage (Retrieve multiple) and the records count is null i am change the field value. 
 
Below given is the source code i am using: 
 
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace makeFieldRed
{
    public class checkForMoreRecords : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            if (context.InputParameters.Contains(/Target/) && context.InputParameters[/Target/] is Entity)
            {
                
                if (context.PrimaryEntityName == /mahi_domain/)
                {
                    Entity entity = (Entity)context.InputParameters[/Target/];

                    Entity preImage = context.PreEntityImages[/mahi_student/];//Getting preImage field value
                    //Entity preImage = (Entity)context.InputParameters[/preImage/];

                    Guid projectGuid = (Guid)preImage.Attributes[/mahi_student/]; //Getting guid of the preImage lookup
                    // getting domain records with student lookup 
                    QueryExpression queryExpression = new QueryExpression(/mahi_domain/);
                    queryExpression.ColumnSet = new ColumnSet(/mahi_student/);
                    queryExpression.Criteria.AddCondition(new ConditionExpression(/mahi_student/, ConditionOperator.Equal, projectGuid));
                    EntityCollection domainRecords = service.RetrieveMultiple(queryExpression);
                    if(domainRecords != null)
                    {
                        Entity updateStudent = new Entity(/mahi_student/);
                        updateStudent.Id = projectGuid;
                        updateStudent[/mahi_domainsopp/] = /Plugin - Opp Exists/;
                        service.Update(updateStudent);
                    } else
                    {
                        Entity updateStudent = new Entity(/mahi_student/);
                        updateStudent.Id = projectGuid;
                        updateStudent[/mahi_domainsopp/] = /Plugin - Opp need to be created/;
                        service.Update(updateStudent);
                    }
                }
            }
        }
    }
}
 
 

Would be really helpful if anyone could share the correct syntax to retrieve the pre-image of a GUID field.

Or if there's a better way to go about approaching the problem.

Thanks!

I have the same question (0)
  • Suggested answer
    Leah Ju Profile Picture
    Microsoft Employee on at
    Hi Partner,
    Please refer to the following similar thread:
    Or you can try to use power automate to achieve your goal easily.

    I hope you can mark my answer verified if it is helpful! If you have any questions, please feel free to contact me.
    Regards,
    Leah

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans