Baanboard.com

Baanboard.com (http://www.baanboard.com/baanboard/index.php)
-   Tools Development (http://www.baanboard.com/baanboard/forumdisplay.php?f=8)
-   -   Extension - Execute when an object finishes initialization (http://www.baanboard.com/baanboard/showthread.php?t=71343)

tmannais 4th September 2018 14:21

Extension - Execute when an object finishes initialization
 
Hi,

I created an extension type Table for tisfc001. I want it to execute some functions when a new record is created. Sounds easy, right?
Yes. I then implemented on After Save hook with the condition "mode = DAL_NEW". As a result, the functions inside the condition get executed as expected. However, the functions need to use some data that is related to the table, which is Material. The tough part comes when I realized that the data that link to the table is generated after a new Production Order is created. Therefore, my functions inside the Extension cannot perform correctly because the data is not there in time.

To make it easier to understand, let me visualize it this way. It now flows like this:
1. Create new record
2. After Save hook is executed
3. The related data is generated according to the newly created order

The desired flow:
1. Create new record
2. The related data is generated according to the newly created order
3. **a way to execute the functions here**

The generated data often have many records.
The After Save hook triggers in total of 3 times like this:
1. DAL_NEW
2. DAL_UPDATE
3. DAL_UPDATE
at the point of 2. and 3. are the only moment that the functions can perform correctly since the required data is not generated in the step 1.

I tried using CDF in this table to act as a flag.
When an order is created, the flag is initialized to 0, and when the After Save hook gets executed, it will check if this CDF is 0, which means it was just created then it will perform the functions and then update the CDF to other value such as 1 in order to trigger it only once.
The problem is that updating in the same table as the table in the After Save here is not allowed because it will cause a recursion. So, this method is not valid.

In case who wants to see the code, it's as simple as this:
Code:

function extern long after.save.object(long mode)
{
        if mode = DAL_UPDATE then
                some.functions.to.run() |* Gets executed 2 times on creating new record
        endif
        return (0)
}

Please help.

Regards,
Thana

Ajesh 4th September 2018 17:19

You mean to say you want some data in some other related table than tisfc001 ? But that data is not coming at the time of after.save.object.

Why dont you try writing in db dll? specifically in db.after.update() Hook,worth a try...

tmannais 5th September 2018 06:57

Quote:

Originally Posted by Ajesh (Post 210297)
You mean to say you want some data in some other related table than tisfc001 ? But that data is not coming at the time of after.save.object.

Yes, I think you understand most of it.

Quote:

Originally Posted by Ajesh (Post 210297)
Why dont you try writing in db dll? specifically in db.after.update() Hook,worth a try...

I have never tried db dll. Could you please give me more details about what it is and how to start using it?

By the way, I have changed the method of achieving the goal to use another hook that is somehow acceptable; so, this case is not urgent anymore but solving it will lead to a knowledge that one might find it useful in the future.

Ajesh 5th September 2018 09:57

db dll wouldnt work because after its execution after.save.object will be called.

At this point i could think of scrapping the extension altogether and write a User Exit dll, In the after.after.save.object hook of User Exit.

Other option is in the Session Extension of that session and including New Record and after.command of that new Record you can write your logic. But it depends whether you want your logic to execute every time at a New Record creation or only through entering of that session. In the First case writing a Session Extension wouldnt work...

JaapJD 5th September 2018 13:45

I would do it the other way round: In the table extension of the Material table you can update tisfc001...

tmannais 6th September 2018 04:46

When a Production Order is created, Materials will be generated accordingly and there are a lot of them. I was thinking of a way to run only when the last Material is generated but I don't know how to check for the conditions. Moreover, I don't know if this will impact any other part of the system, like if a Material is created separately (I don't know if it is possible) then the extension will trigger, which it is not supposed to.

Ajesh 6th September 2018 15:47

What do you mean by Materials will be generated? Which Table you are talking about?

tmannais 7th September 2018 04:47

Quote:

Originally Posted by Ajesh (Post 210322)
What do you mean by Materials will be generated? Which Table you are talking about?

Materials are data in table ticst001. They will be automatically created from some conditions after a Production Order is created.

giggty 17th September 2018 14:50

I might be missing something but...
 
... why not just declare a global flag variable and use it like you were trying to use CDF? it should retain its value between hook calls.

tmannais 20th September 2018 09:14

The point is that the hook is not firing in the needed timing.

giggty 20th September 2018 12:02

But your original post states that after.save hook gets called 3 times, 2nd and 3rd being the eligible ones. How so?

tmannais 21st September 2018 04:41

because they are in DAL_UPDATE step, which is not exactly where I wanted to have my functions executed. I want it to execute the functions when a new record is created, which should be DAL_NEW but it just doesn't do at the right moment for me.

giggty 21st September 2018 09:12

But afer.save hook with mode=DAL_UPDATE is called right after the materials are created. This is exactly what you need, no?

tmannais 21st September 2018 13:51

For that particular event, yes. But it needs to do only once after the creation. Now that if I use on DAL_UPDATE it will be executed every time the record is modified not only by users but it will be triggered even from the system itself too when it does something that affects the table. Moreover, it triggers twice in the first place.

JaapJD 21st September 2018 14:06

What you are trying to accomplish is simply not possible. When the record is inserted in tisfc001 the mode is DAL_NEW. Whatever happens after that on tisfc001 for that production order will never be with mode DAL_NEW again.
In your first post you said you tried with a CDF. That can be a good approach. Initialize on 0 and update to 1 as soon as the after.save.hook() did the work using the materials. That recursion problem is probably caused by the fact that you did an update yourself on the table record. If you would move your code to the before.save.hook() and assign just the value 1 to the CDF will work, I suppose.


All times are GMT +2. The time now is 17:39.


vB.Sponsors
©2001-2018 - Baanboard.com - Baanforums.com