senario: here we need to create a record in custom workflow.
need to do early bound.
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 create_invoice_record
{
public class invoicerecord : 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>();
ColumnSet cols = new ColumnSet(true);
var contact1 = service.Retrieve("salesorder", workflowcontext.PrimaryEntityId, cols);
string ordername = contact1["name"].ToString();
EntityReference customerref = (EntityReference)contact1["customerid"];
EntityReference priceref = (EntityReference)contact1["pricelevelid"];
Money totallineamount = (Money)contact1["totallineitemamount"];
Money discountamt = (Money) contact1["discountamount"];
Money taxamt = (Money)contact1["totaltax"];
Money totalamt = (Money)contact1["totalamount"];
int fixdiff = Convert.ToInt32(contact1["new_fixeddifference"]);
Money invoicetotallineamt = new Money() { Value =(totallineamount.Value / fixdiff) };
Money invoicediscountamt = new Money() { Value = (discountamt.Value / fixdiff) };
Money invoicetaxamt = new Money() { Value = (taxamt.Value / fixdiff) };
Money invoicetotalamt = new Money() { Value = (totalamt.Value / fixdiff) };
Entity incidentEntity = new Entity();
incidentEntity.LogicalName = "invoice";
incidentEntity["name"] = ordername;
incidentEntity["customerid"] = customerref;
incidentEntity["pricelevelid"] = priceref;
incidentEntity["totallineitemamount"] = invoicetotallineamt;
incidentEntity["discountamount"] = invoicediscountamt;
incidentEntity["totaltax"] = invoicetaxamt;
incidentEntity["totalamount"] = invoicetotalamt;
CreateRequest createrecord = new CreateRequest()
{
Target=incidentEntity
};
CreateResponse res = (CreateResponse)service.Execute(createrecord);
//ClientCredentials Credentials = new ClientCredentials();
}
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);
}
}
}
}
need to do early bound.
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 create_invoice_record
{
public class invoicerecord : 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>();
ColumnSet cols = new ColumnSet(true);
var contact1 = service.Retrieve("salesorder", workflowcontext.PrimaryEntityId, cols);
string ordername = contact1["name"].ToString();
EntityReference customerref = (EntityReference)contact1["customerid"];
EntityReference priceref = (EntityReference)contact1["pricelevelid"];
Money totallineamount = (Money)contact1["totallineitemamount"];
Money discountamt = (Money) contact1["discountamount"];
Money taxamt = (Money)contact1["totaltax"];
Money totalamt = (Money)contact1["totalamount"];
int fixdiff = Convert.ToInt32(contact1["new_fixeddifference"]);
Money invoicetotallineamt = new Money() { Value =(totallineamount.Value / fixdiff) };
Money invoicediscountamt = new Money() { Value = (discountamt.Value / fixdiff) };
Money invoicetaxamt = new Money() { Value = (taxamt.Value / fixdiff) };
Money invoicetotalamt = new Money() { Value = (totalamt.Value / fixdiff) };
Entity incidentEntity = new Entity();
incidentEntity.LogicalName = "invoice";
incidentEntity["name"] = ordername;
incidentEntity["customerid"] = customerref;
incidentEntity["pricelevelid"] = priceref;
incidentEntity["totallineitemamount"] = invoicetotallineamt;
incidentEntity["discountamount"] = invoicediscountamt;
incidentEntity["totaltax"] = invoicetaxamt;
incidentEntity["totalamount"] = invoicetotalamt;
CreateRequest createrecord = new CreateRequest()
{
Target=incidentEntity
};
CreateResponse res = (CreateResponse)service.Execute(createrecord);
//ClientCredentials Credentials = new ClientCredentials();
}
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);
}
}
}
}
No comments:
Post a Comment