I need to load the CK_XLOTE field by entering the product code in the CK_PRODUTO field, I am generating a function to perform this procedure, but it is not returning the batch in the field. Can anyone help me.
Follow the code
#INCLUDE 'PROTHEUS.CH'
#INCLUDE 'PARMTYPE.CH'
User Function TESTE()
//MsgAlert(M->CK_PRODUTO)
Local cQuery := ""
Local cLote := ""
cQuery := " SELECT TOP 1 "
cQuery += " SB8.B8_LOTECTL, "
cQuery += " SB8.B8_DTVALID "
cQuery += " FROM " + RetSQLName("SB8") + " AS SB8 "
cQuery += " WHERE SB8.B8_FILIAL = '" + xFilial("SB8") + "' "
cQuery += " AND SB8.B8_LOCAL = '01' "
cQuery += " AND SB8.B8_DTVALID > GETDATE() "
cQuery += " AND SB8.B8_SALDO > SB8.B8_EMPENHO "
cQuery += " AND SB8.B8_PRODUTO = '" + M->CK_PRODUTO + "' "
cQuery += " AND SB8.D_E_L_E_T_ = '' "
cQuery += " ORDER BY SB8.B8_DTVALID "
If SELECT("SQL") > 0
dbSelectArea("SQL")
dbCloseArea()
EndIf
cQuery := ChangeQuery(cQuery)
dbUseArea(.T., 'TOPCONN', TCGENQRY(,,cQuery),"SQL", .F., .T.)
dbSelectArea("SQL")
If SQL->(!EOF())
cLote := SQL->B8_LOTECTL
EndIf
return(cLote)