Insert Records into a table while "On After Get Record" Trigger

Good evening,

When creating a new Item, I initialice several properties to “default values” such as “Costing Method” and so on, from the “Form - On New Record” Event.

But once created the Item I would fill out the “Base Unit of Measure”, that involves creating a new entry into the T5404.

Since the product hasn’t “No.” by this time, I cant do this insert in the On New Record event.

So, I’ve tried to put this piece of code in the “On After Get Record” Event.

myIUM.RESET; //myItemUnitMeasure - Variable Local Record “Item Unit of Measure”
IF NOT myIUM.GET(“No.”,‘UDS’) THEN BEGIN
MESSAGE('No existe UDS para ’ + FORMAT(“No.”));
myIUM.INIT;
myIUM.“Item No.”:=“No.”;
myIUM.Code:=‘UDS’;
myIUM.“Qty. per Unit of Measure”:=1;
myIUM.INSERT(TRUE);
VALIDATE(“Base Unit of Measure”,‘UDS’);
MODIFY;
END ELSE BEGIN
MESSAGE('Existe UDS para ’ + FORMAT(“No.”));
END;

But I get this error: “No changes can be done into de database until open a transaction”

I have no idea how to skip this problem.

Any piece of help will be useful.

Thanks.

First I would say,

In the item table you can look at the properties of the fields you want to have a default value.
On the InitValue Property you can set a default value. (No code needed) It will then be consistent. Remember Code should be on the table not the form if at all possible!!!

2nd, for the insert of base unit of measure automatically, I would suggest to you to create a new function in the item table called AutoFillUnitOfMeasure. You will call this from the Oninsert trigger of the item table:
OnInsert()
//Autofill unit of measure MOD
AutoFillUnitOfMeasure;

AutoFillUnitOfMeasure()
IF ItemUnitOfMeasure.GET(“No.”,“Base Unit of Measure”)
THEN BEGIN
VALIDATE(“Base Unit of Measure”);
END
ELSE BEGIN
ItemUnitOfMeasure.Code := “Base Unit of Measure”;
ItemUnitOfMeasure.“Item No.” := “No.”;
ItemUnitOfMeasure.“Qty. per Unit of Measure” := 1;
ItemUnitOfMeasure.INSERT(TRUE);
VALIDATE(“Base Unit of Measure”);
END;

Your code works too, but basically these are the places you want to put these changes.
Save->Complie->Enjoy!

You should never write any validation code on any forms. That should all be in the table object. In your case I’d probably put it in the OnInsert trigger of the table.

[Y]

Once again the quick fingers type complie instead of compile [&]

Oh I didn’t even see that, I just like the phrase “save → compile → enjoy”

i made the same thing, but only in a form, where the code are called from button push.

for item u.o.m insert:

itemuom.“Item No.” := newitem;
itemuom.Code := what_to_be_Base_Unit_of_Measure;
itemuom.“Qty. per Unit of Measure” := 1;
itemuom.INSERT;

it works. what different is, when doing INSERT, there is no ‘(TRUE)’ to run validation. it just need to ensure the item inserted is having consitent “base unit of measure” with what was inserted.

oitem.TRANSFERFIELDS(tempitem,TRUE); // *** use this if item information is stored in a temp.
oitem.“No.” := newitem; //this new item var is the same as what was inserted in item u.o.m above.
oitem.INSERT;

// *** continue for fields assignments if item information is NOT running line: [oitem.TRANSFERFIELDS(tempitem,TRUE);]

what is my risk if not doing INSERT(TRUE) ?

Thanks. It works perfect!!