In this article , I am going to explain, how to convert Opportunity to a Quote, then convert a Quote to a Sales Order and a Sales Order to an Invoice
For better understanding , i divided code into three parts
(i) Convert Opportunity to Quote
(ii) Convert Quote to Sales Order
(iii) Convert Sales Order to Invoice
Namespace need to include
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
Convert Opportunity to Quote
Guid _opportunityId = new Guid("ffbb6e3f-ccfe-4d84-90d9-421418b03a8e");
// Close the opportunity as won
var winOppRequest = new WinOpportunityRequest
{
OpportunityClose = new OpportunityClose
{
OpportunityId = new EntityReference(Opportunity.EntityLogicalName, _opportunityId)
},
Status = new OptionSetValue((int)opportunity_statuscode.Won),
};
_serviceProxy.Execute(winOppRequest);
// Convert the opportunity to a quote
var genQuoteFromOppRequest = new GenerateQuoteFromOpportunityRequest
{
OpportunityId = _opportunityId,
ColumnSet = new ColumnSet("quoteid", "name")
};
var genQuoteFromOppResponse = (GenerateQuoteFromOpportunityResponse)
_serviceProxy.Execute(genQuoteFromOppRequest);
Quote quote = genQuoteFromOppResponse.Entity.ToEntity<Quote>();
Guid _quoteId = quote.Id;
// Set the quote's product
Guid _productId = new Guid("eebb6e3f-ccfe-4d84-90d9-421418b03a8e");
Guid _defaultUnitId = new Guid("mmbb6e3f-ccfe-4d84-90d9-421418b03a8e");
QuoteDetail quoteDetail = new QuoteDetail()
{
ProductId = new EntityReference(Product.EntityLogicalName,
_productId),
Quantity = 1,
QuoteId = quote.ToEntityReference(),
UoMId = new EntityReference(UoM.EntityLogicalName,
_defaultUnitId)
};
Guid _quoteDetailId = _serviceProxy.Create(quoteDetail);
// Activate the quote
SetStateRequest activateQuote = new SetStateRequest()
{
EntityMoniker = quote.ToEntityReference(),
State = new OptionSetValue((int)QuoteState.Active),
Status = new OptionSetValue((int)quote_statuscode.InProgress)
};
_serviceProxy.Execute(activateQuote);
// Mark the quote as won
// Note: this is necessary in order to convert a quote into a
// SalesOrder.
WinQuoteRequest winQuoteRequest = new WinQuoteRequest()
{
QuoteClose = new QuoteClose()
{
Subject = "Quote Close" + DateTime.Now.ToString(),
QuoteId = quote.ToEntityReference()
},
Status = new OptionSetValue(-1)
};
_serviceProxy.Execute(winQuoteRequest);
Convert Quote to Sales Order
ColumnSet salesOrderColumns = new ColumnSet("salesorderid", "totalamount");
ConvertQuoteToSalesOrderRequest convertQuoteRequest =
new ConvertQuoteToSalesOrderRequest()
{
QuoteId = _quoteId,
ColumnSet = salesOrderColumns
};
ConvertQuoteToSalesOrderResponse convertQuoteResponse =
(ConvertQuoteToSalesOrderResponse)_serviceProxy.Execute(convertQuoteRequest);
SalesOrder salesOrder = (SalesOrder)convertQuoteResponse.Entity;
Guid _salesOrderId = salesOrder.Id;
Guid _priceListItemId = new Guid("eebb6e3f-ccfe-4d84-90d9-421418b03a8e");
// Retrieve current price list
ProductPriceLevel priceListItem =
(ProductPriceLevel)_serviceProxy.Retrieve(ProductPriceLevel.EntityLogicalName,
_priceListItemId, new ColumnSet("productpricelevelid", "amount"));
// Update the price list
priceListItem.Amount = new Money(30.0M);
UpdateRequest updatePriceListItem = new UpdateRequest()
{
Target = priceListItem,
};
_serviceProxy.Execute(updatePriceListItem);
// Retrieve the order
SalesOrder updatedSalesOrder = (SalesOrder)_serviceProxy.Retrieve(
SalesOrder.EntityLogicalName,_salesOrderId,new ColumnSet("salesorderid", "totalamount")
);
// Unlock the order pricing
UnlockSalesOrderPricingRequest unlockOrderRequest =new UnlockSalesOrderPricingRequest()
{
SalesOrderId = _salesOrderId
};
_serviceProxy.Execute(unlockOrderRequest);
// Retrieve the order
updatedSalesOrder = (SalesOrder)_serviceProxy.Retrieve( SalesOrder.EntityLogicalName,
_salesOrderId, new ColumnSet("salesorderid", "totalamount") );
// Relock the order pricing
LockSalesOrderPricingRequest lockOrderRequest = new LockSalesOrderPricingRequest()
{
SalesOrderId = _salesOrderId
};
_serviceProxy.Execute(lockOrderRequest);
Convert Sales Order to Invoice
ColumnSet invoiceColumns = new ColumnSet("invoiceid", "totalamount");
// Convert the order to an invoice
ConvertSalesOrderToInvoiceRequest convertOrderRequest = new ConvertSalesOrderToInvoiceRequest()
{
SalesOrderId = _salesOrderId,
ColumnSet = invoiceColumns
};
ConvertSalesOrderToInvoiceResponse convertOrderResponse =
(ConvertSalesOrderToInvoiceResponse)_serviceProxy.Execute(convertOrderRequest);
Invoice invoice = (Invoice)convertOrderResponse.Entity;
Guid _invoiceId = invoice.Id;
// Retrieve current price list
priceListItem = (ProductPriceLevel)_serviceProxy.Retrieve(ProductPriceLevel.EntityLogicalName,
_priceListItemId,new ColumnSet("productpricelevelid", "amount"));
// Lock the invoice pricing
LockInvoicePricingRequest lockInvoiceRequest = new LockInvoicePricingRequest()
{
InvoiceId = _invoiceId
};
_serviceProxy.Execute(lockInvoiceRequest);
// Update the price list
priceListItem.Amount = new Money(40.0M);
updatePriceListItem = new UpdateRequest()
{
Target = priceListItem
};
_serviceProxy.Execute(updatePriceListItem);
// Retrieve the invoice
Invoice updatedInvoice = (Invoice)_serviceProxy.Retrieve(
Invoice.EntityLogicalName,
_invoiceId, new ColumnSet("invoiceid", "totalamount"));
// Unlock the invoice pricing
UnlockInvoicePricingRequest unlockInvoiceRequest = new UnlockInvoicePricingRequest()
{
InvoiceId = _invoiceId
};
_serviceProxy.Execute(unlockInvoiceRequest);
// Retrieve the invoice
updatedInvoice = (Invoice)_serviceProxy.Retrieve(Invoice.EntityLogicalName,
_invoiceId,
new ColumnSet("invoiceid", "totalamount"));
Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
serverConfig.HomeRealmUri,
serverConfig.Credentials,
serverConfig.DeviceCredentials))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
Guid _opportunityId = new Guid("ffbb6e3f-ccfe-4d84-90d9-421418b03a8e");
// Close the opportunity as won
var winOppRequest = new WinOpportunityRequest
{
OpportunityClose = new OpportunityClose
{
OpportunityId = new EntityReference(Opportunity.EntityLogicalName, _opportunityId)
},
Status = new OptionSetValue((int)opportunity_statuscode.Won),
};
_serviceProxy.Execute(winOppRequest);
// Convert the opportunity to a quote
var genQuoteFromOppRequest = new GenerateQuoteFromOpportunityRequest
{
OpportunityId = _opportunityId,
ColumnSet = new ColumnSet("quoteid", "name")
};
var genQuoteFromOppResponse = (GenerateQuoteFromOpportunityResponse)
_serviceProxy.Execute(genQuoteFromOppRequest);
Quote quote = genQuoteFromOppResponse.Entity.ToEntity<Quote>();
Guid _quoteId = quote.Id;
// Set the quote's product
Guid _productId = new Guid("eebb6e3f-ccfe-4d84-90d9-421418b03a8e");
Guid _defaultUnitId = new Guid("mmbb6e3f-ccfe-4d84-90d9-421418b03a8e");
QuoteDetail quoteDetail = new QuoteDetail()
{
ProductId = new EntityReference(Product.EntityLogicalName,
_productId),
Quantity = 1,
QuoteId = quote.ToEntityReference(),
UoMId = new EntityReference(UoM.EntityLogicalName,
_defaultUnitId)
};
Guid _quoteDetailId = _serviceProxy.Create(quoteDetail);
// Activate the quote
SetStateRequest activateQuote = new SetStateRequest()
{
EntityMoniker = quote.ToEntityReference(),
State = new OptionSetValue((int)QuoteState.Active),
Status = new OptionSetValue((int)quote_statuscode.InProgress)
};
_serviceProxy.Execute(activateQuote);
// Mark the quote as won
// Note: this is necessary in order to convert a quote into a
// SalesOrder.
WinQuoteRequest winQuoteRequest = new WinQuoteRequest()
{
QuoteClose = new QuoteClose()
{
Subject = "Quote Close" + DateTime.Now.ToString(),
QuoteId = quote.ToEntityReference()
},
Status = new OptionSetValue(-1)
};
_serviceProxy.Execute(winQuoteRequest);
// Define columns to be retrieved after creating the order
ColumnSet salesOrderColumns = new ColumnSet("salesorderid", "totalamount");
ConvertQuoteToSalesOrderRequest convertQuoteRequest =
new ConvertQuoteToSalesOrderRequest()
{
QuoteId = _quoteId,
ColumnSet = salesOrderColumns
};
ConvertQuoteToSalesOrderResponse convertQuoteResponse =
(ConvertQuoteToSalesOrderResponse)_serviceProxy.Execute(convertQuoteRequest);
SalesOrder salesOrder = (SalesOrder)convertQuoteResponse.Entity;
Guid _salesOrderId = salesOrder.Id;
Guid _priceListItemId = new Guid("eebb6e3f-ccfe-4d84-90d9-421418b03a8e");
// Retrieve current price list
ProductPriceLevel priceListItem =
(ProductPriceLevel)_serviceProxy.Retrieve(ProductPriceLevel.EntityLogicalName,
_priceListItemId, new ColumnSet("productpricelevelid", "amount"));
// Update the price list
priceListItem.Amount = new Money(30.0M);
UpdateRequest updatePriceListItem = new UpdateRequest()
{
Target = priceListItem,
};
_serviceProxy.Execute(updatePriceListItem);
// Retrieve the order
SalesOrder updatedSalesOrder = (SalesOrder)_serviceProxy.Retrieve(
SalesOrder.EntityLogicalName,_salesOrderId,new ColumnSet("salesorderid", "totalamount")
);
// Unlock the order pricing
UnlockSalesOrderPricingRequest unlockOrderRequest =new UnlockSalesOrderPricingRequest()
{
SalesOrderId = _salesOrderId
};
_serviceProxy.Execute(unlockOrderRequest);
// Retrieve the order
updatedSalesOrder = (SalesOrder)_serviceProxy.Retrieve( SalesOrder.EntityLogicalName,
_salesOrderId, new ColumnSet("salesorderid", "totalamount") );
// Relock the order pricing
LockSalesOrderPricingRequest lockOrderRequest = new LockSalesOrderPricingRequest()
{
SalesOrderId = _salesOrderId
};
_serviceProxy.Execute(lockOrderRequest);
// Define columns to be retrieved after creating the invoice
ColumnSet invoiceColumns = new ColumnSet("invoiceid", "totalamount");
// Convert the order to an invoice
ConvertSalesOrderToInvoiceRequest convertOrderRequest = new ConvertSalesOrderToInvoiceRequest()
{
SalesOrderId = _salesOrderId,
ColumnSet = invoiceColumns
};
ConvertSalesOrderToInvoiceResponse convertOrderResponse =
(ConvertSalesOrderToInvoiceResponse)_serviceProxy.Execute(convertOrderRequest);
Invoice invoice = (Invoice)convertOrderResponse.Entity;
Guid _invoiceId = invoice.Id;
// Retrieve current price list
priceListItem = (ProductPriceLevel)_serviceProxy.Retrieve(ProductPriceLevel.EntityLogicalName,
_priceListItemId,new ColumnSet("productpricelevelid", "amount"));
// Lock the invoice pricing
LockInvoicePricingRequest lockInvoiceRequest = new LockInvoicePricingRequest()
{
InvoiceId = _invoiceId
};
_serviceProxy.Execute(lockInvoiceRequest);
// Update the price list
priceListItem.Amount = new Money(40.0M);
updatePriceListItem = new UpdateRequest()
{
Target = priceListItem
};
_serviceProxy.Execute(updatePriceListItem);
// Retrieve the invoice
Invoice updatedInvoice = (Invoice)_serviceProxy.Retrieve(
Invoice.EntityLogicalName,
_invoiceId, new ColumnSet("invoiceid", "totalamount"));
// Unlock the invoice pricing
UnlockInvoicePricingRequest unlockInvoiceRequest = new UnlockInvoicePricingRequest()
{
InvoiceId = _invoiceId
};
_serviceProxy.Execute(unlockInvoiceRequest);
// Retrieve the invoice
updatedInvoice = (Invoice)_serviceProxy.Retrieve(Invoice.EntityLogicalName,
_invoiceId,
new ColumnSet("invoiceid", "totalamount"));
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.