locked
Update Contact with Parent Account RRS feed

  • Question

  • Dear All,

    I am relativly new in CRM development and have one issue with which you can help me.

    Namely, I made one simple application in VB.NET which should provide possiblity to Update ceartin Contact with the Parent Account but face with problems.

    The problem is in it that in that application use Account name as input (not AccountID) and how much I understand if I want to update Contact with the Parent Account than must use AccountID.

    My Question to you :
    Can somebody write to me a code (in VB.NET or C#) which will provide possiblity to according to the Account Name read the AccountId and than with that AccountID Update the ParentAccount in Concact.

    Thanks in Advance.
    Tuesday, March 31, 2009 10:30 AM

Answers

  • Hi

    Take a look at http://www.xrmlinq.com, we created this tool to make it easier on developers getting started with Dynamics CRM. In your case the query would look something like this (XrmLinq)

    CrmService sdk = Connection.Create(...);
    CrmDataContext context = new CrmDataContext(sdk);

    Account searched = (from a in context.Accounts where a.AccountName.Equals("Name of account").SingleOrDefault();
    if (searched != null)
    {
        Contact someContact = ... get the contact using a LINQ query ...
        someContact.ParentCustomerId = new Customer(Accounts.Fields.SchemaName, searched.AccountId);

        CrmDataAccess.Update<Contact>(sdk, contact);
    }

    It's that simple!
    http://www.xrmlinq.com - LINQ to CRM
    Thursday, April 2, 2009 6:46 AM

All replies

  • Some advice on finding the account GUID by using the account name (I don't have a C# example for you, sorry).
    Getting the GUID this way might be tricky whenever you have accounts with the same name.
    Tuesday, March 31, 2009 10:59 AM
  •          /// <summary>   
             /// Performs a Search.   
             /// </summary>   
             /// <param name="entityName">Name of the entity.</param>   
             /// <param name="queryAttributes">The query attributes (Condition Columns).</param>   
             /// <param name="queryAttributeValues">The query attribute values (Condition to evaluate). at the moment it only compares using =.</param>   
             /// <returns></returns>   
             protected BusinessEntityCollection Search(string entityName, string[] queryAttributes, object[] queryAttributeValues)   
             {   
                 try  
                 {   
                     // Create the Query Object   
                     QueryByAttribute query = new QueryByAttribute();   
                     query.ColumnSet = new AllColumns();   
                     query.EntityName = entityName;   
       
                     // Query will retrieve all content whose type matches what was passed in   
                     // QueryByAttribute AND's all criteria's in the arrays together   
                     query.Attributes = queryAttributes;   
                     query.Values = queryAttributeValues;   
       
                     // Execute the Retrieval   
                     BusinessEntityCollection retrieved = service.RetrieveMultiple(query);   
                     return retrieved;   
                 }   
                 catch (System.Web.Services.Protocols.SoapException sopEx)   
                 {   
                     string exceptionMessage = string.Empty;   
                     if (sopEx.Detail != null)   
                     {   
                         exceptionMessage = String.Format("[Search] Error occured while executing query for Entity {0}. " + sopEx.Detail.InnerText, entityName);   
                     }   
       
                     throw new Exception(exceptionMessage, sopEx.InnerException);   
                 }   
                 catch (Exception ex)   
                 {   
                     throw;   
                 }   
             } 
    
    

    You can use the above method to retrieve an entity and pass in multiple search attributes. Once you have the entity, you can get its id and use that to update Contact's Parent Account.
    • Proposed as answer by Maruf Wednesday, April 1, 2009 4:10 PM
    Wednesday, April 1, 2009 4:09 PM
  • Hi

    Take a look at http://www.xrmlinq.com, we created this tool to make it easier on developers getting started with Dynamics CRM. In your case the query would look something like this (XrmLinq)

    CrmService sdk = Connection.Create(...);
    CrmDataContext context = new CrmDataContext(sdk);

    Account searched = (from a in context.Accounts where a.AccountName.Equals("Name of account").SingleOrDefault();
    if (searched != null)
    {
        Contact someContact = ... get the contact using a LINQ query ...
        someContact.ParentCustomerId = new Customer(Accounts.Fields.SchemaName, searched.AccountId);

        CrmDataAccess.Update<Contact>(sdk, contact);
    }

    It's that simple!
    http://www.xrmlinq.com - LINQ to CRM
    Thursday, April 2, 2009 6:46 AM