locked
Update attribute values in CRM RRS feed

  • Question

  • Hi,

    I need to update a particular attribute value in CRM using c# code. I never did this before. Any help would be appreciated.


    MSBI Developer
    Wednesday, October 26, 2011 10:54 AM

Answers

  • Hi

    \sdk\samplecode\cs\generalprogramming\earlybound check this sample out it has sample code that create an account, retrieve that account and update that account.

    The sample retrieve the account based on accountId.

    You can use queryexpression to retrieve the account based on different criteria.


                        QueryExpression query = new QueryExpression()
                        {
                            Distinct = false,
                            EntityName = Contact.EntityLogicalName,
                            ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
                            LinkEntities =
                            {
                                new LinkEntity
                                {
                                    JoinOperator = JoinOperator.LeftOuter,
                                    LinkFromAttributeName = "parentcustomerid",
                                    LinkFromEntityName = Contact.EntityLogicalName,
                                    LinkToAttributeName = "accountid",
                                    LinkToEntityName = Account.EntityLogicalName,
                                    LinkCriteria =
                                    {
                                        Conditions =
                                        {
                                            new ConditionExpression("name", ConditionOperator.Equal, "Litware, Inc.")
                                        }
                                    }
                                }
                            },
                            Criteria =
                            {
                                Filters =
                                {
                                    new FilterExpression
                                    {
                                        FilterOperator = LogicalOperator.And,
                                        Conditions =
                                        {
                                            new ConditionExpression("address1_stateorprovince", ConditionOperator.Equal, "WA"),
                                            new ConditionExpression("address1_city", ConditionOperator.In, new String[] {"Redmond", "Bellevue" , "Kirkland", "Seattle"}),
                                            new ConditionExpression("createdon", ConditionOperator.LastXDays, 30),
                                            new ConditionExpression("emailaddress1", ConditionOperator.NotNull)
                                        },
                                    },
                                    new FilterExpression
                                    {
                                        FilterOperator = LogicalOperator.Or,
                                        Conditions =
                                        {
                                            new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(206)%"),
                                            new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(425)%")
                                        }
                                    }
                                }
                            }
                        };

                        DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;

    I hope this helps.

     


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Thursday, October 27, 2011 4:16 AM
    • Marked as answer by TDPN Thursday, October 27, 2011 4:46 AM
    Thursday, October 27, 2011 4:16 AM

All replies

  • There are a few different approaches. Assuming you are using late binding (i.e. you are not generating a class which defines your entities) you can use the standard 'Entity' class approach.

    Basically you use the syntax

    Entity['yourattributename'] = "Your New Value";
    

    See http://msdn.microsoft.com/en-us/library/gg309731.aspx for details on how to do this and http://msdn.microsoft.com/en-us/library/gg328416.aspx as a walkthrough.


    Paul Dowman, CRM Developer
    • Proposed as answer by Paul Dowman Wednesday, October 26, 2011 1:11 PM
    Wednesday, October 26, 2011 11:53 AM
  • download the sdk

    http://www.microsoft.com/download/en/details.aspx?id=24004

    there are so many samples and helper code in there. They talk about different scenario in web app, in plugins etc.

    I hope this helps.

     


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Wednesday, October 26, 2011 12:25 PM
    Wednesday, October 26, 2011 12:25 PM
  • Hi,

    Thanks for the replies and i am looking into the links. Any pointers on how to retrieve records of that attribute based on a filter condition and update only those values?


    MSBI Developer
    Thursday, October 27, 2011 3:33 AM
  • Hi

    \sdk\samplecode\cs\generalprogramming\earlybound check this sample out it has sample code that create an account, retrieve that account and update that account.

    The sample retrieve the account based on accountId.

    You can use queryexpression to retrieve the account based on different criteria.


                        QueryExpression query = new QueryExpression()
                        {
                            Distinct = false,
                            EntityName = Contact.EntityLogicalName,
                            ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
                            LinkEntities =
                            {
                                new LinkEntity
                                {
                                    JoinOperator = JoinOperator.LeftOuter,
                                    LinkFromAttributeName = "parentcustomerid",
                                    LinkFromEntityName = Contact.EntityLogicalName,
                                    LinkToAttributeName = "accountid",
                                    LinkToEntityName = Account.EntityLogicalName,
                                    LinkCriteria =
                                    {
                                        Conditions =
                                        {
                                            new ConditionExpression("name", ConditionOperator.Equal, "Litware, Inc.")
                                        }
                                    }
                                }
                            },
                            Criteria =
                            {
                                Filters =
                                {
                                    new FilterExpression
                                    {
                                        FilterOperator = LogicalOperator.And,
                                        Conditions =
                                        {
                                            new ConditionExpression("address1_stateorprovince", ConditionOperator.Equal, "WA"),
                                            new ConditionExpression("address1_city", ConditionOperator.In, new String[] {"Redmond", "Bellevue" , "Kirkland", "Seattle"}),
                                            new ConditionExpression("createdon", ConditionOperator.LastXDays, 30),
                                            new ConditionExpression("emailaddress1", ConditionOperator.NotNull)
                                        },
                                    },
                                    new FilterExpression
                                    {
                                        FilterOperator = LogicalOperator.Or,
                                        Conditions =
                                        {
                                            new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(206)%"),
                                            new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(425)%")
                                        }
                                    }
                                }
                            }
                        };

                        DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;

    I hope this helps.

     


    Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com
    • Proposed as answer by Amreek Singh Thursday, October 27, 2011 4:16 AM
    • Marked as answer by TDPN Thursday, October 27, 2011 4:46 AM
    Thursday, October 27, 2011 4:16 AM
  • Hi,

    There are many ways to Update the attribute using C# code. You can either go for the early binding or the late binding. Both example I have shown below:

     Early Binding:

     Account accountObj= new Account();

      accountObj.accountnumber="74787534543984";

      sevice.Update(accountObj);

    But remember, For using the early binding you need to generate the early binding classes. 

    For More Detail: http://deepakexploring.wordpress.com/2011/02/16/crud-operation-early-bound-classes-using-wcf-services-in-crm-2011/

    Late Binding:

    Entity accountObj= new Entity("account");

    accountObj["accountnumber"]="74787534543984";

    service.Update(accountObj);

     

    -----------------------------------------------

    Deepak Kumar

    Blog: http://deepakexploring.wordpress.com/

     



    Thursday, October 27, 2011 7:14 AM