locked
update custom entity (picklist) attribute RRS feed

  • Question

  • i have the guid of the custom entity

    i need to update a picklist value in that

    please help me with some code snippets

    Thanks
    varun
    Thursday, March 4, 2010 1:10 PM

Answers

  • First read SDK and following link
    http://msdn.microsoft.com/en-us/library/cc151183.aspx


    #region Retrieve Contact Dynamically

                // Create the retrieve target.
                TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

                // Set the properties of the target.
                targetRetrieve.EntityName = "new_CustomEnitiy"; // Custom entity name
                targetRetrieve.EntityId = "6DA37520-3707-DF11-B5C8-00155D3C0B2B"; // Custom record GUid

                // Create the request object.
                RetrieveRequest retrieve = new RetrieveRequest();

                // Set the properties of the request object.
                retrieve.Target = targetRetrieve;
                // Be aware that using AllColumns may adversely affect
                // performance and cause unwanted cascading in subsequent
                // updates. A best practice is to retrieve the least amount of
                // data required.
                retrieve.ColumnSet = new AllColumns();

                // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
                retrieve.ReturnDynamicEntities = true;

                // Execute the request.
                RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);

                // Extract the DynamicEntity from the request.
                DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;         

                #endregion


                // Contact PreferredContactMethodCode property
                PicklistProperty picklist = new PicklistProperty();

                //   Specify the picklist property name of the DynamicEntity.
                picklist.Name = "preferredcontactmethodcode";
                picklist.Value = new Picklist();

                //   Set the property's picklist index to 1.
                picklist.Value.Value = 1;           

                //   Update the properties of the DynamicEntity.
                entity.Properties.Add(picklist);

                // Create the update target.
                TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                // Set the properties of the target.
                updateDynamic.Entity = entity;

                //   Create the update request object.
                UpdateRequest update = new UpdateRequest();

                //   Set request properties.
                update.Target = updateDynamic;

                //   Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);


    http://www.allaboutdynamics.com
    Friday, March 5, 2010 7:13 AM

