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 CRM (Archived)

Get id lookup in target entity

(0) ShareShare
ReportReport
Posted on by 403

How to get id lookup field in target entity ?, I use Plugin c#

Thanks,

4617.get-guid.PNG

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Check the below code. It retrieves the Primary Contact look field of an account entity. You can change the schema name as per your system.

                    var primaryContact = (EntityReference)target["primarycontactid"];

    Full Console Code

    ================

    public void RetrievePrimaryContactId()

          {

              // Set the credentials

              ClientCredentials credential = new ClientCredentials();

              credential.UserName.UserName = "username@testorg.onmicrosoft.com";

              credential.UserName.Password = "password";

              // Set the org url

              var organizationURI = "testorg.crm6.dynamics.com/.../Organization.svc";

              using (service = new OrganizationServiceProxy(new Uri(organizationURI), null, credential, null))

              {

                  var entityId = new Guid("AF327C64-98BE-E711-8129-E0071B67EC91");

                  var entityName = "account";

                  var target = service.Retrieve(entityName, entityId, new ColumnSet(true));

                  // Check if the Primary Context exists

                  if (target.Contains("primarycontactid"))

                  {

                      // Get the primary contact entity reference

                      var primaryContact = (EntityReference)target["primarycontactid"];

                      // Get the GUID of primary contact

                      var contactId = primaryContact.Id;

                  }

              }

          }

    ================

    Hope this helps.

  • Verified answer
    Iswarya Profile Picture
    1,347 on at

    Hi,

    try this

       Entity entity = (Entity)context.InputParameters["Target"];

                           if (entity.LogicalName != "new_activationrequest")

                           {

                               return;

                           }

                                   if (entity.Attributes.Contains("new_projectsurveyid") && entity.Attributes["new_projectsurveyid"] != null)

                               {

                                  Guid ProjectSurvey = ((EntityReference)entity.Attributes["new_projectsurveyid"]).Id;

                               }

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    This also depends in what step and message you are trying to retrieve the attribute from.

    You need to get the Project Survey Id field of the Activation Request entity.

    In general there are two ways to get the entity object.

    The first is to use the Target Parameter of the Execution Context -> Input Parameters:

    Entity activationRequest = (Entity)context.InputParameters("Target");

    The second is using the Organization Service Retrieve method passing the entity name, entity id and columns to be retrieved:

    Entity activationRequest = service.Retrieve(entityName, entityId, new ColumnSet(true));

    // The above line of code will retrieve all columns. You can filter the columns to retrieve by changing the last parameter of the Retrieve function call.

    Once you have the entity object, you can get the parameter by calling:

    Guid projectSurveyId = ((EntityReference)activationRequest.Attributes[attributeName]).Id;

    Hope this helps...

  • Ferdiansah Profile Picture
    403 on at

    Hi Iswarya,

    What if I want to read projectsurveyid is null?

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You can use the below statment to check if it si null or not

    // Returns false if it is null

    entity.Attributes.Contains("new_projectsurveyid")

  • Ferdiansah Profile Picture
    403 on at

    without adding this?

    else if (targetEntity.Attributes.Contains("swo_projectsurveyid") && targetEntity.Attributes["swo_projectsurveyid"] == null)

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans