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 GUID of an account using C#

(0) ShareShare
ReportReport
Posted on by 3,634

hi I've to set the parent account look up in the contacts. So, I've only the account name. I'm here to grab an idea to set the parent account field value using the account name only using C#

I need your kind help in this manner.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aileen Gusni Profile Picture
    44,524 on at

    Mohsin,

    You must do query with account name as keyword, query to account entity.

    But, it might return result more than 1 if it is not unique

    Example here;

    community.dynamics.com/.../151023

    Your output here is to get the id by name.

    Hope this helps,

    Thanks.

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    You can use following code:

    QueryByAttribute querybyattribute = new QueryByAttribute("account");

    querybyattribute.ColumnSet = new ColumnSet("name", "accountnumber");

    //  Attribute to query.

     querybyattribute.Attributes.AddRange("name");

     querybyattribute.Values.AddRange("Accountname"); //replace this with account name

     Entity accountEntity = service.RetrieveMultiple(querybyattribute).Entities.FirstOrDefault();

     if(accountEntity!= null)

       Guid accountID=accountEntity.Id;

    Thanks

  • Verified answer
    Mohsin Ali Profile Picture
    3,634 on at

    Hi Gusni Thank you for your comment. I'm done this using the following code snippet.

    //Query for the GUID of the Account using Account Name
    QueryExpression query = new QueryExpression("account");
        string[] cols = { "accountid", "name" };
        query.Criteria = new FilterExpression();
        query.Criteria.AddCondition("name", ConditionOperator.Equal, accountName);
        query.ColumnSet = new ColumnSet(cols);
        var account = services.RetrieveMultiple(query);
    //Casting the reference to GUID
        Guid accountId = (Guid)account[0].Attributes["accountid"];
    	
    //specify the field values
    	Entity contact = new Entity("contact");
        contact.Attributes["firstname"] = "ContactFirstName";
        contact.Attributes["lastname"] = "ContactLastName";
        //Assign Value to the Lookup Field
        contact["parentcustomerid"] = new EntityReference("account", accountId);
    //Execute the create service
        Guid contactId = services.Create(contact);
  • Suggested answer
    Saddamk206 Profile Picture
    777 on at

    //Query for the GUID of the Account using Account Name

    QueryExpression query = new QueryExpression("account");

    //Retrieve  Guid List

    EntityCollection  accounts = organizationService.RetrieveMultiple(query);

  • Saddamk206 Profile Picture
    777 on at

    Get Value of Lookup fields

    EntityReference entref = (EntityReference)item.Attributes[attributeName];

    var LookupId = entref.Id;

    var logicalName = entref.LogicalName;

    Set Value of Lookup fields

    newAccount[attributeName] = new EntityReference(logicalName, LookupId);

    Set Null Value of Lookup fields

    newAccount[attributeName] = 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