mjpedreira1
8th February 2010, 11:23
Hello,

I have an AFS program that read a table and execute UPADTES or INSERT in a maintain session of Project module.

If I run the session manually, then the updates are correct. But If I run the session with a task, and there are several records, the updates don´t work.
I think that problem is that the execution is very fast. In have this problem few weeks ago and I solve it with suspend() function.
Now I try to use this function but I can´t solve the problem. Perhaps I dont use the suspend function in the correct place.

Does anybody have this problem?

Thanks,

María

george7a
8th February 2010, 13:29
Hi,

Can you post you code?

- George

mjpedreira1
9th February 2010, 14:14
Hello George, here is my complete code


|******************************************************************************
|* Mantener plazos de proyectos desde la intranet (AFS)
|* 15-10-09
|******************************************************************************
|* Script Type: 0
|******************************************************************************

declaration:

#pragma used dll ottstpapihand
#include <bic_text>

table ttpcmn010 | Tabla de datos temporales de os - Exportación desde la intranet
table ttpcmn011 | Log de errores en plazos de proyectos
table ttppin020
table ttppdm740
table ttcmcs036

|****************************** forms *****************************************

form.1:
init.form:
get.screen.defaults()

|****************************** choice section ********************************

choice.cont.process:
on.choice:

select *
from tpcmn010
selectdo
cargar.plazo.proyecto(tpcmn010.cprj,tpcmn010.cuno, tpcmn010.nins,
tpcmn010.cact,tpcmn010.cpro,tpcmn010.insa,
tpcmn010.sidt,tpcmn010.desc)

selectempty
endselect

crear.textos()

borrar.total.tpcmn010()

|****************************** function section ******************************
functions:

function crear.textos()
{
string temp.file1(500) |Fichero creado temporalmente
long File.pointer |Puntero al fichero txt
string kw1(17),kw2(17),kw3(17),kw4(17),tgrp(8),eopt(15)
long write.return, ret.code


select tpcmn010.*
from tpcmn010
where ( tpcmn010.txt1 <> ""
or tpcmn010.txt2 <> ""
or tpcmn010.txt3 <> ""
or tpcmn010.txt4 <> ""
or tpcmn010.txt5 <> "" )
selectdo
|Crear fichero
temp.file1 = creat.tmp.file$(bse.tmp.dir$())
file.chmod(temp.file1,S_IRWXU)
File.pointer = seq.open( temp.file1 , "wt")

if File.pointer < 1 then |Error al crear fichero
insertar.error.tpcmn011("Error al crear textos del plazo " & tpcmn010.cprj & "-" & tpcmn010.cuno & "-" & str$(tpcmn010.nins))
endif

seq.puts( shiftl$(strip$(tpcmn010.txt1 & tpcmn010.txt2 & tpcmn010.txt3 & tpcmn010.txt4)), File.pointer )
seq.close( File.pointer )
tppin020.cprj=tpcmn010.cprj
tppin020.cuno=tpcmn010.cuno
tppin020.nins=tpcmn010.nins
db.retry.point()
db.eq(ttppin020,db.delayed.lock)
write.return = text.delete("tppin020.txta","5")
write.return = text.write("tppin020.txta", "5", kw1, kw2, kw3, kw4, "", "", temp.file1)
db.update(ttppin020,db.retry)
commit.transaction()
ret.code = seq.unlink(temp.file1)
selectempty
endselect
}



