Pre Create Plugin Works when manually creating invoices but when sales order creates it

Pre Create Plugin Works when manually creating invoices but when sales order creates it

Post by JPS » Sat, 09 May 2009 18:50:35


have a simple plugin that added the next available number to the
lineitemnumber attribute of the invoicedetails entity. This is to allow us
to generate auto increment numbers which we store in custom entity.

I have registered the plugin and create a new step for create message of
invoicedetails entity to fire this

pre create
synchronous
server
parent pipeline

The code works fine when I manually add items to the invoice and the value
to the lineitemnumber attribute.

When I create an invoice from the sales order by using the create invoice
button the pre create of invoice details is not firing.

Does someone know why? The code is below.

Another issue is this works fine when I place the attribute lineitemnumber
on the form. If I remove it from the form it does not work i.e. the
lineitemnumber property is not updated?

Many thanks in advance for your help
using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

using Microsoft.Crm.Sdk.Query;



namespace Microsoft.Crm.Sdk.InvoiceProductAutoIncrement

{

public class InvoiceProductAutoIncrement: IPlugin

{

/// <summary>

/// This plugin automatically sets the line item number of the invoice
product.

/// Written by Jaspinder Singh @ IMG 04th May 2009

/// </summary>

public void Execute(IPluginExecutionContext context)

{


// Verify we have an entity to work with

if (context.InputParameters.Properties.Contains("Target") &&

context.InputParameters.Properties["Target"] is DynamicEntity)

{

// Obtain the target business entity from the input parmameters.

DynamicEntity entity =
(DynamicEntity)context.InputParameters.Properties["Target"];


// Verify that the entity represents an account.

if (entity.Name == EntityName.invoicedetail.ToString())

{

// If Account Number was not set, generate a new Account Number

// Use "contains" because the indexer will throw if the column is not found

if (entity.Properties.Contains("lineitemnumber") == false)

{

ICrmService _crmService = context.CreateCrmService(true);

Guid myguid = new Guid("B20D0D71-F433-DE11-B283-005056957839");

TargetRetrieveDynamic target = new TargetRetrieveDynamic();

target.EntityName = "ce_counter";

target.EntityId = myguid; // what should go here?


RetrieveRequest requestaccount = new RetrieveRequest();

requestaccount.Target = target;

requestaccount.ColumnSet = new AllColumns();

requestaccount.ReturnDynamicEntities = true;

RetrieveResponse result =
(RetrieveResponse)_crmService.Execute(requestaccount);

DynamicEntity ce_counter = (DynamicEntity)result.BusinessEntity;

//Guid contactId = (Guid)ce_counter.Properties["ce_counterid"];

Key contactIdAttr = (Key)ce_counter.Properties["ce_counterid"];

Guid contactId = contactIdAttr.Value;

CrmNumber nextnumber = new CrmNumber();

nextnumber = (CrmNumber)ce_counter.Properties["ce_nextnumber"];

int nextNumberValue = nextnumber.Value;

CrmNumber incrementBy = new CrmNumber();

incrementBy = (CrmNumber)ce_counter.Properties["ce_incrementby"];

int incrementValue = incrementBy.Value;


string accnumber = nextnumber.Value.ToString();

nextNumberValue = nextNumberValue + incrementValue;

nextnumber.Value = nextNumberValue;

ce_counter.Properties["ce_nextnumber"] = nextnumber;


_crmService.Update(ce_counter);

// Random rndgen = new Random
 
 
 

Pre Create Plugin Works when manually creating invoices but when sales order creates it

Post by Uma Mahesw » Wed, 13 May 2009 01:08:09

his is a multi-part message in MIME format.


Why the plugin is not firing when you create an invoice from sales order:

"Creating invoice from salesorder" internally uses a different message than
create invoice. This is why the plugin registered on parent pipeline of
create invoice, it is not getting trigerred.
Soultion is to register plugin in child pipeline of create invoice. In that
case, the plugin will be triggered for both your scenarios:
1. Creating invoice from salesorder
2. Creating an invoice by itself.

Why lineitemnumber property is not updated?

After the line "entity.Properties.Add(lineitemnumber);", try adding the following line:

context.InputParameters[ParameterName.Target] = entity;


Thanks,
Uma Maheswari Anbazhagan [MSFT] ,
Dynamics CRM, Microsoft.



"JPS" < XXXX@XXXXX.COM > wrote in message news:% XXXX@XXXXX.COM ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6001.18226" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2><STRONG><U>Why the plugin is not firing  when
you create an invoice from sales order: </U></STRONG></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>"Creating invoice from salesorder" internally uses
a different message than <BR>create invoice. This is why the plugin registered
on parent pipeline of <BR>create invoice, it is not getting
trigerred.<BR>Soultion is to register plugin in child pipeline of create
invoice. In that <BR>case, the plugin will be triggered for both your
scenarios:<BR>1. Creating invoice from salesorder<BR>2. Creating an invoice by
itself.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><STRONG><U>Why lineitemnumber property is not
updated?</U></STRONG></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>After the
line "entity.Properties.Add(lineitemnumber);", try adding the following
line:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>context.InputParameters[ParameterName.Target] =
entity;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks,</FONT></DIV>
<DIV><FONT face=Arial size=2>Uma Maheswari Anbazhagan [MSFT] ,</FONT></DIV>
<DIV><FONT face=Arial size=2>Dynamics CRM, </FONT><FONT face=Arial
size=2>Microsoft.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>"JPS" <</FONT><A
href="mailto: XXXX@XXXXX.COM "><FONT face=Arial
s