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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin code to auto populate fields from opportunity to quote

    I believe your main issue is getting the opportunity id when the quote is created.

    Here is an approximate code you can use:

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

    var opportunityRef = entity.GetAttributeValue<EntityReference>("opportunityid");

    if (opportunityRef == null) {

    //Do whatever you need when there is no parent opportunity

    } else {

    var oppId = opportunityRef.Id;

    //Do whatever you need with opportunityid

    }

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin code to auto populate fields from opportunity to quote

    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.

  • Suggested answer
    RE: How to get opportunity id from target

    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

  • HarshReddy Profile Picture
    HarshReddy 30 on at
    How to get opportunity id from target

    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.

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin code to auto populate fields from opportunity to quote

    Dear ReddyM,

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

  • ReddyM Profile Picture
    ReddyM 5 on at
    To auto populate products from opportunity to quote using plugin

    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!
  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin code to auto populate fields from opportunity to quote

    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.

  • HarshReddy Profile Picture
    HarshReddy 30 on at
    Populating products from opportunity to quote

    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
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin code to auto populate fields from opportunity to quote

    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
    HarshReddy 30 on at
    RE: Plugin code to auto populate fields from opportunity to quote

    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?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans