Friday 29 June 2012

custom workflow for increment or decrement field value in dynamic crm 2011

Senario: we need to set a value to one field in dynamic crm 2011 form

ex:


using System;
using System.Collections.Generic;
using System.Text;
using System.Activities;
using System.ServiceModel.Description;
using System.Workflow.ComponentModel;
//using System.Text;
using System.ServiceModel;
//using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
//using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Xrm.Sdk.Workflow;




namespace Kryptos.invoiceonbilltype.calculation
{
    public class calculatedifference:CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                //Create the context and tracing service
                IExecutionContext context = executionContext.GetExtension<IExecutionContext>();
                IWorkflowContext workflowcontext = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                ITracingService tracer = executionContext.GetExtension<ITracingService>();

                int value = InvoiceTimes.Get(executionContext);// for ex :1
                int invoice = InvoiceDate.Get(executionContext);//for ex: 30

                //Guid id = (Guid)context.InputParameters["Id"];
                //Guid id = (Guid)workflowcontext.InputParameters["id"];
                ColumnSet cols = new ColumnSet(new String[] { "new_days", "new_nextinvoice" });
                var contact1 = service.Retrieve("salesorder",workflowcontext.PrimaryEntityId , cols);

                if (contact1.Attributes.Count != 0)
                {
                    DateTime nextinvoice = Convert.ToDateTime(contact1["new_nextinvoice"]);

                    int invoices = Convert.ToInt32(contact1["new_days"]);

                 DateTime nexti =   nextinvoice.AddDays(invoice);

                    int invoicesleft = invoices - value;

                    Entity incidentEntity = new Entity();
                    incidentEntity.LogicalName = "salesorder";

                    incidentEntity["new_days"] = invoicesleft;//nextinvoice;
                    incidentEntity["new_nextinvoice"] = nexti;//invoicesleft;

                    incidentEntity["salesorderid"] = workflowcontext.PrimaryEntityId;

                    UpdateRequest updatecountreq = new UpdateRequest()
                    {
                        Target = incidentEntity,
                    };
                    UpdateResponse res = (UpdateResponse)service.Execute(updatecountreq);
                  
                }

            }
            catch (Exception ex)
            {
                //Helpers.Throw(String.Format("An error occurred in the {0} workflow.",
                //        this.GetType().ToString()),
                //      ex);
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.  " , ex);
            }
        }
        #region Input Parameters

        [Input("new_days")]
        [ReferenceTarget("salesorder")]
        public InArgument<int> InvoiceTimes { get; set; }

        [Input("new_nextinvoice")]
        [ReferenceTarget("salesorder")]
        public InArgument<int> InvoiceDate { get; set; }

        #endregion


    }
}

need to give the reference:

System.runtime.serializer.

then we need to attach it for debug:

1. take the dll and pdb to the server: dynamiccrm\server\bin\assembly
2. run the asynchronous services in service.msc
3. in x64  MSVSMON.exe, run as administrator and give permissions.
4. copy the server name in x64 MSVSMON.exe
5. register the dll in plugin register tool.
6. select all process in attachment dialogue box.
7. select CRMAsychonos.exe and attach it to process.


No comments:

Post a Comment