Update Entity Record Using Dynamic Entity

I want to share somecode to update entity using dynamic entity with all

//Create ICrmService object

ICrmService  _iCrmserviceObject=context.CreateCrmService(true);

//Create Dynamic entity object

 DynamicEntity _DynamicEntity = new DynamicEntity();

//Assign name of entity which you want to update let’s say account

            _DynamicEntity.Name = “account”;

//We have to add properties to dyanic entity that we want to update

//we can add directly to dynamic entity or can first create property and then add that property to dynamic entity

            // Create a KeyProperty to hold the guid of the record to be updated

            _DynamicEntity.Properties.Add(new KeyProperty(“accountid”, new Key(GUID of record which you want to update)));

            _DynamicEntity.Properties.Add(new PicklistProperty(“accountcategorycode”, new Picklist(IntValue)));

            // Create the update target.

            TargetUpdateDynamic _UpdateDynamic = new TargetUpdateDynamic();

            // Set the properties of the target.

            _UpdateDynamic.Entity = _DynamicEntity;

            //   Create the update request object.

            UpdateRequest _UpdateReq = new UpdateRequest();

            //Set request properties.

            _UpdateReq.Target = _UpdateDynamic;

            //Execute the request.

            UpdateResponse updated = (UpdateResponse)_iCrmserviceObject.Execute(_UpdateReq);

Leave a Reply

Your email address will not be published. Required fields are marked *