Community
Showing results for 
Search instead for 
Do you mean 
Reply

Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Accepted Solution Solved
Copper Contributor
Posts: 10
Country: United Kingdom
Accepted Solution

Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Hi All,

 

I have been developing a tool that will allow the import of opportunities on mass.

I have successfully performed all tasks including user lookup, stage and process lookup, all data assignment etc.

 

I am struggling, however, to populate and assign an OpportunityProduct object to the newly created Opportunity Object.

 

I assume the code to do this will follow the following structure:

 

//create the Opportunity object

Opportunity newOpp = framework.Opportunities.CreateOpportunity();

 

//assign all the fields etc to the opportunity

newOpp.Fields[fieldName, true] = value;

 

I can create a product using

Product newProd = framework.Products.CreateProduct(name, item, price, cost);

 

I also know you update the associated OpportunityProducts of an Opportunity using the following

newOpp.UpdateProducts(new OpportunityProduct[] {productToAdd},new OpportunityProduct[] {}); 

 

I know there is an Product Manager and OpportunityProductManager

framework.Products

framework.Products.OpportunityProductManager

 

and an OpportunityProduct Constructor although I have no idea how to use the associated state parameter!!

public OpportunityProduct( 
   CustomSubEntity.CustomSubEntityInitializationState state

 

 I would appreciate any help in constructing a newOpportunityProduct and attaching it to an Oportunity.

 

Many Thanks,

 

Rob.

 

 


Accepted Solutions
Solution
Accepted by topic author RobNightingale
‎09-25-2015 03:20 AM
Copper Contributor
Posts: 10
Country: United Kingdom

Re: Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Tom, You Sir are a legend!

 

I have just got time to play with just the first section of your code and I used the following simple code:

 

Opportunity opp = framework.Opportunities.CreateOpportunity(); opp.Name = "ROBTEST"; opp.Update(); OpportunityProduct oppProd = framework.Products.OpportunityProductManager.CreateCustomEntity(); oppProd.Name = "ROBTESTPROD"; oppProd.Quantity = 2.00m; oppProd.Cost = 53.00m; oppProd.Price = 65.00m; oppProd.Discount = 0.00m; oppProd.SetOpportunities(framework.Opportunities.GetOpportunityAsOpportunityList(opp)); oppProd.Update();

 

I did receive errors at the point you stated "oppProd.Update" but the InnerException just stated that the various values for Cost, Quantity etc could not accept null values and therefore need explicity setting!

 

After editing this so that all the necessary variables are completed I can add whatever product I want!

 

It seems so simple when you look back!!!!!

 

Thanks again for your help.

 

Rob.

 

 

View solution in original post


All Replies
Copper Super Contributor
Posts: 59
Country: USA

Re: Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Hi Rob,

 

The method I use to add a product to an opportunity is:

 

newOpportunity.AddProduct("Test Name", "Test Item Number", 0D, 0D, 0, 0D)

 

Hope it helps

 

Hugo Vale
Datastream Custom Solutions
http://www.datastreamcs.com
Copper Contributor
Posts: 10
Country: United Kingdom

Re: Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Thanks for the reply but I have just checked and this method is not available on the ACT 2010 version of the SDK, I fear I may have an early beta version of the SDK as I downloaded it about 6 weeks ago and I am currently trying to chase down the development team as the download site seems to be broken.

 

Thanks for the reply though Hugovale, what SDK version /version of ACT are you using?

 

Rob.

Nickel Super Contributor
Posts: 441
Country: UK

Re: Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Products are a custom entity now... I have tried to get it to work for you but I always get an error on saving the product, maybe someone will be able to help; I have tried 2 ways, using data bindings and without...

 

 No data bindings:

 

'create the opp Dim opp As Act.Framework.Opportunities.Opportunity = ActApp.ActFramework.Opportunities.CreateOpportunity() opp.UpdateContacts(New Guid() {ActApp.ApplicationState.CurrentContact.ID}, Nothing) opp.Name = "test" opp.Update() 'create the product Dim product As Act.Framework.Opportunities.OpportunityProduct = _ ActApp.ActFramework.Products.OpportunityProductManager.CreateCustomEntity product.SetOpportunities(ActApp.ActFramework.Opportunities.GetOpportunityAsOpportunityList(opp)) 'set a name product.Name = "test" 'save the product product.Update() '<<<<-----error saving here

 

 

 

Data bindings:

 

'create the opp Dim opp As Act.Framework.Opportunities.Opportunity = ActApp.ActFramework.Opportunities.CreateOpportunity() opp.UpdateContacts(New Guid() {ActApp.ApplicationState.CurrentContact.ID}, Nothing) opp.Name = "test" opp.Update() 'get the current product list for the opp Dim plist As Act.Framework.CustomEntities.CustomEntityList(Of Act.Framework.Opportunities.OpportunityProduct) = _ opp.GetProducts(Nothing) 'add the fields For Each field As Act.Framework.ComponentModel.DBFieldDescriptor In ActApp.ActFramework.Products.OpportunityProductManager.GetFieldDescriptors() plist.FieldDescriptors.Add(field) Next 'create a binding source and bind to the product list Dim bindingsource As New BindingSource bindingsource.DataSource = plist 'create the product Dim product As Act.Framework.Opportunities.OpportunityProduct = _ bindingsource.AddNew() product.SetOpportunities(ActApp.ActFramework.Opportunities.GetOpportunityAsOpportunityList(opp)) 'set the contacts, companies and groups Try product.SetCompanies(opp.GetCompanies(Nothing)) Catch ex As Exception End Try Try product.SetGroups(opp.GetGroups(Nothing)) Catch ex As Exception End Try Try product.SetContacts(opp.GetContacts(Nothing)) Catch ex As Exception End Try product.Name = "test" 'save the product product.Update() '<<<--- error svaing product here

 

 Tom

 

Solution
Accepted by topic author RobNightingale
‎09-25-2015 03:20 AM
Copper Contributor
Posts: 10
Country: United Kingdom

Re: Assigning an OpportunityProduct to a new Opportunity prgrammatically using the SDK

Tom, You Sir are a legend!

 

I have just got time to play with just the first section of your code and I used the following simple code:

 

Opportunity opp = framework.Opportunities.CreateOpportunity(); opp.Name = "ROBTEST"; opp.Update(); OpportunityProduct oppProd = framework.Products.OpportunityProductManager.CreateCustomEntity(); oppProd.Name = "ROBTESTPROD"; oppProd.Quantity = 2.00m; oppProd.Cost = 53.00m; oppProd.Price = 65.00m; oppProd.Discount = 0.00m; oppProd.SetOpportunities(framework.Opportunities.GetOpportunityAsOpportunityList(opp)); oppProd.Update();

 

I did receive errors at the point you stated "oppProd.Update" but the InnerException just stated that the various values for Cost, Quantity etc could not accept null values and therefore need explicity setting!

 

After editing this so that all the necessary variables are completed I can add whatever product I want!

 

It seems so simple when you look back!!!!!

 

Thanks again for your help.

 

Rob.