function domain tcbool cargar.plazo.proyecto(domain tppdm.cprj proyecto, domain tccuno cliente,
domain tppdm.nins numplazo, domain tppdm.cact actividad,
domain tppdm.cpro codingreso, domain tppdm.aalc importe,
domain tppdm.date fechafactura, domain tppdm.desc descripcion)
{
extern string err.msg(60), recover.msg(60), sesion(13),tabla(8)
extern domain tcbool errores, insertar
domain tcmcs.int ret

errores=false

|Comprobaciones sobre el proyecto
select tppdm600
from tppdm600
where tppdm600._index1 = {:proyecto}
selectdo
selectempty
insertar.error.tpcmn011("No existe el proyecto " & proyecto)
errores = true
return(false)
endselect

|Actividad
select tppss200
from tppss200
where tppss200._index1 = {:proyecto, "001", :actividad}
selectdo
selectempty
insertar.error.tpcmn011("Para el proyecto y plan " & proyecto & "/001 no existe la actividad " & actividad)
errores = true
return(false)
endselect

|Código de Ingresos Proyecto
select tppdm043
from tppdm043
where tppdm043._index1 = {:codingreso}
selectdo
selectempty
insertar.error.tpcmn011("No existe el código de ingreso " & codingreso)
errores = true
return(false)
endselect

|Comprobar cliente e IVA del proyecto
select tppdm740.cvat,tppdm740.ccty
from tppdm740
where tppdm740._index1 = {:proyecto, :cliente}
selectdo
select tcmcs036.cvat
from tcmcs036
where tcmcs036._index1 = { :tppdm740.ccty, :tppdm740.cvat }
selectdo
selectempty
insertar.error.tpcmn011("El IVA " & tcmcs036.cvat & " del proyecto " & proyecto & " no existe")
errores = true
return(false)
endselect
selectempty
insertar.error.tpcmn011("El cliente " & cliente & " no es cliente del proyecto " & proyecto)
errores = true
return(false)
endselect

|Estatus del plazo
select tppin020
from tppin020
where tppin020._index1 = {:proyecto, :cliente, :numplazo}
and tppin020.ninv > 0
and tppin020.stin = 3
selectdo
|insertar.error.tpcmn011("No se puede modificar un plazo facturado")
errores = true
return(false)
endselect

if not errores then

stpapi.put.field("tppin4151m000","tppin020.cprj",str$(proyecto)) | Proyecto: tppin020.cprj
stpapi.put.field("tppin4151m000","tppin020.cuno",str$(cliente)) | Cliente: tppin020.cuno
stpapi.put.field("tppin4151m000","tppin020.nins",str$(numplazo)) | Plazo: tppin020.nins

if stpapi.find("tppin4151m000", err.msg) = 1 then
stpapi.put.field( "tppin4151m000","tppin020.desc",str$(descripcion)) | Descripción: tppin020.desc
stpapi.put.field( "tppin4151m000","tppin020.insa",str$(importe)) | Importe: tppin020.insa
stpapi.put.field( "tppin4151m000","tppin020.cact",str$(actividad)) | Actividad: tppin020.cact
stpapi.put.field( "tppin4151m000","tppin020.sidt",str$(fechafactura)) | Fecha factura: tppin020.sidt
stpapi.put.field( "tppin4151m000","tppin020.cpro",str$(codingreso)) | Código de ingreso: tppin020.cpro

if not stpapi.update("tppin4151m000", true, err.msg) then
stpapi.recover("tppin4151m000", recover.msg)
insertar.error.tpcmn011("Error en un plazo del proyecto " & tpcmn010.cprj & ". " & err.msg)
endif
else
insertar = true
endif
stpapi.end.session("tppin4151m000")


if insertar=true then

select tppin020.cprj
from tppin020 for update
where tppin020._index1 = {:proyecto,:cliente,:numplazo}
selectdo
insertar.error.tpcmn011("No se puede duplicar el plazo del proyecto " & tpcmn010.cprj & "y cliente " & cliente )
selectempty
tppin020.cprj=proyecto
tppin020.cuno=cliente
tppin020.nins=numplazo
tppin020.cspa=""
tppin020.cpla="001"
tppin020.cact=actividad
tppin020.cpro=codingreso
tppin020.cpra=""
tppin020.desc=descripcion
tppin020.dsca=""
tppin020.npoi=0
tppin020.nper=0.00
tppin020.insa=importe
tppin020.inaa=0.0
tppin020.cuni=""
tppin020.sidt=fechafactura
tppin020.invo=tppdm.yeno.no
tppin020.inpo=0
tppin020.inpr=0.00
tppin020.inia=0.00
tppin020.iiaa=0.00
tppin020.tipo=0
tppin020.tipr=0.00
tppin020.tiia=0.00
tppin020.tiaa=0.00
tppin020.cvyn=tppdm.yeno.yes
tppin020.cvat=tcmcs036.cvat
| tppin020.peru=""
tppin020.fins=tppdm.yeno.no
| tppin020.stin=tppdm.yeno.yes
tppin020.ninv=0
tppin020.seri=0
| tppin020.stna=0
tppin020.nina=0
tppin020.sera=0
tppin020.clos=tppdm.yeno.no
tppin020.sern=0
tppin020.txta=0
tppin020.toin=tppdm.yeno.yes
db.insert(ttppin020, db.retry)
commit.transaction()
endselect
endif
endif
return(true)

}

