locked
Add opportunity product using REST RRS feed

  • Question

  • Hi!

    I'm creating a webresource for adding a some default products to an opportunity. I've gone through the example here, but is still unsure of what properties to set on the entity to create.

        // Retrive the oppertunity, just to make sure something works....
        SDK.REST.retrieveRecord(
            opportunityId,
            "Opportunity",
            null, null,
            function (opportunity) {
                console.log("Retrieved the opportunity:" + opportunity.Name + ".", opportunity, opportunityId);
    
                // Now I want to add a new opportunityProduct with the productId given below..
                var toAdd = {};
         
                toAdd.ProductId = "e171ce8e-1ef3-e211-b252-984be17c47e4";
                toAdd.Quantity = 123;
                toAdd.ProductDescription = "Added from code - TEST123";
                toAdd.OpportunityId = opportunity.OpportunityId;
    
                SDK.REST.createRecord(
                    toAdd,
                    "OpportunityProduct",
                    function (added) {
                        console.log("The OpportunityProduct was added:  YES!", added);
                    },
                    errorHandler
                  );
            },
            errorHandler
      );

    So, as you can see from my example, I have the productId of the product to add, and I have the opportunityId of the opportunity that the product should be added to. But to get the toAdd object correct..... The error I get is:

    Error processing request stream. The request should be a valid top-level resource

    I be really happy if someone could point me in the right direction. And maybe also give me some links to a document explaing this. (maybe I need a google crash course :-) )

    Thanks for any help

    Regards

    Larsi


    • Edited by Larsi Monday, January 20, 2014 1:22 PM Updates
    Monday, January 20, 2014 12:18 PM

Answers

  • The object you are creating should look something like this to relate an existing Product to an Opportunity (you need UOM):

    var entity = {};
    entity.Quantity = parseFloat(4).toFixed(2);
    entity.OpportunityId = {
        Id: '69BB2236-B57F-E311-BB6D-6C3BE5A881A4',
        LogicalName: 'opportunity'
    };
    entity.ProductId = {
        Id: 'C8138483-DF81-E311-B542-6C3BE5A8362C',
        LogicalName: 'product'
    };
    entity.UoMId = {
        Id: 'BE0FB859-7E90-4B3E-B501-3AB3CD4DC8FC',
        LogicalName: 'uom'
    };


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, January 20, 2014 2:59 PM
    Moderator