All replies

  • If u are using webservice, update teh webreference. then simply write your code like this.

    yourCustEntityObj.new_picklistAttribute = new new_picklistAttribute();
    yourCustEntityObj.new_picklistAttribute.Value = 1;      // check the integer value you are updating.

    If Are u using dynamic entity.

    PicklistPropery prop = new PicklistProperty("new_picklistAttribute", new Picklist());
    prop.Value.Value = 1;

    dynEntity.Properties.Add(prop);
    Muhammad Ali Khan
    My MS CRM blog
    Thursday, March 4, 2010 1:16 PM
  • i am using webreferece

    can u give me the complete code snippets for updating the value
    Thursday, March 4, 2010 1:18 PM
  • I did,
    create your object.

    new_customEntity obj = new new_customEntity(); // first get the object from using cmrService.Reterive();


    // then update your picklist.

    obj.new_picklistattribute = new new_picklistattribute();
    obj.new_picklistattribute.Value = 1;      //


    // thats all. no send the request for update

    crmService.Update(obj);
    Muhammad Ali Khan
    My MS CRM blog
    Thursday, March 4, 2010 1:20 PM
  • //first get the object from using cmrService.Reterive();

    i did not understand what do u mean by this
    Friday, March 5, 2010 5:38 AM
  • It means to create an object to your custom entity for accessing it.

    e.g Your entity is Opportunity, then the below sample is

    opportunity opportunityRecord = new opportunity();
    opportunityRecord.opportunityid = new Key();
    opportunityRecord.opportunityid.Value = <<opportunityId>>; // Pass GUID
    Picklist customPicklist = new Picklist();
    customPicklist.Value = 1; // Set the value of the Picklist here
    opportunityRecord.<<YourCustomPicklistAttributeName>> = customPicklist;
    CRMService.Update(opportunityRecord);

    Regards
    Vinoth
    Friday, March 5, 2010 5:46 AM
  • will this work for custom entities 
    Friday, March 5, 2010 5:51 AM
  • Its for Custom entity           

    #region  Update the DynamicEntity

                // Contact Credit Limit
                CrmMoneyProperty money = new CrmMoneyProperty();

                // Specify the property name of the DynamicEntity.
                money.Name = "creditlimit";
                money.Value = new CrmMoney();

                // Specify a $10000 credit limit.
                money.Value.Value = 10000M;

                // Contact PreferredContactMethodCode property
                PicklistProperty picklist = new PicklistProperty();

                //   Specify the property name of the DynamicEntity.
                picklist.Name = "preferredcontactmethodcode";
                picklist.Value = new Picklist();

                //   Set the property's picklist index to 1.
                picklist.Value.Value = 1;

                // Contact ParentCustomerId property.
                CustomerProperty parentCustomer = new CustomerProperty();

                //   Specify the property name of the DynamicEntity.
                parentCustomer.Name = "parentcustomerid";
                parentCustomer.Value = new Customer();

                //   Set the customer type to account.
                parentCustomer.Value.type = EntityName.account.ToString();

                //   Specify the GUID of an existing CRM account.
                // SDK:parentCustomer.Value.Value = new Guid("A0F2D8FE-6468-DA11-B748-000D9DD8CDAC");
                parentCustomer.Value.Value = accountID;

                //   Update the DynamicEntities properties collection to add new properties.
                //   Convert the properties array of DynamicEntity to an ArrayList.
                ArrayList arrProps = new ArrayList(entity.Properties);

                //   Add properties to ArrayList.
                arrProps.Add(money);
                arrProps.Add(picklist);
                arrProps.Add(parentCustomer);

                //   Update the properties array on the DynamicEntity.
                entity.Properties = (Property[])arrProps.ToArray(typeof(Property));

                // Create the update target.
                TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                // Set the properties of the target.
                updateDynamic.Entity = entity;

                //   Create the update request object.
                UpdateRequest update = new UpdateRequest();

                //   Set request properties.
                update.Target = updateDynamic;

                //   Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);

                #endregion


    http://www.allaboutdynamics.com
    Friday, March 5, 2010 5:56 AM
  • i did not understand anything in the last post......can u please smplify this...Shahid_Hameed
    Friday, March 5, 2010 6:42 AM
  • DynamicEntity

     

    entity = new DynamicEntity();

     

    // Contact PreferredContactMethodCode property

     

    PicklistProperty picklist = new PicklistProperty();

     

    // Specify the picklist property name of the DynamicEntity.

    picklist.Name =

    "preferredcontactmethodcode";

    picklist.Value =

    new Picklist();

     

    // Set the property's picklist index to 1.

    picklist.Value.Value = 1;

     

    // Convert the properties array of DynamicEntity to an ArrayList.

     

    ArrayList arrProps = new ArrayList(entity.Properties);

     

    // Add properties to ArrayList.

    arrProps.Add(picklist);

     

    // Update the properties array on the DynamicEntity.

    entity.Properties = (

    Property[])arrProps.ToArray(typeof(Property));

     

    // Create the update target.

     

    TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

     

    // Set the properties of the target.

    updateDynamic.Entity = entity;

     

    // Create the update request object.

     

    UpdateRequest update = new UpdateRequest();

     

    // Set request properties.

    update.Target = updateDynamic;

     

    // Execute the request.

     

    UpdateResponse updated = (UpdateResponse)service.Execute(update);


    http://www.allaboutdynamics.com
    Friday, March 5, 2010 6:48 AM
  • Or you can do in this way


                DynamicEntity entity = new DynamicEntity();
                // Contact PreferredContactMethodCode property
                PicklistProperty picklist = new PicklistProperty();
                //   Specify the picklist property name of the DynamicEntity.
                picklist.Name = "preferredcontactmethodcode";
                picklist.Value = new Picklist();
                //   Set the property's picklist index to 1.
                picklist.Value.Value = 1;            
                //   Update the properties of the DynamicEntity.
                entity.Properties.Add(picklist);
                // Create the update target.
                TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();
                // Set the properties of the target.
                updateDynamic.Entity = entity;
                //   Create the update request object.
                UpdateRequest update = new UpdateRequest();
                //   Set request properties.
                update.Target = updateDynamic;
                //   Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);


    http://www.allaboutdynamics.com
    Friday, March 5, 2010 6:51 AM
  • where do i give the guid of the record
    Friday, March 5, 2010 7:02 AM
  • Varun

    Can you provide your custom entity name and picklist name?

    Regards
    Vinoth

    Friday, March 5, 2010 7:10 AM
  • First read SDK and following link
    http://msdn.microsoft.com/en-us/library/cc151183.aspx


    #region Retrieve Contact Dynamically

                // Create the retrieve target.
                TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();

                // Set the properties of the target.
                targetRetrieve.EntityName = "new_CustomEnitiy"; // Custom entity name
                targetRetrieve.EntityId = "6DA37520-3707-DF11-B5C8-00155D3C0B2B"; // Custom record GUid

                // Create the request object.
                RetrieveRequest retrieve = new RetrieveRequest();

                // Set the properties of the request object.
                retrieve.Target = targetRetrieve;
                // Be aware that using AllColumns may adversely affect
                // performance and cause unwanted cascading in subsequent
                // updates. A best practice is to retrieve the least amount of
                // data required.
                retrieve.ColumnSet = new AllColumns();

                // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
                retrieve.ReturnDynamicEntities = true;

                // Execute the request.
                RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);

                // Extract the DynamicEntity from the request.
                DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;         

                #endregion


                // Contact PreferredContactMethodCode property
                PicklistProperty picklist = new PicklistProperty();

                //   Specify the picklist property name of the DynamicEntity.
                picklist.Name = "preferredcontactmethodcode";
                picklist.Value = new Picklist();

                //   Set the property's picklist index to 1.
                picklist.Value.Value = 1;           

                //   Update the properties of the DynamicEntity.
                entity.Properties.Add(picklist);

                // Create the update target.
                TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                // Set the properties of the target.
                updateDynamic.Entity = entity;

                //   Create the update request object.
                UpdateRequest update = new UpdateRequest();

                //   Set request properties.
                update.Target = updateDynamic;

                //   Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);


    http://www.allaboutdynamics.com
    Friday, March 5, 2010 7:13 AM