function insertar.error.tpcmn011(domain tcstr.300 mensaje)
{
stpapi.put.field("tpcmncm011m00", "tpcmn011.date", str$(date.num()))
stpapi.put.field("tpcmncm011m00", "tpcmn011.erro", mensaje)
stpapi.put.field("tpcmncm011m00", "tpcmn011.time", calcular.hora())

stpapi.put.field("tpcmncm011m00", "tpcmn011.cprj", str$(tpcmn010.cprj))
stpapi.put.field("tpcmncm011m00", "tpcmn011.cuno", str$(tpcmn010.cuno))
stpapi.put.field("tpcmncm011m00", "tpcmn011.nins", str$(tpcmn010.nins))
stpapi.put.field("tpcmncm011m00", "tpcmn011.cact", str$(tpcmn010.cact))
stpapi.put.field("tpcmncm011m00", "tpcmn011.cpro", str$(tpcmn010.cpro))
stpapi.put.field("tpcmncm011m00", "tpcmn011.insa", str$(tpcmn010.insa))
stpapi.put.field("tpcmncm011m00", "tpcmn011.sidt", str$(tpcmn010.sidt))
stpapi.put.field("tpcmncm011m00", "tpcmn011.desc", str$(tpcmn010.desc))
stpapi.put.field("tpcmncm011m00", "tpcmn011.txt1", str$(tpcmn010.txt1))
stpapi.put.field("tpcmncm011m00", "tpcmn011.txt2", str$(tpcmn010.txt2))
stpapi.put.field("tpcmncm011m00", "tpcmn011.txt3", str$(tpcmn010.txt3))
stpapi.put.field("tpcmncm011m00", "tpcmn011.txt4", str$(tpcmn010.txt4))
stpapi.put.field("tpcmncm011m00", "tpcmn011.txt5", str$(tpcmn010.txt5))

if not stpapi.insert("tpcmncm011m00",1,err.msg) = 1 then
stpapi.recover("tpcmncm011m00",recover.msg)
endif
stpapi.end.session("tpcmncm011m00")
suspend(2000)
borrar.tpcmn010(tpcmn010.cprj,tpcmn010.cuno,tpcmn010.nins)
}

function borrar.tpcmn010(domain tppdm.cprj cprj_b, domain tccuno cl_b, domain tppdm.nins nins_b)
{
db.retry.point()
select tpcmn010
from tpcmn010 for update
where tpcmn010._index1 = {:cprj_b,:cl_b,:nins_b}
selectdo
db.delete(ttpcmn010, db.retry)
commit.transaction()
endselect
}

function borrar.total.tpcmn010()
{
db.retry.point()
select tpcmn010.*
from tpcmn010 for update
selectdo
db.delete(ttpcmn010, db.retry)
commit.transaction()
endselect
}

function domain tcmcs.str8 calcular.hora()
{
domain tcmcs.str2 hora
domain tcmcs.str2 minutos
domain tcmcs.str2 segundos
domain tcmcs.int resto_hora
domain tcmcs.int resto_minutos
domain tcmcs.int resto_segundos

hora = STR$( time.num() / 3600 )
if len( hora ) = 1 then
hora = "0" & hora
endif

resto_hora = time.num() \ 3600

minutos = STR$( resto_hora / 60 )
if len( minutos ) = 1 then
minutos = "0" & minutos
endif

resto_minutos = resto_hora \ 60

segundos = STR$(resto_minutos)
if len( segundos ) = 1 then
segundos = "0" & segundos
endif

return( STR$(hora) & ":" & STR$(minutos) & ":" & STR$(segundos))
}

|*** end.source

mark_h
9th February 2010, 15:19
If single tasks work correctly there are a couple of things you can try - one put the suspend after the insert and before the end. And yes you might have to lengthen the time - also make sure that tpcmncm011m00 session has ended. But the first thing I would try - remove the stpapi.end.session and do not close the session until all records have be processed. I have had situation where opening and closing a session repeatedly caused errors eventually - I would then have to reprocess those records. What I did was open the session - process all the records and then end the session right before I ended the program. This might work for you.