locked
Updating custom attribute from all accounts RRS feed

  • Question

  • Hi everyone,

    I fairly new to CRM development and I was wondering if you could give me some advice.

    I've added a custom attribute to the CRM Account entity, and I wish to update the attribute value from code.

    This is what I've got so far:

    // Set up the CRM Service.

     

     

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
    token.AuthenticationType = 0;
    token.OrganizationName =
    "AxxesCRM";
    CrmService service = new CrmService();
    service.Url = http://xxx/MSCrmServices/2007/Crmservice.asmx?WSDL&uniquename=AxxesCRM;
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.
    CredentialCache.DefaultCredentials;

    // Create the QueryExpression object.
    QueryExpression query = new QueryExpression();
    // Set the properties of the QueryExpression object.
    query.EntityName = EntityName.account.ToString();
    query.ColumnSet =
    new ColumnSet();
    query.ColumnSet.AddColumn(
    "name");
    query.ColumnSet.AddColumn(
    "ownerid");
    query.ColumnSet.AddColumn(
    "owningbusinessunit");
    query.ColumnSet.AddColumn(
    "axxes_accountmanager1id");

    // Retrieve the accounts.
    RetrieveMultipleRequest requestAccount = new RetrieveMultipleRequest();
    requestAccount.Query = query;
    requestAccount.ReturnDynamicEntities =
    true;
    RetrieveMultipleResponse result = (RetrieveMultipleResponse)service.Execute(requestAccount);
    BusinessEntityCollection accounts = service.RetrieveMultiple(query);
    foreach( account acc in accounts.BusinessEntities)
    {//DoUpdate}

    As you can see I've made no use of a web service reference. Because when I did I only received a class which represented the account entity, along with the custom attributes, but no way of retrieving accounts from CRM.

    But the way I'm using it now, I'm not getting the custom attributes out of the accounts returned, let alone update them to CRM.

    Can anyone tell me what I'm doing wrong? How can I use the entity provided in the refferenced webservice?

    All help is appreciated!

    Thursday, September 23, 2010 7:46 AM

Answers

  • I've managed to succeed. For future reference, here is my code:

    // Set up the CRM Service.
          CrmAuthenticationToken token = new CrmAuthenticationToken();
          // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
          token.AuthenticationType = 0;
          token.OrganizationName = "AxxesCRM";
    
          CrmService service = new CrmService();
          service.Url = "http://xxx/MSCrmServices/2007/Crmservice.asmx?WSDL&uniquename=AxxesCRM";
          service.CrmAuthenticationTokenValue = token;
          service.Credentials = System.Net.CredentialCache.DefaultCredentials;
          
    
          // Create the QueryExpression object.
          QueryExpression query = new QueryExpression();
    
          // Set the properties of the QueryExpression object.
          query.EntityName = EntityName.account.ToString();
          query.ColumnSet = new ColumnSet();
          query.ColumnSet.AddColumn("name");
          query.ColumnSet.AddColumn("ownerid");
          query.ColumnSet.AddColumn("owningbusinessunit");
          query.ColumnSet.AddColumn("axxes_accountmanager1id");
          // Retrieve the accounts.
          RetrieveMultipleRequest requestAccount = new RetrieveMultipleRequest();
          requestAccount.Query = query;
          requestAccount.ReturnDynamicEntities = true;
          RetrieveMultipleResponse result = (RetrieveMultipleResponse)service.Execute(requestAccount);
          BusinessEntityCollection accounts = service.RetrieveMultiple(query);
          pgb.Maximum = accounts.BusinessEntities.Count;
          pgb.Value = 0;
          foreach (DynamicEntity acct in result.BusinessEntityCollection.BusinessEntities)
          {
              Owner o = (Owner)acct.Properties["ownerid"];
              Lookup lu = (Lookup)acct.Properties["owningbusinessunit"]; 
              if (lu.Value == new Guid("{f4fb36f6-71c1-df11-8ed7-00155d02c900}"))//
              {
                acct.Properties["axxes_accountmanager1id"] = new Lookup("systemuser",new Guid( o.Value.ToString()));
              }
              else
              {
                acct.Properties["axxes_accountmanager2id"] = new Lookup("systemuser", new Guid(o.Value.ToString()));
                
              }
              service.Update(acct);
            if(pgb.Value != pgb.Maximum)
              pgb.Value++;
            lst.SelectedIndex = lst.Items.Count - 1;
            Application.DoEvents();
          }

     

    Thursday, September 23, 2010 9:14 AM

All replies

  • not sure I understand your question, but you have to write update request seperatly in your foreach loop

    refer http://msdn.microsoft.com/en-us/library/bb929003.aspx 

    if you could provide your question with some more clarity that will help


    Mahain : http://mahenderpal.wordpress.com
    Thursday, September 23, 2010 8:00 AM
    Moderator
  • If you use the pre-compiled crmservice proxy you need to cast each BusinessEntity as a DynamicEntity then you can access the custom attributes in the Properties collection by name. See the SDK.
    Thursday, September 23, 2010 8:02 AM
  • I've managed to succeed. For future reference, here is my code:

    // Set up the CRM Service.
          CrmAuthenticationToken token = new CrmAuthenticationToken();
          // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
          token.AuthenticationType = 0;
          token.OrganizationName = "AxxesCRM";
    
          CrmService service = new CrmService();
          service.Url = "http://xxx/MSCrmServices/2007/Crmservice.asmx?WSDL&uniquename=AxxesCRM";
          service.CrmAuthenticationTokenValue = token;
          service.Credentials = System.Net.CredentialCache.DefaultCredentials;
          
    
          // Create the QueryExpression object.
          QueryExpression query = new QueryExpression();
    
          // Set the properties of the QueryExpression object.
          query.EntityName = EntityName.account.ToString();
          query.ColumnSet = new ColumnSet();
          query.ColumnSet.AddColumn("name");
          query.ColumnSet.AddColumn("ownerid");
          query.ColumnSet.AddColumn("owningbusinessunit");
          query.ColumnSet.AddColumn("axxes_accountmanager1id");
          // Retrieve the accounts.
          RetrieveMultipleRequest requestAccount = new RetrieveMultipleRequest();
          requestAccount.Query = query;
          requestAccount.ReturnDynamicEntities = true;
          RetrieveMultipleResponse result = (RetrieveMultipleResponse)service.Execute(requestAccount);
          BusinessEntityCollection accounts = service.RetrieveMultiple(query);
          pgb.Maximum = accounts.BusinessEntities.Count;
          pgb.Value = 0;
          foreach (DynamicEntity acct in result.BusinessEntityCollection.BusinessEntities)
          {
              Owner o = (Owner)acct.Properties["ownerid"];
              Lookup lu = (Lookup)acct.Properties["owningbusinessunit"]; 
              if (lu.Value == new Guid("{f4fb36f6-71c1-df11-8ed7-00155d02c900}"))//
              {
                acct.Properties["axxes_accountmanager1id"] = new Lookup("systemuser",new Guid( o.Value.ToString()));
              }
              else
              {
                acct.Properties["axxes_accountmanager2id"] = new Lookup("systemuser", new Guid(o.Value.ToString()));
                
              }
              service.Update(acct);
            if(pgb.Value != pgb.Maximum)
              pgb.Value++;
            lst.SelectedIndex = lst.Items.Count - 1;
            Application.DoEvents();
          }

     

    Thursday, September 23, 2010 9:14 AM