Please wait, loading...

 

Update value in another entity on create using plugin ( Using QueryExpression ) in ms crm 2011 2013

March 19, 20141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages
;

namespace plugin1

{
    public class Class1 :IPlugin

    {
        public void Execute(IServiceProvider serviceProvider)

        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

           // Only Retrieve The Data From The Record On Which Plugin Is Firing
            new_teston test = (new_teston)service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));

            //Select * From new_testto
            QueryExpression querry = new QueryExpression(new_testto.EntityLogicalName);

            //It Will Retrieve All Fields , If Wants Specific Field : Instead Of True Write Field Name
            querry.ColumnSet = new ColumnSet(true);

            // test1 Has All Records In Entity new_testto
            EntityCollection test1 = service.RetrieveMultiple(querry);

     

            // The Loop Will Run For No Of Record Available

            for (int i = 0; i <= count; i++){     
                // test11 Has All Field Value From i(1-2-3—) Records
                new_testto test11 = (new_testto)test1.Entities[i];

                if (test.new_teston1 == test11.new_name) {
                    test11.new_singlelineto = test.new_singleline;

                }
              service.Update(test11);

            }

        }

    }

}


SOURCE : JUST2CODE.IN
Subscribe to our YouTube channel : https://www.youtube.com/user/TheRussell2012

One comment

  • Unknown

    October 21, 2014 at 11:25 pm

    hey when i used this code it throws an error on "new_teston " are u missing assembly reference.. i can understand its an custom entity but can u kindly say in detail how u declared /assigned it..
    iam new to XRM… thanks a lot for the help

Comments are closed.

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651&ssl=1
Microsoft Dynamics Community Profile

Learn more