Monday, March 5, 2012

Create a Plug Ins With Pre Method in 4.0.

Example: Change the quotenumber in the Pre-Stage for the Create of a Quote.

The quotenumber is a field that the CRM web service will not allow you to change after it is written to the database so you need to use the Pre-Stage and modify the context Entity before the end of the Execute method as shown below.


Create a Class Library and write as below


namespace CRMPlugIn
{
    public class PreClass:IPlugin
    {
        #region IPlugin Members
        public void Execute(IPluginExecutionContext context)
        {
            var entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

            // . . . retrieve data to populate number . . .

            // Set value before it is written to the database

            if (entity.Properties.Contains("quotenumber"))
            {
                entity.Properties.Remove("quotenumber");
            }
            Random r = new Random();
            var prop = new StringProperty("quotenumber", "Q"+r.Next(1,1000));
            entity.Properties.Add(prop);
        }
        #endregion

    }
}

This example will create a Quote Number whatever Format You want.....

No comments:

Post a Comment