web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Plugin code to auto populate fields from opportunity to quote

(0) ShareShare
ReportReport
Posted on by 30

Hi!

I have created a ribbon button to create a quote from opportunity. With java script code, I had auto populated pricelist field.

I want to auto populate the products from opportunity to quote with the help of plugin.

Can someone please help with the c# code to add products from opportunity. It will be great if I get help as I am new to crm.

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Check this post - mileyja.blogspot.com/.../copy-all-products-from-any-opportunity.html

  • HarshReddy Profile Picture
    30 on at

    Hi!

    This code will work only to one particular record as they have used GUID.

    But for me, it should work every time I create a sales process. 

    Could you help me with this requirement?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I believe you will be able to get the Id of your current opportunity so you will be able to use this code with Id of the opportunity that is used in your process.

    Good luck.

  • HarshReddy Profile Picture
    30 on at

    Hi!

    I have created a ribbon button to create quote from opportunity after adding products. I want to auto populate  products using plug in.

    Here's the which I have used

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    namespace MyPlugin
    {
    public class ProdPlugin : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    //Extract the tracing service for use in debugging sandboxed plug-ins.
    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    // Obtain the execution context from the service provider.
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));

    // Obtain the organization service reference.
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    Entity entity = (Entity)context.InputParameters["Target"];
    if (entity.LogicalName == "quote")
    {
    if (context.PostEntityImages.Contains("PostImage"))
    {
    // Here is your Quote entity data
    Entity quote = (Entity)context.PostEntityImages["PostImage"];

    try
    {
    QueryExpression query = new QueryExpression
    {
    EntityName = "quotedeatailsGrid",
    ColumnSet = new ColumnSet(),
    Criteria = new FilterExpression
    {
    Conditions =
    {
    new ConditionExpression
    {
    AttributeName = "products",
    Operator = ConditionOperator.Equal,
    Values = { (Guid)entity.Id }
    }
    }
    }
    };
    // Here is your Quote Product(s) data
    EntityCollection prodList = service.RetrieveMultiple(query);
    if (prodList.Entities.Count > 0)
    {
    foreach (Entity quoteProd in prodList.Entities)
    {
    var quoteprodId = quoteProd.Id;
    //etc
    }
    }

    if (quote.Attributes.Contains("opportunityid"))
    {
    // Here is your Opportunity entity data
    Entity opportunity =
    service.Retrieve("opportunity", ((EntityReference)quote["opportunityid"]).Id, new ColumnSet(true));
    }
    }
    catch (Exception ex)
    {
    }
    }
    }
    }
    }
    }
    }

    But I am getting error when I am excuting.

    Can someone please help by telling where the code is wrong as I am new to coding.

    Thank you!

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    First thing - please, don't create threads that ask the same question.

    Second. I see a few issues in your code and the first is the name of the entity you try to query - quotedeatailsGrid - there is no such entity. There is quotedetail.

    Another issue is the name of the attribute you use in the condition - products. There is no such attribute available.

    But the main question I have is the following - I'm not fully confident what you want your code to do. You're trying to query quote products when the quote is not created yet.

    I believe that the valid idea of the code is following:

    1. Get the opportunityid from the target.

    2. Build the query for opportunityproduct entity to query products.

    3. Loop through the collection you got and create quote detail based on the opportunity product.

    That looks more correct for me but I don't understand why do you want to go this path instead of using the message I left the reference to in one of my replies.

    Good luck.

  • ReddyM Profile Picture
    5 on at

    Hi cloflyMao

    I have created a ribbon button "Create Quote" to create quote from opportunity. Using java script, I have populated the pricelist.

    On creation of Quote I need to write one Plugin to populate Quote products from Opportunity products.
    As I am new to coding, I am unable to complete this task. Could you please help me?
    Thank you!
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Dear ReddyM,

    Please don't create duplicated threads. If you have the need just update your previous thread with your update.

  • HarshReddy Profile Picture
    30 on at

    Hi!

    I am new to CRM and I have no knowledge on coding.

    Can someone please help me how to get the opportunity id using plugin code?

    Thank you in advance.

  • Suggested answer
    Prakash Kalimuthu Profile Picture
    on at

    Hi HarshReddy,

    To get target entity through plugin.

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

    Guid recordID = entity.Id;

    For basic plugin, refer below url

    www.tutorialspoint.com/.../microsoft_crm_plugins.htm

    Regards,

    Prakash

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Please, stop creating new and new threads. I moderate forums here and I will see it.

    As I mentioned before - if you have updates/new questions to your initial thread - don't create a new one but post your question as an update to your initial thread.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans