4
In several functions documented by Totvs there are optional parameters. I would like to create an optional parameter in my function, how to do?
I’m wanting to make the function updEnvio
receive a vector (from recno
s) as an optional parameter.
User Function updEnvio(cTabela)
Local cSQL := ""
Local cCampo := IIF(SUBS(cTabela,1,1)=='S', SUBS(cTabela,2,2), cTabela)
Local nStatus := 0
cSQL := ""
cSQL += " UPDATE " + RetSqlName(cTabela)
cSQL += " SET " + cCampo + "_YGSENV = 'S' "
cSQL += " WHERE 1=1 "
cSQL += " AND " + cCampo + "_FILIAL = '" + xFilial(cTabela) + "' "
cSQL += " AND " + cCampo + "_YGSENV <> 'S' "
nStatus := TcSqlExec(cSQL)
If (nStatus < 0)
conout("Oops, TCSQLError: " + TCSQLError())
Endif
Return
Roughly speaking, I wanted to transform it so that, if the vector was empty, it would update the whole table; however, if it had content, only the elements whose recno
s are described in that vector. Using the C++ logic to be, I would write the default value of the parameter in your statement. So my pseudo ADVPL code goes like this:
User Function updEnvio(cTabela, aRecnos := {})
Local cSQL := ""
Local cCampo := IIF(SUBS(cTabela,1,1)=='S', SUBS(cTabela,2,2), cTabela)
Local nStatus := 0
Local i
cSQL := ""
cSQL += " UPDATE " + RetSqlName(cTabela)
cSQL += " SET " + cCampo + "_YGSENV = 'S' "
cSQL += " WHERE 1=1 "
cSQL += " AND " + cCampo + "_FILIAL = '" + xFilial(cTabela) + "' "
cSQL += " AND " + cCampo + "_YGSENV <> 'S' "
If LEN(aRecnos) > 0
cSql += " AND R_E_C_N_O_ IN ("
cSql += STR(aRecnos[1])
For i := 2 to len(aRecnos)
cSql += "," + STR(aRecnos[i])
Next i
cSql += ")"
Endif
nStatus := TcSqlExec(cSQL)
If (nStatus < 0)
conout("Oops, TCSQLError: " + TCSQLError())
Endif
Return
When the workaround becomes official syntax
– Jefferson Quesado
@Jeffersonquesado edited, in fact it is still what I had created :D
– Maniero