CRUD Operations

OData allows us to update entities via POST/PUT/DELETE methods. However, PDS allows updates only through three pre-defined Service Operations (i.e., “Create”, “Save” and “Delete”). If the pre-defined Service Operations do not work for you, you may have to develop your own “Custom Service Operation” to meet your needs.

 

The helper classes/methods in previous section already include the functionality of calling “Create”, “Save”, and “Delete” respectively (for both authenticated/anonymous access).

Code Samples

1.    Following is a code sample which creates and saves customer activity using “Create” Service Operation (you can execute this method using previous project created in previous section):

        public void CreateCusActivityTest()

        {

            try

            {

                //Create a new CusActivity instance (from server to get default values)

                CusActivity respCreate = SvcClient.Create<CusActivity>();

 

                //Provide values and Save

                respCreate.MasterCustomerId = "LKPSIERRA";

                respCreate.SubCustomerId = 0;

                respCreate.ActivityDate = DateTime.Now;

                respCreate.ActivityText = "This is sample text and should be counting " + DateTime.Now.Ticks;

                respCreate.StaffUserId = "ADMIN";

                respCreate.Subsystem = "MRM";

                respCreate.ActivityCode = "COMMUNICATION";

                respCreate.CallTopicCode = "ADDRESS";

                respCreate.CallTopicSubcode = "CHANGE";

                respCreate.CallTypeCode = "EMAIL-INBOUND";

 

                CusActivity respSave = SvcClient.Save<CusActivity>(respCreate);

                if (respSave.CustomerActivityId > 0)

                    Console.WriteLine("Created Activity with ID: " + respSave.CustomerActivityId.ToString());

                else

                    Console.WriteLine("Some problem to create activity");

 

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

 

            Console.ReadLine();

        }

    }

 

2.    Following is a code sample which fetches, modifies (only the first row), and saves customer activity using “Save” Service Operation (you can execute this method using previous project created in previous section):

 

        public void FetchActivitiesAndSave()

        {

            try

            {

                var q = SvcClient.Ctxt.CusActivities.Where(p => p.MasterCustomerId == "LKPSIERRA").Select(o => o);

                DataServiceCollection<CusActivity> oCusActivities = new DataServiceCollection<CusActivity>(q);

 

                if (oCusActivities.Count > 1)

                {

                    Console.WriteLine("No Activities exist");

                    return;

                }

 

                CusActivity oCusActivityToSave = oCusActivities[0]; //modify the first activity

                oCusActivityToSave.ActivityText = "This is sample text and should be counting " + DateTime.Now.Ticks;

 

                CusActivity oRespSave = SvcClient.Save<CusActivity>(oCusActivityToSave);

 

                if(oRespSave.CustomerActivityId > 0)

                    Console.WriteLine("Saved Successfully");

 

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

 

            Console.ReadLine();

        }

 

3.    Following is a snippet which tries to perform all CRUD operations in a simple function:

 

        public void DeleteCusActivityTest()

        {

            try

            {

                //Create a new CusActivity instance (from server to get default values)

                CusActivity oRespCreate = SvcClient.Create<CusActivity>();

if (oRespCreate.CustomerActivityId > 0)

                    Console.WriteLine("Created Activity with ID: " + respSave.CustomerActivityId.ToString());

                Else {

                    Console.WriteLine("Some problem to create activity");return; }

  

                 //Provide values

                 oRespCreate.MasterCustomerId = "JEFFERSON";

                 oRespCreate.SubCustomerId = 0;

                 oRespCreate.ActivityDate = DateTime.Now;

                 oRespCreate.ActivityText = "This is sample text and should be counting " + DateTime.Now.Ticks;

                 oRespCreate.StaffUserId = "ADMIN";

                 oRespCreate.Subsystem = "MRM";

                 oRespCreate.ActivityCode = "COMMUNICATION";

                 oRespCreate.CallTopicCode = "ADDRESS";

                 oRespCreate.CallTopicSubcode = "CHANGE";

                 oRespCreate.CallTypeCode = "EMAIL-INBOUND";

  

                 //Save it

                 CusActivity oRespSave = SvcClient.Save<CusActivity>(oRespCreate);

                 if(oRespSave.CustomerActivityId > 0)

                     Console.WriteLine("Saved Successfully");

  

                 //Retrieve it

                 var lstCusActivities = SvcClient.Ctxt.CusActivities.Where(c => c.CustomerActivityId == oRespSave.CustomerActivityId).ToList();

                 var oReqCusActivity = lstCusActivities[0];

  

                 //Delete it

                 CusActivity oRespDelete = SvcClient.Delete<CusActivity>(oReqCusActivity);

                   //retrieving again (to make sure it is deleted)

                 var lstCusActivitiesTest = SvcClient.Ctxt.CusActivities.Where(c => c.CustomerActivityId == oRespSave.CustomerActivityId).ToList();

                  if(lstCusActivitiesTest.Count == 0)

                     Console.WriteLine("Deleted Successfully");

  

             }

             catch (Exception ex)

             {

                 Console.WriteLine(ex.Message);

             }

          Console.ReadLine();

         }

  

4.    Following is a sample (you may have to create entities/data accordingly) which executes a service operation anonymously (without any authentication details):

          [TestMethod]

         public void AnonymousCreateSimpleOrderTest()

         {

             try

             {

                 SimpleOrderParam op = new SimpleOrderParam()

                 {

                     MasterCustomerId = "JEFFERSON",

                     SubCustomerId = 0,

                     ProductId = 225

                 };

  

                 SimpleOrderParam oResp = SvcClient.PostAnonymous<SimpleOrderParam>("CreateSimpleOrder", op);

                 Assert.IsNotNull(oResp.OrderNumber);

  

             }

             catch (Exception ex)

             {

                 Assert.Fail(ex.Message);

             }

         }

 

 

5.    Following is a sample (you may have to create entities/data accordingly) which fetches data from OData anonymously:

 

        [TestMethod]

         public void AnonymousOdataGetTest()

         {

             try

             {

                 var q = SvcClient.CtxtAnonymous.Customers.Where(p => p.MasterCustomerId == "JEFFERSON").Select(o => o);

                 DataServiceCollection<Customer> oCustomers = new DataServiceCollection<Customer>(q);

                 if (oCustomers.Count > 0)

                 {

                     Assert.IsTrue(oCustomers[0].FirstName.StartsWith("Thomas"));

                     Assert.AreEqual("Jefferson", oCustomers[0].LastName);

                 }

                 else

                 {

                     Assert.Fail("Could not find JEFFERSON");

                 }

  

             }

             catch (Exception ex)

             {

                 Assert.Fail(ex.Message);

             }

         }

 

See also:

·            Creating Service Operations using Personify Data Services

·            Executing Service Operations (without code)

·            Executing Service Operations (using .NET client)