locked
Quote Address Populate RRS feed

  • General discussion

  • The way CRM get address information to populate fields in a quote, order or invoice can be confusing.  To have a database with 1 to many relationship to addresses is not your typical layout.  I do appreciate the way Microsoft thought of this, but it does come with some confusion.

    To enable our sales people to create quotes slightly faster, I created a plugin using C# with mainly to capabilities:

    1.  When a quote is created populate the billto and shipto address fields from the potential customer (always account with our company)

    2.  Many companies have only one address so it make sense to populate with addresses with only one address if the company only have address.

    Just thought i'd share my code to maybe make someones life out there a little easier ;-)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    
    /* *
     * CRM Plugin
     * Christo Vermeulen
     * 15/05/2014
     * 
     * 
     * Added functionality for the quote when it is created
     * 
     * 1. Populate the qoute address details acording to the customer selected
     */
    
    namespace QuoteOnCreate
    {
        public class OnCreate : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                tracer.Trace("Start IPlugin.execute");
                Entity Quote = null;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Quote = (Entity)context.InputParameters["Target"];
                    if (Quote.LogicalName != "quote")
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
                try
                {
                    #region execute
                    IOrganizationService _orgService = factory.CreateOrganizationService(context.UserId);
                    /* *
                     * Get the account id from potential customer and set the address details
                     * according to the account details
                     */
                    var CustomerReferenceValue = Quote.GetAttributeValue<EntityReference>("customerid");
                    ColumnSet attributes = new ColumnSet(new string[] { "address1_line1",
                                                                          "address1_line2",
                                                                          "address1_line3",
                                                                          "address1_city",
                                                                          "address1_stateorprovince",
                                                                          "address1_postalcode",
                                                                          "address1_country",
                                                                          "address2_line1",
                                                                          "address2_line2",
                                                                          "address2_line3",
                                                                          "address2_city",
                                                                          "address2_stateorprovince",
                                                                          "address2_postalcode",
                                                                          "address2_country"});
                    Entity Customer = _orgService.Retrieve("account", CustomerReferenceValue.Id, attributes);
                    /* * Address layout Mapping          
                     * 
                     * Field                | Account                     | Quote                     | 
                     * =====================|=============================|===========================|
                     * * * Bill To * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                     * Street 1             | address1_line1              | billto_line1              |
                     * Street 2             | address1_line2              | billto_line2              |
                     * Street 3             | address1_line3              | billto_line3              |
                     * City                 | address1_city               | billto_city               |
                     * State/Province       | address1_stateorprovince    | billto_stateorprovince    |
                     * Zip/Postal Code      | address1_postalcode         | billto_postalcode         |
                     * Country Reqion       | address1_country            | billto_country            |
                     * 
                     * * * * Ship To * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                     * Street 1             | address2_line1              | shipto_line1              |
                     * Street 2             | address2_line2              | shipto_line2              |
                     * Street 3             | address2_line3              | shipto_line3              |
                     * City                 | address2_city               | shipto_city               |
                     * State/Province       | address2_stateorprovince    | shipto_stateorprovince    |
                     * Zip/Postal Code      | address2_postalcode         | shipto_postalcode         |
                     * Country Reqion       | address2_country            | shipto_country            |
                     */
                    Quote["billto_line1"] = Customer.GetAttributeValue<string>("address1_line1");
                    Quote["billto_line2"] = Customer.GetAttributeValue<string>("address1_line2");
                    Quote["billto_line3"] = Customer.GetAttributeValue<string>("address1_line3");
                    Quote["billto_city"] = Customer.GetAttributeValue<string>("address1_city");
                    Quote["billto_stateorprovince"] = Customer.GetAttributeValue<string>("address1_stateorprovince");
                    Quote["billto_postalcode"] = Customer.GetAttributeValue<string>("address1_postalcode");
                    Quote["billto_country"] = Customer.GetAttributeValue<string>("address1_country");
                    /* *
                     * If the company only have one address the shipto and billto is the same
                     */
                    if (Customer.GetAttributeValue<string>("address2_line1") +
                        Customer.GetAttributeValue<string>("address2_line2") +
                        Customer.GetAttributeValue<string>("address2_line3") == "")
                    {
                        Quote["shipto_line1"] = Customer.GetAttributeValue<string>("address1_line1");
                        Quote["shipto_line2"] = Customer.GetAttributeValue<string>("address1_line2");
                        Quote["shipto_line3"] = Customer.GetAttributeValue<string>("address1_line3");
                        Quote["shipto_city"] = Customer.GetAttributeValue<string>("address1_city");
                        Quote["shipto_stateorprovince"] = Customer.GetAttributeValue<string>("address1_stateorprovince");
                        Quote["shipto_postalcode"] = Customer.GetAttributeValue<string>("address1_postalcode");
                        Quote["shipto_country"] = Customer.GetAttributeValue<string>("address1_country");
                    }
                    else
                    {
                        Quote["shipto_line1"] = Customer.GetAttributeValue<string>("address2_line1");
                        Quote["shipto_line2"] = Customer.GetAttributeValue<string>("address2_line2");
                        Quote["shipto_line3"] = Customer.GetAttributeValue<string>("address2_line3");
                        Quote["shipto_city"] = Customer.GetAttributeValue<string>("address2_city");
                        Quote["shipto_stateorprovince"] = Customer.GetAttributeValue<string>("address2_stateorprovince");
                        Quote["shipto_postalcode"] = Customer.GetAttributeValue<string>("address2_postalcode");
                        Quote["shipto_country"] = Customer.GetAttributeValue<string>("address2_country");
                    }
                    _orgService.Update(Quote);
                    #endregion
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("ERROR:  " + ex.Message);
                }
            }
        }
    }
    

     

    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Thursday, May 15, 2014 9:35 AM