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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Get text value from opportunity to Opportunity product, code or no code

(0) ShareShare
ReportReport
Posted on by

Hi.

I need to get the value of a text field from the opportunity to opportunity product. I'm use D365 
I tried to make a plugin but it turns out that it does not throw any errors and does not copy the value of the text field

the field name in opportunity is: bzg_companyname
the field name in opportuniryproduct is: bzg_companyname

this is the code

public void Execute(IServiceProvider serviceProvider)
        {
            
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            Entity Opportunity = new Entity("opportunity");
            if ((context.InputParameters.Contains("Target")) && (context.InputParameters["Target"] is Entity))
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "oportunityproduct")
                    return;
                try
                {
                   if (entity.Contains("opportunityid"))
                   {
                       Opportunity.Id = ((EntityReference)entity["opportunityid"]).Id;
                       Opportunity["bzg_companynameforurl"] = entity.Contains("bzg_companynameforurl") ? entity["bzg_companynameforurl"].ToString() : string.Empty;
                       service.Update(entity);
                   }
                }
                catch (InvalidPluginExecutionException ex)
                {
                    throw ex;
                }

             }
        }

 Please, I'm not a full developer, so can you suggest me any solution, could be a plugin or not

thanks a lot

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Get text value from opportunity to Opportunity product, code or no code

    Hi Pravin, thanks for your time

    The relationship mapping exist, but doesn't work. but maybe I could try with your code reference.

    thanks

  • Community Member Profile Picture
    on at
    RE: Get text value from opportunity to Opportunity product, code or no code

    Hi Ravi, thanks for your time

    Ok, the thing is, in the opportunity record, I add the opp product  from the grid like an existing product. when the product is added in the grid, I open the opp product record and should be the text field copy in the opp product.

    in fact, the relationship mapping exist, but doesn't work

  • Verified answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: Get text value from opportunity to Opportunity product, code or no code

    Hi ,

    It depends upon how you creating record of opportunity product.

    1. If you are creating opportunity product manually from subgrid or associated view then mapping fields will work.

    Please refer below url for reference:

    [View:https://www.marksgroup.net/blog/dynamics-365-mapping-fields/:750:50

    2. In other hand if you are creating opportunity products using JS/workflow/plugin/console or using any other third application then create pre-create operation plugin on opportunity product.

    This case you need not to update record.

    Please refer below code for reference:

     Guid opportunityId = opportunityproduct.Attributes.Contains("opportunitylookuplogicalnamehere") ? opportunityproduct.GetAttributeValue("opportunitylookuplogicalnamehere").Id : Guid.Empty;
                        Entity opportunity = service.Retrieve("opportunity", opportunityId, new ColumnSet("bzg_companyname"));
                        string bzgcompanyname = opportunity.Attributes.Contains("bzg_companyname") ? (string)opportunity.Attributes["bzg_companyname"] : string.Empty;
    
                        opportunityproduct.Attributes["bzg_companyname"] = bzgcompanyname;
                        context.InputParameters["Target"] = opportunityproduct;

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Get text value from opportunity to Opportunity product, code or no code

    Hi,

    Do you need to copy From "Opportunity" to "Opportunity Product" or from "Opportunity Product" to Opportunity"?

    In your question you have mentioned From "Opportunity" to "Opportunity Product"  whereas the plugin you have written is on thecoded to trigger on "Opportunity Product" entity.

    Either way, you don't need plugin.

    From "Opportunity" to "Opportunity Product" : Use out of box mapping

    From "Opportunity Product" to Opportunity": Use real time workflow

    Hope this helps.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 167

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 116 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans