问题
I have an existing opportunity with some menu items as shown in the below image.
I want to programatically add the product using a windows forms application.The existing product was added using Dynamics Online Portal. But, I need to do the same via c#.
After searching in the internet, I found out that we can achieve this by using Related Enitities & calling Service.Execute(Request).
So first, I found out the entity names of the required.As I found the entity name for Motor Products, I used Service.Create(entity) to add the product. The product was added but it wasn't showing under this opportunity as I didn't pass the reference for this opportunity.
Then, I tried to find the relation between Opportunity and Product Line Items but I couldn't find any common unique attribute (ex: opportunityid is not there in Product Line Items). Ultimately, the application was throwing errors.
Is there any way we can add the product in the Motor Products programatically?
回答1:
The grid you're seeing under Motor Products is not showing Products related to the Opportunity, but Opportunity Products.
Opportunity Product is a different entity, and acts as an intersection between Opportunities and their associated Products.
Opportunity Products can be created programatically in C#. Here's how it might look:
var opportunityProduct = new Entity("opportunityproduct");
opportunityProduct["opportunityid"] = new EntityReference("opportunity", new Guid("oppId");
opportunityProduct["productid"] = new EntityReference("product", new Guid("productId");
...
var opportunityProductId = service.Create(opportunityProduct);
You were right to look for the opportunityid
field; it's not on the form but it is on the entity's schema. You can check the SDK metadata file for proof.
来源:https://stackoverflow.com/questions/45283483/how-do-i-add-items-in-the-sub-menu-options-through-c-sharp-dynamics-crm-online