Quantcast
Channel: Forum CRM Deployment
Viewing all articles
Browse latest Browse all 997

Plugin MessageName Create for update record

$
0
0

Hi

I have a plugin that identifies the record if it exists it updates. If you are new should be the path of creation. The plugin is updating and creating a new record.

Code:

        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                Entity entidade = null;
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) entidade = (Entity)context.InputParameters["Target"];

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

                if (context.MessageName.ToUpper() != "CREATE") { return; }
                if (entidade != null)
                {
                    ColumnSet attributes = new ColumnSet(new string[] { "corp_chave_integracao", "corp_grupo_empresaid", "corp_nome_grupo" });

                    QueryExpression queryL = new QueryExpression("corp_grupo_empresa");
                     queryL.ColumnSet = attributes;
                   // queryL.ColumnSet = new ColumnSet("corp_chave_integracao", "corp_grupo_empresaid", "corp_nome_grupo");

                    queryL.Criteria.AddCondition(new ConditionExpression("corp_chave_integracao", ConditionOperator.Equal, entidade.Attributes["corp_chave_integracao"]));

                    var colecaoL = service.RetrieveMultiple(queryL);

                    
                    if (colecaoL.Entities.Count > 0)
                    {

                            var nomeGrupo = entidade.Attributes["corp_nome_grupo"];
                            var ChaveIntegracao = entidade.Attributes["corp_chave_integracao"];
                            Entity grupoEmpresa = new Entity("corp_grupo_empresa");

                            //grupoEmpresa = service.Retrieve("corp_grupo_empresa", colecaoL.Entities[0].Id, attributes);

                            grupoEmpresa["corp_nome_grupo"] = nomeGrupo;
                            grupoEmpresa["corp_chave_integracao"] = ChaveIntegracao;
                            grupoEmpresa["corp_grupo_empresaid"] = colecaoL.Entities[0].Id;

                            service.Update(grupoEmpresa);

                            //throw new InvalidPluginExecutionException();                            

                        
                    }

                }

            }
            catch (Exception ex)
            {
                //Log.Tratar(ex);
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }

        }




Viewing all articles
Browse latest Browse all 997

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>