Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Highlighted
New Member
Posts: 8
Registered: ‎04-07-2011
Accepted Solution

creating a product via SData, how to set the productprogram?

[ Edited ]

Hi, I'm trying to create a product via SData, and setting the default productprogram in the same function.

The below function creates the product ok, but seems to ignore the productprogram.

Any help would be much appreciated. 

      

public static void createSlxProduct(string productName, string description, string actualID, string family, double price, double sellingprice)

        {

           try

            {

               var entry = newAtomEntry();

               var payload = newSDataPayload

                {

                    ResourceName ="Product",

                    Namespace ="http://schemas.sage.com/dynamic/2007",

                    Values = {

                        {"Name", productName},

                        {"Description", description},

                        {"ActualId", actualID},

                        {"Family", family},

                        {"Price", price},

                        {"ProductProgram", newSDataPayload{

                                Values = {

                                    {"Program","Retail"},

                                    {"DefaultProgram", "T"},

                                    {"Price", sellingprice}

                                }

                            }

                        }

                    }

                };

                entry.SetSDataPayload(payload);

               SDataService service = newSDataService(_url + "/slx/dynamic/-/", _user, _password);

               var request = newSDataSingleResourceRequest(service, entry) { ResourceKind = "Products" };

               AtomEntry result = request.Create();

            }

           catch (Exception ex)

            {

            }

        }

Highlighted
Copper Super Contributor
Posts: 23
Registered: ‎08-17-2009

Re: creating a product via SData, how to set the productprogram?

 

ProductProgram is a collection, so you need to do the following:

 

        public static void CreateSlxProduct(string productName, string description, string actualID, string family, double price, double sellingprice)
        {
           try
            {
               var entry = new AtomEntry();
               var payload = new SDataPayload
                {
                    ResourceName ="Product",
                    Namespace ="http://schemas.sage.com/dynamic/2007",
                    Values = {
                        {"Name", productName},
                        {"Description", description},
                        {"ActualId", actualID},
                        {"Family", family},
                        {"Price", price},
                        {"ProductProgram", 
                            new SDataPayloadCollection {                            
                                new SDataPayload{
                                    ResourceName = "ProductProgram",
                                    Values = {
                                        {"Program","Wholesale"},
                                        {"DefaultProgram", true},
                                        {"Price", sellingprice}
                                    }
                                }
                            }
                        }
                    }
                };

                entry.SetSDataPayload(payload);
                SDataService service = new SDataService("http://10.40.241.80/sdata/slx/dynamic/-/", "admin", "");
               var request = new SDataSingleResourceRequest(service, entry) { ResourceKind = "Products" };
               AtomEntry result = request.Create();
            }

 

Highlighted
New Member
Posts: 8
Registered: ‎04-07-2011

Re: creating a product via SData, how to set the productprogram?

Thanks for your help Mark.  It now does create a productprogram, however the newly created product now has 2 productprograms, one default (a completely different family to the one set), and the one created in the function.  It's the default productprogram that I would like to set a price on.  I tried the following, but that just throws an exception saying that "the given key was not present in the dictionary".

So should I create the product first, and then update the default productprogram afterwards? 

I was hoping that you could do this in one go.

       

publicstaticvoid createSlxProduct(string productName, string description, string actualID, string family, double price, double sellingprice)

        {

           try

            {

               var entry = newAtomEntry();

               var payload = newSDataPayload

                {

                    ResourceName ="Product",

                    Namespace ="http://schemas.sage.com/dynamic/2007",

                    Values = {

                        {"Name", productName},

                        {"Description", description},

                        {"ActualId", actualID},

                        {"Family", family},

                        {"Price", price}

                       //{"ProductProgram",

                       //    new SDataPayloadCollection {                           

                       //        new SDataPayload{

                       //            ResourceName = "ProductProgram",

                       //            Values = {

                       //                {"Program","Wholesale"},

                       //                {"DefaultProgram", true},

                       //                {"Price", sellingprice}

                       //            }

                       //        }

                       //    }

                       //}

                    }

                };

               var productprograms = (SDataPayloadCollection)payload.Values["ProductProgram"];

               foreach (SDataPayload productprogram in productprograms)

                {

                   var newprice = productprogram.Values["Price"] = sellingprice;

                }

                entry.SetSDataPayload(payload);

               SDataService service = newSDataService(_url + "/slx/dynamic/-/", _user, _password);

               var request = newSDataSingleResourceRequest(service, entry) { ResourceKind = "Products" };

               AtomEntry result = request.Create();

            }

           catch (Exception ex)

            {

            }

        }

Highlighted
Copper Super Contributor
Posts: 23
Registered: ‎08-17-2009

Re: creating a product via SData, how to set the productprogram?

I see what you're talking about.  I'm not certain whether it can be done in one statement, but for now I had to put the following after AtomEntry result = request.Create();:

 

 

payload = entry.GetSDataPayload();

request.Entry = entry;

request.Include ="ProductProgram";

entry = request.Read();

payload = entry.GetSDataPayload();

var nestedPayloadCollection = payload.Values["ProductProgram"] asSDataPayloadCollection;

 SDataPayloadnestedPayload = nestedPayloadCollection[0];

nestedPayload.Values["Price"] = price;

entry.SetSDataPayload(payload);

request.Entry = entry;

request.Update();

Highlighted
New Member
Posts: 8
Registered: ‎04-07-2011

Re: creating a product via SData, how to set the productprogram?

Thanks Mark. This does the trick!
(it's a shame it can't be done in one go, but so be it)
Thanks for your help.
Highlighted
Copper Super Contributor
Posts: 23
Registered: ‎08-17-2009

Re: creating a product via SData, how to set the productprogram?

Glad to help - I'll follow up here if I learn of a way to do it with one statement.
Highlighted
New Member
Posts: 8
Registered: ‎04-07-2011

Re: creating a product via SData, how to set the productprogram?

Cheers Mark.

 

the final function looks like this:

       

publicstaticvoid createSlxProduct(string productName, string description, string actualID, string family, double price, double sellingprice)

        {

           try

            {

               var entry = newAtomEntry();

               var payload = newSDataPayload

                {

                    ResourceName ="Product",

                    Namespace ="http://schemas.sage.com/dynamic/2007",

                    Values = {

                        {"Name", productName},

                        {"Description", description},

                        {"ActualId", actualID},

                        {"Family", family},

                        {"Price", price}

                    }

                };

                entry.SetSDataPayload(payload);

               SDataService service = newSDataService(_url + "/slx/dynamic/-/", _user, _password);

               var request = newSDataSingleResourceRequest(service, entry) { ResourceKind = "Products" };

               AtomEntry result = request.Create();

               string uri = result.Id.Uri.AbsoluteUri;

                payload = result.GetSDataPayload();

                request.Entry = result;

                request.Include ="ProductProgram";

                entry = request.Read();

                payload = entry.GetSDataPayload();

               var nestedPayloadCollection = payload.Values["ProductProgram"] as SDataPayloadCollection;

               SDataPayload nestedPayload = nestedPayloadCollection[0];

                nestedPayload.Values["Price"] = sellingprice;

                entry.SetSDataPayload(payload);

                request.Entry = entry;

                request.Update();

            }

           catch (Exception ex)

            {

               Console.WriteLine(ex.Message);

            }

        }