ajm1811
20th July 2016, 17:13
Hi,

This is my first script and post on baanboard so please forgive and gently correct me if my ettiquette isn't quite up to scratch!!

I am trying to write a script to do the following:

1) find production warehouse orders in timfc001 that meet certain criteria
2) delete them
3) once deleted edit the warehouse of the corresponding record in ticst001
4) recreate the production warehouse order in timfc001

Steps 1 - 3 I have acheived successfully via DAL functions.
Step 4 is what I'm struggling with. In order to create a new record in timfc001 it looks like I have to do it from another session. I cannot create a record in there directly. Therefore I am using AFS calls to call timfc0101m000 from ticst0101m000. The steps I take to do what I want to do in LN are:

1) Select the record in ticst0101m000
2) Run session timfc0101m000 from here
3) Put a qty into "To Issue" Field (frm.mat.to.issu)
4) Run the process function
5) Run the initiate funtion

Done.

Therefore I would have thought my code would be:


stpapi.put.field("ticst0101m100", "ticst001.pdno", str$ (ticst001.pdno))
stpapi.put.field("ticst0101m100", "ticst001.pono", str$ (ticst001.pono))
afs_ret = stpapi.find("ticst0101m100", err.msg)
if afs_ret then
afs_ret = stpapi.mark("ticst0101m100", err.msg)
if afs_ret then
stpapi.handle.subproc("ticst0101m100", "timfc0101m000", "add")
stpapi.form.command("ticst0101m100", 2, "timfc0101m000", err.msg)
stpapi.put.field("timfc0101m000", "frm.mat.to.issu", str$ (qty.ordered))
stpapi.form.command("timfc0101m000", 5, "process.chg.qty", err.msg)
stpapi.form.command("timfc0101m000", 5, "initiate.order", err.msg)
endif
stpapi.end.session("timfc0101m000")
endif
stpapi.end.session("ticst0101m000")


I found the following thread which sounds like exactly what I want to do but it doesn't work for me.

57661

The stpapi.find("timfc0101m000", msg.out) in the code from the above thread returns a 0 for me. I therefore tried an stpapi.change.view on timfc0101m000 after the call from ticst0101m000 but I get an error message saying "enter a value for the unit field". On the form itself I can't see anywhere to enter a value for the unit field. There is a field called timfc001.orun for the unit which is a child of frm.mat.to.issu and I have also tried passing that via afs calls and creating the view but I get the same error.

Any help would be much appreciated.

Thanks,

Andy

mark_h
20th July 2016, 18:43
Wish that member had returned and posted a final solution.

Okay - not familiar with your version of baan or even timfc0101m000. So my question is first does the find in ticst0101m100 work just fine? Second - you have a stpapi.handle.subproc for timfc0101m000 - but does it work when you hit the stpapi.form.command("ticst0101m100", 2, "timfc0101m000", err.msg)? You can check this by checking processes ids - I do this in 4c4 thru option dialog - start shell - ps. I am assuming 2 Then I can see which process is a parent and which is a child. In this case I would expect the process id of timfc to be a child of the ticst session. The reason I ask this is sometimes if the child session is not controlled by a parent session things like find might not work correctly. Also the code you posted does not have the find in it and I am assuming it is like the linked code.

Next question - if you do this manually - when in ticst0101m100 and you zoom to timfc0101m000 - do you have to do a find? If it zooms to the correct record then the afs code should (in theory) do the same thing. Do you actually have to get to timfc0101m000 thru a zoom to the session - or can you run it independently of ticst0101m100?

PS - an ln user might have another solution rather than using AFS.

ajm1811
21st July 2016, 00:31
Thanks for the quick reply Mark.

Yes the first stpapi.find returns a value of 1 as does the stpapi.mark. I've not checked the process ids nor have I done that before in LN but I'll have a go. I've checked the AFS logs and the stpapi.form.command to start session timfc0101m000 does start the session sucessfully.

When browsing to timfc0101m000 from ticst0101m100 I do not have to put any values in or find etc. Everything is there on the screen. I just need to enter qtys. However, if I browse to another record and then back the record that was on the screen initially is no longer there. Therefore, when browsing from ticst0101m100 values are displayed on the screen but nothing is written to the db at this point. I have also tested this with AFS by using an stpapi.get.field and nothing is returned. It only seems to be written to the db when I run the process.chg.qty function, which currently I can only do from the session in LN.

In answer to your last question. It is possible to run timfc0101m000 independently but both the new view and add record commands are disabled. It appears that you can only add records to this session by calling it from another session. Another point to note and I'm not sure if it is significant, there are two ways to search by on this session. By default it is by end item which shows transactions relating to the main item produced by the production order. The other option is by material which shows transactions relating to the material to be issued to the production order. When running timfc0101m000 independently the search mode is by end item. However, when calling from ticst0101m100 it is in the correct search mode.

Thanks

Andy

bhushanchanda
21st July 2016, 07:02
Hi,

Can you try using stpapi.change.view before putting your position? That might solve your search issue. Rest should work fine if nothing else stucks.

http://www.baanboard.com/baanboard/showthread.php?t=66296

This will give you some hint on stpapi.find.

ajm1811
21st July 2016, 10:51
No matter where I put the stipapi.change.view I get a message. "Please enter a value for the unit field". I also get this error both when I click new view in LN when doing this manually.

When doing this manually in LN it may also significant to note that I get this message 8 times and that there are 8 form fields relating to qty each with the same child field of timfc001.orun. I thought passing a value to this field would get rid of this message but it doesn't. There is no where on the form to enter a unit but everything seems to go through when I run the process.chg.qty function. That is why I thought calling the function via afs would just make it work. The script runs fine but no data is saved.

