Dynamics 365





Showing posts with label Plugin. Show all posts
Showing posts with label Plugin. Show all posts

Sunday, November 17, 2019

Dynamics 365 make invoice active and Paid using C#.net code or plugin




Dynamics 365 make invoice active and Paid using C#.net code or plugin


                foreach (var item in ERPInvoices.Entities)
                {
// Set active
                    SetStateRequest setStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = new Guid(item["invoiceid"].ToString()),
                            LogicalName = "invoice",
                        },
                        State = new OptionSetValue(0),

                        Status = new OptionSetValue(1)

                    };
                    _CrmService.Execute(setStateRequest);

                  // Update the field

                    Entity invoiceToUpdate = new Entity("invoice");
                    invoiceToUpdate.Id = item.Id;
                    if (issendfullyerp)
                    {
                        invoiceToUpdate.Attributes["has_issendfullyerp"] = true;
                    }
                    if (issendpartiallyerp)
                    {
                        invoiceToUpdate.Attributes["has_issendpartiallyerp"] = true;
                    }
                    _CrmService.Update(invoiceToUpdate);
//set abck paid - to old status
                    SetStateRequest request = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference
                        {
                            Id = new Guid(item["invoiceid"].ToString()),
                            LogicalName = "invoice",
                        },
                        State = new OptionSetValue(2),
                        Status = new OptionSetValue(100001),
                    };

                    _CrmService.Execute(request);

}

Monday, November 4, 2013

Close quote Plugin code in c#.net

Close quote Plugin code in c#.net

Below code to achieve logic 
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
namespace CloseQuote
{
    public class PluginCloseQuote:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            try
            {
                // Plugin to close the quote
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.Depth<2)
                {
                    Entity entity = context.InputParameters["Target"] as Entity;

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


                    OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
                    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains("PostImage")) ? context.PostEntityImages["PostImage"] : null;
                  
                    if (postImageEntity.GetAttributeValue<bool>("pmax_isclosequote"))
                    {
                     
                        if (((OptionSetValue)postImageEntity.Attributes["statecode"]).Value == 0)
                        {
                            //method to Activate quote
                            SetEntityStatus(service, entity.Id, "quote");

                        }
                       
                        //method to close quote
                        CloseQuoteRequest req = new CloseQuoteRequest();
                        Entity quoteClose = new Entity("quoteclose");
                        quoteClose.Attributes.Add("quoteid", new EntityReference("quote", entity.Id));
                        quoteClose.Attributes.Add("subject", "Customer was mean so we just closed it.");
                        req.QuoteClose = quoteClose;
                        req.RequestName = "CloseQuote";
                        OptionSetValue o = new OptionSetValue();
                        o.Value = 5;
                        req.Status = o;
                       
                        CloseQuoteResponse resp = (CloseQuoteResponse)service.Execute(req);
                    }
                    else if (postImageEntity.GetAttributeValue<bool>("pmax_isactivatequote"))
                    {
                        if (((OptionSetValue)postImageEntity.Attributes["statecode"]).Value == 0)
                        {
                            //method to Activate quote
                            SetEntityStatus(service, entity.Id, "quote");

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(
                   "An error occurred in the CloseQuote.PluginCloseQuote Plug-in" + ex.Message, ex);
            }
        }
        //activate the quote
        private void SetEntityStatus(IOrganizationService service, Guid recordGUID, string entityName)
        {

            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = recordGUID;

            setState.EntityMoniker.Name = entityName;

            setState.EntityMoniker.LogicalName = entityName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 1;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 3;

            SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);

        }
        private void SetEntityStatusActive(IOrganizationService service, Guid recordGUID, string entityName)
        {

            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = recordGUID;

            setState.EntityMoniker.Name = entityName;

            setState.EntityMoniker.LogicalName = entityName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 3;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 5;

            SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);

        }
        private SetStateRequest inactiveSuspect(Entity entSuspect)
        {
            //to set prospect record inactive
            //WinOpportunityRequest

           
            SetStateRequest setState = new SetStateRequest();

            setState.EntityMoniker = new EntityReference();

            //Pass GUID of the record to be activated or Deactivated

            setState.EntityMoniker.Id = entSuspect.Id;

            setState.EntityMoniker.Name = "quote";

            setState.EntityMoniker.LogicalName = entSuspect.LogicalName;

            //Setting ‘State’ i.e., (0 – Active ; 1 – InActive)

            setState.State = new OptionSetValue();

            setState.State.Value = 0;

            //Setting ‘Status’ i.e., (1 – Active ; 2 – InActive)

            setState.Status = new OptionSetValue();

            setState.Status.Value = 1;


            return setState;
            //
        }
    }
}