Hi, here’s the problem:
I have a logic_hook called at the time of saving a new record, more precisely in before_save. Here, I need to change the ID of this record:
$bean->new_with_id = true;
$bean->id=$new_code;
Of course, I do checks on this ID, taking the last one entered, so $new_code will contain the last value entered+1. I didn’t insert the line
$bean=newBean("MODULE");
because I’m in before_save(), the bean is already being created.
So far so good, it works, but when I save a related field (present in the form), it is not saved, I have to reopen the form and insert that field again. This doesn’t happen if I don’t modify the ID:
I think the problem is due to the relationship table between the two modules, in the field …ida is stored the original ID value and not the one I am going to modify, I tried to change it:
The moment I enter before_save, the IDA field is not set, there is only IDB, that’s why when I search for relationships it doesn’t find anything.
Manually setting
$bean->_ida=$new_code;
and printing the $bean to the screen, the changes exist, but if I go to look in the DB, that field has been populated with a different ID, the default one.
I have no idea why or how to fix it.
Update:
I moved all the logic into after_relationship_add. In the before_save the relationship was not yet saved, now I have the relationship created which I delete, after which I edit my ID as desired and add it to the relationship.
The problem is that now in the DB, every time I save a record, I have two records, one with alphanumeric ID whose field deleted=1 and the other with my ID.
Unfortunately I didn’t have any other ideas to solve the problem, if there is any better idea I will gladly listen to it, for now it works.
I wouldn’t have any problems with using a direct SQL query right there, to permanently delete the row with the “deleted=1”, to avoid cluttering the database.
If I remember correctly, after a certain amount of time, deleted=1 are permanently deleted from Suite, right? Or do they remain indefinitely? If not, I’ll have to do a SQL DELETE, although I’m taking it easy on a direct delete.
That is true if you have the “purge” job enabled, which is disabled by default.
Then you need to check if that job actually deletes these records, because it does some kinds of purges, but not others. I would bet that it never touches relationship tables.
Yes, I found it in the schedule list, but it’s inactive, as you said, I don’t know what it does, if it deletes the deleted reports as well.
If I operate directly on the DB, can I delete only that record from the relationship table? Or do I necessarily have to delete some other records from other tables?
Note that you might have two rows, not just one, for each relationship: one with your id as ida, the other as idb.
If you delete that, you break the relationship. That’s it. You might leave some orphaned records behind, or some loose ends, but that depends on application-level considerations (on what you’re doing with those records, your business flows etc).
So, if you know these are new relationships, or new records, and not a complex web of related records, you can set things up just as you want with some direct SQL.
Thanks pgr, there are 2 rows stored, one with the ID given by Suite and the other with the ID given by me, I can delete the one with the Suite ID and at the end only my row is left