In several functions documented by Totvs there are optional parameters. I would like to create an optional parameter in my function, how to do it?
I want to make the updEnvio
function get a vector (of 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
In a rough way, 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 C ++ logic to be, I would write the default value of the parameter in its declaration. So my ADVPL pseudocode looks 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