I am hoping to shortcut writing a bunch of code…
I have a record in a UD table that I want to copy to another record and only change one Key field. If the new record already exists, then overwrite the non-key fields, otherwise create it. I then want to delete the old record.
I saw @Chris_Conn write a simple way of going through the UD fields in the post on GetANewUDXX and it would save a few lines. I am looking for maybe a GetByID on the old record, change the one Key value and use UpdateExt to save ithe new one and delete the old one. However, I am also hoping there is a simple Copy method in BPMs…
You can still use BufferCopy in E10, which also existed in ABL and I actually learned about if from one of your old ABL Codes.
// Example
BufferCopy.CopyExceptFor(QueryFromDBRecordRow, ttMyNewRow, "SysRowID","SysRevID","RevisionNum", "EffectiveDate", "RowMod");
// Change more stuff on ttMyNewRow
// Save
Example:
You might want to use CopyExceptFor and exclude KeyX and other fields like shown above – probably need to tweak this code a bit, but its a boilerplate.
Action UpdateUDRecord = (iResourceID) =>
{
var UD02svc = Ice.Assemblies.ServiceRenderer.GetService(Db);
var udRow = (from ud02 in Db.UD02 where ud02.Company == callContextClient.CurrentCompany && ud02.Key1 == "Runtime" && ud02.Key2 == iResourceID && ud02.CheckBox01 == true select ud02).FirstOrDefault();
if (udRow != null)
{
UD02Tableset ds = UD02svc.GetByID(udRow.Key1,udRow.Key2,udRow.Key3,udRow.Key4,udRow.Key5);
//Create the Before Image
UD02Row OriginalRecord = (UD02Row)ds.UD02.NewRow();
//Populate it
BufferCopy.Copy(ds.UD02[0], OriginalRecord);
//Add to the data set
ds.UD02.Add(OriginalRecord);
ds.UD02[0].Date02 = DateTime.Today;
ds.UD02[0].Number02 = (decimal)((TimeSpan)(DateTime.Now - DateTime.Today)).TotalSeconds;
ds.UD02[0].CheckBox01 = false;
ds.UD02[0].RowMod = IceRow.ROWSTATE_UPDATED;
UD02svc.Update(ref ds);
}
};
1 Like
I think that would work, you just need to change the keys before saving and set the rowmod to “A”
I wonder if something similar to this would also work:
UD02Row desRow = (UD02Row)ds.UD02.NewRow();
var sourceRow = ds.UD02[rowNum]; //whatever the source row is
desRow.ItemArray = sourceRow.ItemArray.Clone() as object[];
That could be perfect. I will jump in and try it.
Here is the ABL Equivalent, explains the concept of using BufferCopy in fewer lines.
FIND FIRST UD05 WHERE UD05.Key3 = sCurrentQuoteNum AND UD05.Key4 = sCurrentQuoteLine
IF AVAILABLE UD05 THEN DO:
CREATE bNewUD05.
BUFFER-COPY UD05 EXCEPT sysrevid sysrowid to bNewUD05
ASSIGN bNewUD05.Key3 = string(targetQuoteNum)