Babu Nagarajan
19th July 2016, 14:13
Hi Friends,

We are using Inter company transactions to make purchase and sales transactions between sister companies.

My parent company is 10 where I create an AR batch and sister company is 20 where I create an AP batch.

I have written a AFS script to do the process

I dont have problem in creating AP batch and transactions.

While creating the AP Batch I use switch.to.company(Child.company) before inserting the Data in the session tfgld1101m000. Subsequently I do the stpapi.handle.subproc() to insert the transaction data in tfacp1110s000. Then I do an stpapi.end.session() and then I do the switch.to.company(Parent.company).

Till here everything is fine. Now when my next AP batch is to be created in company 55 (another child company), again the AR batch and transaction is properly created in company 10 but when I do switch.to.company(child.company), the new AP batch is still getting created in company 20. On the back end the session tfgld1101m000 is not coming out of the previous process.

Is there anyone who came across the same kind of situation. I need to be able to switch companies using stpapi. I am attaching the sample code.

It will be really helpful if I can find a help.

Thanks
Babu

switch.to.company(sister.company)
stpapi.put.field("tfgld1101m000","tedt.f",str$(invoice.date))
stpapi.put.field("tfgld1101m000","tfgld101.year",str$(year.f))
stpapi.put.field("tfgld1101m000","bref.f",str$("Markup"))
stpapi.put.field("tfgld1101m000","fprd.f",str$(prno.f))
retval = stpapi.change.view("tfgld1101m000",errm)
stpapi.put.field("tfgld1101m000","tfgld101.ttyp",str$(tfgld503.ttyp))
retval = stpapi.insert("tfgld1101m000",true,errm)
stpapi.get.field("tfgld1101m000","tfgld101.btno",btno)

retval = stpapi.mark("tfgld1101m000",errm)
stpapi.handle.subproc("tfgld1101m000","tfacp1110s000","add")
stpapi.continue.process("tfgld1101m000",errm)


| tfgld501.famt = 1000
stpapi.put.field("tfacp1110s000","tfacp200.suno",str$(tfgld503.suno))
stpapi.put.field("tfacp1110s000","tfacp200.docd",str$(invoice.date))
stpapi.put.field("tfacp1110s000","screen.amnt",str$(tfgld501.famt))
isup = str$(tfgld501.tran)&"/"&str$(tfgld501.idoc)
stpapi.put.field("tfacp1110s000","tfacp200.isup",str$(isup))
stpapi.put.field("tfacp1110s000","tfacp200.refr",str$("Markup"))
stpapi.handle.subproc("tfacp1110s000","tfacp1120s000","add")
retval = stpapi.insert("tfacp1110s000",true,errm)
stpapi.save("tfacr4100m000",errm)
enter.purchase.invoice.transaction()
stpapi.get.field("tfacp1110s000","tfacp200.ttyp",ap_ttyp)
stpapi.get.field("tfacp1110s000","tfacp200.ninv",ap_ninv)
stpapi.end.session("tfacp1110s000")
stpapi.end.session("tfacp1101m000")

endselect
}

function enter.purchase.invoice.transaction()
{

select tfgld501.*
from tfgld501
where tfgld501._compnr = :ncmp
and tfgld501._index1 = {:tfgld500.cuno}
and tfgld501.tran = :ttyp
and tfgld501.idoc = :ninv2
selectdo
stpapi.put.field("tfacp1120s000","tfgld102.leac",str$(tfgld503.muas))
stpapi.put.field("tfacp1120s000","tfgld102.dim1",str$(tfgld501.dimx))
stpapi.put.field("tfacp1120s000","amount",str$(tfgld501.famt))
retval = stpapi.insert("tfacp1120s000",true,errm)
endselect
stpapi.end.session("tfacp1120s000")
switch.to.company(parent.company)

}

mark_h
19th July 2016, 14:36
You can search this forum, but a quick search turned up this http://www.baanboard.com/baanboard/showthread.php?t=22982&highlight=switch.to.company here. What I don't see in your code is closing all of the sessions in the process. Are you making sure everything is closed including the tfgld1101m000? This is where if you notice in the link that it looks like it might not support multiple company switches - I do not know that for sure, never done it myself.

The other thing the link made me thing of is putting the transactions creations inside separate sessions - kind of like:
Switch.to.company(parent.company)
zoom.to.session(...put new session herewhat you need here...)
switch.to.company(child.company)
zoom.to.session(....put new session here to create what is needed)

I was thinking that by zooming and closing a session it might clear up anything that is being cached in memory while running thru the process. Not sure it will work, but might be worth a shot.

Babu Nagarajan
20th July 2016, 08:00
Hi Mark,

Thank you very much for pointing out the mistake. I have made a mistake in the stpapi.end.session("tfacp1101m000"). It should be stpapi.end.session("tfgld1101m000"). It is working fine now.

Thanks
Regards
Babu Nagarajan

mark_h
20th July 2016, 15:43
That is really good to hear.