locked
How to add a Opportunity Product to an Opportunity via Custom Workflow Activity RRS feed

  • Question

  • I'm trying to add an existing Product as an Opportunity Product on an Opportunity, but I seem to be having trouble. What follows is a snippet of code that hopefully illustrates my approach. I've registered the Custom Workflow Activity and I've verified that I'm returning the correct Product and Opportunity Entities but the Opportunity Product does not show on the Opportunity. What is required to identify a Product with an Opportunity?

    .

    .

    .

    //return the Product Entity via fetch

    EntityCollection productRetrieve = service.RetrieveMultiple(new FetchExpression(fetch));
    Entity prodAnnualSupport = productRetrieve.Entities[0];

    //Retrieve the Opportunity record from the opportunity "name"

    QueryByAttribute oppQuery = new QueryByAttribute {

         EntityName = "opportunity",

         ColumnSet = new ColumnSet("name", "description", "opportunityid")
         };

    oppQuery.AddAttributeValue("name", oppTopic.Get<string>(context));

    EntityCollection oppResults = service.RetrieveMultiple(oppQuery);
    Entity oppAnnualSupport = oppResults.Entities[0];

    //Debug code: update the description field
    oppAnnualSupport["description"] = prodAnnualSupport["name"].ToString();
    service.Update(oppAnnualSupport);

               
    //Create the Opportunity Product Line and associate with the Opportunity
    OpportunityProduct newOpportunityProduct = new OpportunityProduct();
    newOpportunityProduct.ProductId = new EntityReference(Product.EntityLogicalName, prodAnnualSupport.Id);
    newOpportunityProduct.OpportunityId = new EntityReference(Opportunity.EntityLogicalName, oppAnnualSupport.Id);
    newOpportunityProduct.Quantity = (decimal) 1.000;

    Guid oppProdId = service.Create(newOpportunityProduct);

     

    Wednesday, May 28, 2014 8:17 PM