Below is my afs.log which looks like it runs successfully but nothing is saved:


LOGGING STARTED
21-07-2016
>ticst0101m100(25) put.field:ticst001.pdno RRO001922
>ticst0101m100(25) put.field:ticst001.pono 10
>ticst0101m100 Find
22dll->serv (def.find25)
ticst0101m100(25)-RUNNING:def.find
25serv<-4gl (def.find01)
ticst0101m100(25)-RUNNING:
<ticst0101m100 record found (1)
>ticst0101m100 mark
22dll->serv (mark.occur25)
ticst0101m100(25)-RUNNING:mark.occur
25serv<-4gl (mark.occur0)
ticst0101m100(25)-RUNNING:
<ticst0101m100 mark successful
>ticst0101m100 handle.subproc timfc0101m000 add
22dll->serv (handle.subsessiontimfc0101m000225)
>ticst0101m100 form.command command type: 2 command prog: timfc0101m000
22dll->serv (form.command2timfc0101m00025)
ticst0101m100(25)-RUNNING:form.command
26serv<-4gl (api.child.startedtimfc0101m00025)
26serv<-4gl (init.ready)
timfc0101m000(26)-RUNNING:
<ticst0101m100
25serv<-4gl (form.command0)
ticst0101m100(25)-RUNNING:
>timfc0101m000(26) put.field:frm.mat.to.issu 2
>timfc0101m000 form.command command type: 5 command prog: process.chg.qty
22dll->serv (form.command5process.chg.qty26)
timfc0101m000(26)-RUNNING:form.command
26serv<-4gl (form.command0)
timfc0101m000(26)-RUNNING:
<timfc0101m000
>timfc0101m000 form.command command type: 5 command prog: initiate.order
22dll->serv (form.command5initiate.order26)
timfc0101m000(26)-RUNNING:form.command
26serv<-4gl (form.command0)
timfc0101m000(26)-RUNNING:
<timfc0101m000
>timfc0101m000 end.session
22dll->serv (end.program26)
timfc0101m000(26)-RUNNING:end.program
(0)-RUNNING:end.program
26serv<-4gl (end.program0)
timfc0101m000(26)-RUNNING:
26dll->serv (timfc0101m000: exit(26))
>ticst0101m000 end.session
22dll->serv (afsscript: exit(22))


It's as if the value I want to pass to frm.mat.to.issu isn't being put into the form and so it's processing a qty of 0 i.e not writing anything to the db as there is nothing to save. I have also tried running an stpapi.update after passing the value to the buffer but the log suggests that this closes timfc0101m000 after the update and before the process function is called. I can post this log too if it helps?

Thanks,

Andy

ajm1811
21st July 2016, 13:11
Mark - after you suggesting I look through the process IDs I have noticed while stepping through the code that the stpapi.synchronize.dialogue call creates another process running this session but in a different group (see attachment). I use the synchronise call because without it the call to stpapi.update fails. Once the stpapi.update call sucessfully executes, this 2nd instance of the process running timfc0101m000 dies process leaving just the two running ticst0101m100 and timfc0101m000 in the same group.

Could it be that the qty value I'm trying to pass is actually passed to this 2nd instance, then as it has no parent, when I try to update it it dies and I'm actually running the process.chg.qty on the initial instance of timfc0101m000 which has had no qty passed to it?

If this is the case any ideas on how do I fix it?

mark_h
21st July 2016, 14:49
That could be exactly what the problem is. The second session starts and it gets the updates. Step thru your code one line at a time and see when the first instance of timfc0101m000 is gets created. I think if you can get control of that process it might very well work.

baan_guru
15th May 2021, 13:36
After 'change.view' it resets the frm.mat.to.cancel to zero. Thus using put.var to set the variable 'frm.mat.to.cancel' again in AFS.

Similar approach can be taken for other 'frm.mat....' variables.


select timfc001.*
from timfc001
where timfc001._index1 = {:i.pdno, :i.pono}
order by timfc001._index1
as set with 1 rows
selectdo
afs.process.id = -1
stpapi.put.field("timfc0101m000", "timfc001.pdno", timfc001.pdno)

procid = pid
procid = pstat(procid, progname, info)
while procid > 0
prev.progname = progname
afs.process.id = procid
procid = pstat(procid, progname, info)
if progname = "timfc0101m000" and pos(prev.progname, "ottstpapiserv") > 0 then
break
else
afs.process.id = -1
endif
endwhile

if afs.process.id < 0 then
o.err.msg = "Error in starting AFS."
return(false)
endif

stpapi.put.field("timfc0101m000", "timfc001.pono", str$(timfc001.pono))
stpapi.put.field("timfc0101m000", "timfc001.effn", str$(timfc001.effn))
stpapi.put.field("timfc0101m000", "timfc001.seqn", str$(timfc001.seqn))
stpapi.put.field("timfc0101m000", "timfc001.ittp", str$(etol(timfc001.ittp)))
if stpapi.find("timfc0101m000", o.err.msg) = 1 then
stpapi.put.field("timfc0101m000", "frm.mat.to.cancel", str$(i.qnty))
if stpapi.change.view("timfc0101m000", o.err.msg) = 1 then
put.var(afs.process.id, "frm.mat.to.cancel", i.qnty)
stpapi.form.command("timfc0101m000", 5, "process.chg.qty", o.err.msg)
if isspace(o.err.msg) then
return(true)
endif
endif
else
o.err.msg = "Record not found in 'Production warehouse Orders'"
endif
endselect