REST, Unmodified Row, Modified Row, Etc - External App

when calling these, they should return defaulted value rows, with RowMod “A” already set.

I think I thought the trace shows what was submitted and the return values after.

@CSmith So, I made those changes and unfortunately I’m still getting successful status but no new row. I’m going to try it on the Epicor rest help site and see if that makes a difference somehow.

Ok, if you have a labor head, use that, if not, you call GetNewLaborHed.

That should return a dataset with a new laborHed, with RowMod “A”.

Make your adjustments, and call Update.

Then, for a new laborDtl, you call GetNewLaborDtlWithHdr with the appropriate data, and you should get back a laborDtl with the RowMod “A”.

Set your stuff and call Update.

From here on out, depending on the BO, you may need to have an unmodified and modified row “U” for your further actions.

current order of operations (only the important parts):


        public static void GetNewLaborHed1(LaborHed laborHed, LaborDTL laborDtl)
        {
            Console.WriteLine("Step 12:");
            Console.WriteLine("Create appropriate number of duplicate records for Kinetic");
            Console.WriteLine("");
            // Assemble data
            JArray laborHedArr = JArray.FromObject(new[] { laborHed, laborHed, laborHed });
            laborHedArr[laborHedArr.Count - 1]["RowMod"] = "A";
            laborHedArr[laborHedArr.Count - 1]["SysRevID"] = "00000000";
            laborHedArr[laborHedArr.Count - 1]["SysRowID"] = "00000000-0000-0000-0000-000000000000";
....
       Console.WriteLine("Step 14:");
            Console.WriteLine("Create Empty arrays to match schema");
            Console.WriteLine("");

            JArray LaborDtlAttch = new JArray();
            JArray LaborDtlAction = new JArray();
            JArray LaborDtlComment = new JArray();
            JArray LaborEquip = new JArray();
            JArray LaborPart = new JArray();
            JArray LbrScrapSerialNumbers = new JArray();
            JArray LaborDtlGroup = new JArray();
            JArray SelectedSerialNumbers = new JArray();
            JArray SNFormat = new JArray();
            JArray TimeWeeklyView = new JArray();
            JArray TimeWorkHours = new JArray();
            JArray ExtensionTables = new JArray();


            Console.WriteLine("Step 15:");
            Console.WriteLine(" Create and fill 'ds' object");
            Console.WriteLine("");

            var ds = new JObject
            {
                { "LaborHed", laborHedArr },
                { "LaborDtl", laborDtlArr },
                { "LaborDtlAttch", LaborDtlAttch },
                { "LaborDtlAction", LaborDtlAction },
                { "LaborDtlComment", LaborDtlComment },
                { "LaborEquip", LaborEquip },
                { "LaborPart", LaborPart },
                { "LbrScrapSerialNumbers", LbrScrapSerialNumbers },
                { "LaborDtlGroup", LaborDtlGroup },
                { "SelectedSerialNumbers", SelectedSerialNumbers },
                { "SNFormat", SNFormat },
                { "TimeWeeklyView", TimeWeeklyView },
                { "TimeWorkHours", TimeWorkHours },
                { "ExtensionTables", ExtensionTables }
            };


            Console.WriteLine("Step 16:");
            Console.WriteLine(" Create and fill container object");
            Console.WriteLine("");
            // Create the completedRecord object
            var completedRecord = new JObject
            {
                { "ds", ds },
                { "EmployeeNum", laborDtlArr[0]["EmployeeNum"] }, // Adding EmployeeNum as a key-value pair
                { "ShopFloor", false },
                { "payrollDate", laborDtlArr[0]["PayrollDate"] } // Adding PayrollDate as a key-value pair
            };
            string strGetLaborHdr = completedRecord.ToString();


            try
            {
                Console.WriteLine("Step 17:");
                Console.WriteLine("Creating new row in Laborhed.");
                Console.WriteLine("");
                using (var client = CreateEpicorClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, StaticConfigurationClass.GetKineticTestURL() + "/api/v1/Erp.BO.LaborSvc/GetNewLaborHed1");
                    request.Headers.Add("Accept", "application/json");
                    var content = new StringContent(strGetLaborHdr, Encoding.UTF8, "application/json");
                    // Assign content to the request
                    request.Content = content;

                    var response = client.SendAsync(request).Result;
                    response.EnsureSuccessStatusCode();
                    string hdrResponse = response.Content.ReadAsStringAsync().Result;
                    // var hdrResponseObj = JObject.Parse(hdrResponse);

                    Console.WriteLine($"Employee {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}'s LaborHdr successfully created");

                    UpdateLaborHed(completedRecord);
                }
            }

        public static void UpdateLaborHed(JObject completedRecord)
        {
            Console.WriteLine("Step 18:");
            Console.WriteLine("Update Laborhed Record");
            Console.WriteLine("");

            completedRecord.Remove("EmployeeNum");
            completedRecord.Remove("payrollDate");
            completedRecord.Remove("ShopFloor");

            // define arrays to alter rowmod
            JArray laborhedArray = (JArray)completedRecord["ds"]["LaborHed"];
            JArray laborDtlArray = (JArray)completedRecord["ds"]["LaborDtl"];

            // Update RowMod property in laborhedArray
            if (laborhedArray != null && laborhedArray.Count > 0)
            {

                for (int i = 0; i < laborhedArray.Count; i++)
                {
                    // last labordtl rowmod value "U"
                    JObject obj = (JObject)laborhedArray[i];
                    if (i == laborhedArray.Count - 1)
                    {
                        // Last object in the array
                        if (obj["RowMod"]?.ToString() == "A")
                        {
                            obj["RowMod"] = "U";
                        }
                    }
                    else
                    {
                        // All other rowmod values ""
                        obj["RowMod"] = "";

                    }
                }
            }

            // Update RowMod property in laborDtlArray
            foreach (JObject obj in laborDtlArray.Cast<JObject>())
            {
                if (obj["RowMod"] != null)
                {
                    obj["RowMod"] = "";
                }
            }


            string strUpdateLaborHed = completedRecord.ToString();

            try
            {
                using var client = CreateEpicorClient();
                var request = new HttpRequestMessage(HttpMethod.Post, StaticConfigurationClass.GetKineticTestURL() + "/api/v1/Erp.BO.LaborSvc/Update");
                var content = new StringContent(strUpdateLaborHed, Encoding.UTF8, "application/json");
                // Assign content to the request
                request.Content = content;

                var response = client.SendAsync(request).Result;
                response.EnsureSuccessStatusCode();
                string UpdateLaborHedResponse = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine($"LaborHed Record successfully updated for employee {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}");
            }
      public static void GetNewLaborDtlWithHdr(JObject completedRecord)
        {
            Console.WriteLine("Step 19:");
            Console.WriteLine("Get new Labor Detail row");
            Console.WriteLine("");

            JArray laborDtlArray = (JArray)completedRecord["ds"]["LaborDtl"];
            JArray laborhedArray = (JArray)completedRecord["ds"]?["LaborHed"];

            if (laborDtlArray.Count > 0)
            {
                JObject firstLaborDtl = (JObject)laborDtlArray[0];

                completedRecord.Add("ipClockInDate", firstLaborDtl["AppointmentStart"]);
                completedRecord.Add("ipClockInTime", firstLaborDtl["ClockinTime"]);
                completedRecord.Add("ipClockOutDate", firstLaborDtl["AppointmentEnd"]);
                completedRecord.Add("ipClockOutTime", firstLaborDtl["ClockOutTime"]);
                completedRecord.Add("ipLaborHedSeq", firstLaborDtl["LaborHedSeq"]);

                for (int i = 0; i < laborDtlArray.Count; i++)
                {
                    // last labordtl rowmod value "U"
                    JObject obj = (JObject)laborDtlArray[i];
                    if (i == laborDtlArray.Count - 1)
                    {
                        // Last object in the array
                        if (obj["RowMod"]?.ToString() == "")
                        {
                            obj["RowMod"] = "A";
                        }
                    }
                    else
                    {
                        // All other rowmod values ""
                        obj["RowMod"] = "";

                    }
                }
            }

            // Update RowMod property in laborhedArray
            if (laborhedArray != null && laborhedArray.Count > 0)
            {
                // all laborhed rowmod values ""
                foreach (JObject obj in laborhedArray.Cast<JObject>())
                {
                    if (obj["RowMod"]?.ToString() != "")
                    {
                        obj["RowMod"] = "";
                    }
                }


            }

            string strGetNewLaborDtlWithHdr = completedRecord.ToString();

            try
            {
                using var client = CreateEpicorClient();
                var request = new HttpRequestMessage(HttpMethod.Post, StaticConfigurationClass.GetKineticTestURL() + "/api/v1/Erp.BO.LaborSvc/GetNewLaborDtlWithHdr");

                var content = new StringContent(strGetNewLaborDtlWithHdr, Encoding.UTF8, "application/json");
                request.Content = content;

                // Try to get more detailed error information from the response
                HttpResponseMessage responseLaborDtlHdr = null;
                try
                {
                    responseLaborDtlHdr = client.SendAsync(request).Result;
                    responseLaborDtlHdr.EnsureSuccessStatusCode();
                }
                catch (HttpRequestException ex)
                {
                    if (responseLaborDtlHdr != null)
                    {
                        var errorContent = responseLaborDtlHdr.Content.ReadAsStringAsync().Result;
                        Console.WriteLine($"****************************HTTP request failed creating employee {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}'s LaborDtlHdr in Kinetic. Status: {responseLaborDtlHdr.StatusCode}, Error: {errorContent}");
                    }
                    else
                    {
                        Console.WriteLine($"HTTP Request failed: {ex.Message}");

                    }
                    throw;
                }
                string GetNewLaborDtlWithHdrResponse = responseLaborDtlHdr.Content.ReadAsStringAsync().Result;


                Console.WriteLine($"Employee {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}'s LaborDtlHdr has been successfully created in Kinetic.");

            }
      public static void ChangeLaborType(JObject completedRecord)
        {
            Console.WriteLine("");
            Console.WriteLine("Step 22:");
            Console.WriteLine("remove unneccessary key from outer container");

            // Remove the "ipLaborType" property from the root object
            completedRecord.Remove("ipLaborType");

            // Define arrays to alter RowMod
            JArray laborhedArray = (JArray)completedRecord["ds"]?["LaborHed"];
            JArray laborDtlArray = (JArray)completedRecord["ds"]?["LaborDtl"];

            // Update RowMod property in laborhedArray
            if (laborhedArray != null && laborhedArray.Count > 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Step 23:");
                Console.WriteLine("Change RowMod values");


                foreach (JObject obj in laborhedArray.Cast<JObject>())
                {
                    if (obj["RowMod"]?.ToString() != "")
                    {
                        obj["RowMod"] = "";
                    }
                }




            }

            // Update RowMod property in laborDtlArray
            if (laborDtlArray != null && laborDtlArray.Count > 0)
            {

                for (int i = 0; i < laborDtlArray.Count; i++)
                {
                    // last labordtl rowmod value "U"
                    JObject obj = (JObject)laborDtlArray[i];
                    if (i == laborDtlArray.Count - 1)
                    {
                        // Last object in the array
                        if (obj["RowMod"]?.ToString() == "A")
                        {
                            obj["RowMod"] = "A";
                        }
                    }
                    else
                    {
                        // All other rowmod values ""
                        obj["RowMod"] = "";

                    }
                }

                // Convert the modified JObject to a string
                string strCompletedRecord = completedRecord.ToString();

                try
                {
                    Console.WriteLine("");
                    Console.WriteLine("Step 24:");
                    Console.WriteLine("Change Labor Type");

                    using var client = CreateEpicorClient();
                    var request = new HttpRequestMessage(HttpMethod.Post, StaticConfigurationClass.GetKineticTestURL() + "/api/v1/Erp.BO.LaborSvc/ChangeLaborType");

                    var content = new StringContent(strCompletedRecord, Encoding.UTF8, "application/json");
                    request.Content = content;

                    var response = client.SendAsync(request).Result;
                    response.EnsureSuccessStatusCode();
                    string changeDefaultLaborType = response.Content.ReadAsStringAsync().Result;
                    Console.WriteLine($"Labor type successfully changed for employee  {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}");

                    ChangeIndirectCode(completedRecord);

                }
  public static void FinalUpdateToLaborRecord(JObject completedRecord)
        {
            Console.WriteLine("");
            Console.WriteLine("Step 26:");
            Console.WriteLine("Modify RowMod for Update");

            // Define arrays to alter RowMod
            JArray laborhedArray = (JArray)completedRecord["ds"]?["LaborHed"];
            JArray laborDtlArray = (JArray)completedRecord["ds"]?["LaborDtl"];

            // Update RowMod property in laborhedArray
            if (laborhedArray != null && laborhedArray.Count > 0)
            {
                foreach (JObject obj in laborhedArray.Cast<JObject>())
                {
                    obj["RowMod"] = "";
                }
            }

            // Update RowMod property in laborDtlArray
            if (laborDtlArray != null && laborDtlArray.Count > 0)
            {

                foreach (JObject obj in laborDtlArray.Cast<JObject>())
                {
                    obj["RowMod"] = "";
                }
            }


            string strCompletedRecord = completedRecord.ToString();

            try
            {
                Console.WriteLine("");
                Console.WriteLine("Step 27:");
                Console.WriteLine("Update Labor Record Again");

                using var client = CreateEpicorClient();
                var request = new HttpRequestMessage(HttpMethod.Post, StaticConfigurationClass.GetKineticTestURL() + "/api/v1/Erp.BO.LaborSvc/Update");
                var content = new StringContent(strCompletedRecord, Encoding.UTF8, "application/json");
                // Assign content to the request
                request.Content = content;

                var response = client.SendAsync(request).Result;
                response.EnsureSuccessStatusCode();
                string UpdateLaborResponse = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine($"LaborHed Record successfully updated for employee {completedRecord["ds"]["LaborDtl"][0]["EmployeeNum"]}");

                ValidateChargeRate(completedRecord);

            }

Just follow what @klincecum put there in the post above and it should work exactly like he’s describing.

Normally you will GetNewRecord and it should have the “A” for adding in the return dataset, then you can use that and update the data there and update with rowmod of “U”

Oh my, you are doing a lot of unnecessary work that Epicor does for you.

Agree you could do all this with a simple function and either call that with your dataset OR have it called via a BPM trigger versus all this outside back and forth to the server. LOTS of unneeded traffic.

And even if you don’t use a function, no need to build your own datasets, you can have Epicor build them for you.

We’re not ganging up on you, just trying to save you some work. We’ve all been there.

Sorry for the delay, apparently there’s a cap on how many replies you can make as a new user. ::upside_down_face: :melting_face:

so we have not used any of the epicor function UIs. I’m not even sure where to go for it. Noob here! First software official gig. However, I do plan on looking into it.

@CSmith I think that is what I have done. I may be entirely too close to the project to see where the issue is.

In theory with this guidance I should be able to see the laborHed row when it’s added before the update call?

I am currently going through and using the returned value in the next call instead of just passing along the record that I manually created to see if there’s any difference there.

Turns out that fixed it. Im sure the issue was some miniscule character difference that I missed.

Anyways, Thanks for all your help!

3 Likes