locked
creating opportunity with products RRS feed

  • Question

  • Hi,

    I only see opportunity product and don't see any collections of oppotunity product in API when creating an opportunity.  Is there any way we can create a oppotunity with multiple products in API?

    Thanks in advance,

    Daniel,

    Wednesday, December 15, 2010 5:17 PM

Answers

  • Hi Daniel,

    Yes, It is possible using workflow assembly and plugins. The advantage to using workflow assembly is that it would become a template whereas for plugins you will have to change the code. I am adding the code where I am creating a quote details you can change entity names in this code:-

    using System.Collections.Generic;
    using System.Text;
    using System;
    using System.Collections;
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.ComponentModel.Serialization;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    using System.Workflow.Activities.Rules;
    using System.Reflection;
    using Microsoft.Crm.Workflow;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    
    
    namespace AutoProductDetail
    {
      [CrmWorkflowActivity("Proposal Template Creation")]
    	public class AutoProductDetailWF : Activity
      {
       protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
       {
         IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
         IWorkflowContext context = contextService.Context;
    
         ICrmService crmService = context.CreateCrmService();
    
         DynamicEntity entity = new DynamicEntity();
         entity.Name = "quotedetail";
         entity.Properties = new PropertyCollection();
         if (entity.Properties.Contains("quoteid"))
         {
           entity.Properties.Add(new LookupProperty("quoteid", (Lookup)entity.Properties["oQuote"]));
           entity.Properties.Add(new LookupProperty("productid", (Lookup)entity.Properties["oProduct"]));
           entity.Properties.Add(new LookupProperty("uomid", (Lookup)entity.Properties["oUom"]));
           entity.Properties.Add(new CrmNumberProperty("quantity",(CrmNumber)entity.Properties["oQuantity"]));
         }
         TargetCreateDynamic targetCreate = new TargetCreateDynamic();
         targetCreate.Entity = entity;
    
         // Create the request object.
         CreateRequest create = new CreateRequest();
    
         // Set the properties of the request object.
         create.Target = targetCreate;
    
         // Execute the request.
         CreateResponse created = (CreateResponse)crmService.Execute(create);
    
    
    		 
         return base.Execute(executionContext);
       }
    
    		public static DependencyProperty quoteIdProperty =
    		 DependencyProperty.Register("quoteId",
         typeof(Lookup),
    		 typeof(AutoProductDetailWF));
    
    		[CrmInput("Quote")]
    		[CrmReferenceTarget("quote")]
    		public Lookup quoteId
    		{
    			get
    			{
    				return (Lookup)base.GetValue(quoteIdProperty);
    			}
    			set
    			{
    				base.SetValue(quoteIdProperty, value);
    			}
    		}
    
    		public static DependencyProperty productidProperty =
    		 DependencyProperty.Register("productid",
    		 typeof(Lookup),
    		 typeof(AutoProductDetailWF));
    
    		[CrmInput("Product")]
    		[CrmReferenceTarget("product")]
    		public Lookup productid
    		{
    			get
    			{
    				return (Lookup)base.GetValue(productidProperty);
    			}
    			set
    			{
    				base.SetValue(productidProperty, value);
    			}
    		}
    
    		public static DependencyProperty uomidProperty =
    		 DependencyProperty.Register("uomid",
    		 typeof(Lookup),
    		 typeof(AutoProductDetailWF));
    
    		[CrmInput("Unit")]
    		[CrmReferenceTarget("uom")]
    		public Lookup uomid
    		{
    			get
    			{
    				return (Lookup)base.GetValue(uomidProperty);
    			}
    			set
    			{
    				base.SetValue(uomidProperty, value);
    			}
    		}
    
    		public static DependencyProperty quantityProperty = 
    			DependencyProperty.Register("quantity", 
    			typeof(CrmDecimal),
    			typeof(AutoProductDetailWF));
    
    		[CrmInput("Quantity")]
    		public CrmNumber quantity
    		{
    			get
    			{
    				return (CrmNumber)base.GetValue(quantityProperty);
    			}
    			set
    			{
    				base.SetValue(quantityProperty, value);
    			}
    		}
    
    
    		public static DependencyProperty taxProperty =
    			DependencyProperty.Register("tax",
    			typeof(CrmMoney),
    			typeof(AutoProductDetailWF));
    
    		[CrmInput("Tax")]
    		public CrmMoney tax
    		{
    			get
    			{
    				return (CrmMoney)base.GetValue(taxProperty);
    			}
    			set
    			{
    				base.SetValue(taxProperty, value);
    			}
    		}
    
      }
    }

    Please let me know if you find any issue with this.

      


    Regards Faisal
    Wednesday, December 15, 2010 5:23 PM