Árvore de páginas

The process form validation in ECM is performed through the customization of card index and process events using scripts in the JavaScript language. Events can be implemented in ECM itself or sent by Protheus as parameter of the functions used in the previous examples.

To validate the fields provided by the user, use the process beforeTaskSave event that runs on the confirmation of each task to be sent to the next step in the workflow.

To access the value of a form field, use the hAPI. getCardValue(<nomeCampo>) workflow API and, to handle exceptions, use the throw <message> command, which will present the message to the user preventing them from proceeding until they correct the information typed.

Event script in ECM

Simple validations can be performed in the event script to avoid unnecessary communication with Protheus immediately returning a message to the user.

 

function beforeTaskSave(colleagueId,nextSequenceId,userList){

var A1_COD = hAPI.getCardValue("A1_COD");

 

if (A1_COD == ""){

   throw "The Code field is mandatory"

}

}

Protheus

Validation in the event scripts using external applications is performed via WebServices that should be registered as service in ECM in the Control Panel > Services > Add option. When any WebService definition is changed or added, you will need to update ECM in Control Panel > Services > Edit > Refresh option.

The WebService can be registered through the ECM Control Panel or Protheus using the FWWFPutService function, as in the example below.

#include "totvs.ch"

 

User Function WSCliente()

Local cName := "WSCLIENTE"

Local cURL := "http://hostname:8080/ws/exemplo.apw?wsdl"

Local cDescription := "Service to be validated and updated in customer file"

 

If FWWFPutService(cName,cUrl,cDescription)

   MsgInfo("Service successfully registered")

EndIf

Return

WebService

For more information on how to develop Web Services with Microsiga Protheus, see the link: Web Services with ERP Microsiga Protheus.

Protheus WebService Example:

 

#include "totvs.ch"

#include "apwebsrv.ch"

 

WSSERVICE WSCLIENTE DESCRIPTION "Customer file example"

WSDATA A1_COD AS STRING

WSDATA A1_LOJA AS STRING

WSDATA XML AS Base64Binary

WSDATA RETEXISTCOD AS BOOLEAN

WSDATA RETINCLUIR AS BOOLEAN

WSMETHOD ExistCod DESCRIPTION "Checks if customer code already exists"

WSMETHOD Incluir DESCRIPTION "Adds customer"

ENDWSSERVICE

 

WSMETHOD ExistCod WSRECEIVE A1_COD,A1_LOJA WSSEND RETEXISTCOD WSSERVICE WSCLIENTE  

 

Private INCLUI := .T.

 

::RETEXISTCOD := existchav("SA1",::A1_COD+::A1_LOJA)

Return .T.

 

WSMETHOD Incluir WSRECEIVE XML WSSEND RETINCLUIR WSSERVICE WSCLIENTE

Local oXML := tXMLManager():New()

Local lRet := .T.

Local aDadosCli := {}

 

If oXML:Parse(::XML)

   aAdd( aDadosCli, { "A1_COD"     , oXML:XPathGetNodeValue("/SA1/A1_COD")   , NIL } )

   aAdd( aDadosCli, { "A1_LOJA"    , oXML:XPathGetNodeValue("/SA1/A1_LOJA")  , NIL } )

   aAdd( aDadosCli, { "A1_TIPO"    , oXML:XPathGetNodeValue("/SA1/A1_TIPO")  , NIL } )

   aAdd( aDadosCli, { "A1_PESSOA"  , oXML:XPathGetNodeValue("/SA1/A1_PESSOA"), NIL } )

   aAdd( aDadosCli, { "A1_NOME"    , oXML:XPathGetNodeValue("/SA1/A1_NOME")  , NIL } )

   aAdd( aDadosCli, { "A1_NREDUZ"  , oXML:XPathGetNodeValue("/SA1/A1_NREDUZ"), NIL } )

   aAdd( aDadosCli, { "A1_END"     , oXML:XPathGetNodeValue("/SA1/A1_END")   , NIL } )

   aAdd( aDadosCli, { "A1_MUN"     , oXML:XPathGetNodeValue("/SA1/A1_MUN")   , NIL } )

   aAdd( aDadosCli, { "A1_EST"     , oXML:XPathGetNodeValue("/SA1/A1_EST")   , NIL } )

 

   lMsErroAuto := .F.

   MSExecAuto( { | x, y | MATA030( x, y ) } , aDadosCli, 3 )

      

   If lMsErroAuto

      If __lSX8

         RollBackSX8()

      EndIf

      ::RETINCLUIR := .F.        

   Else

      If __lSX8

         ConFirmSX8()

      EndIf

      ::RETINCLUIR := .T.

   EndIf

Else

   SetSoapFault("WSCLIENTE:INCLUIR",oXML:LastError())

   lRet := .F.

EndIf   

Return lRet

 

Validation on the beforeTaskSave event script:

  1. Call the service load with the ServiceManager.getService method
  2. Use the getBean method to return the utility for access to the service classes through the instantiate method by sending as a parameter the Locator class that is described in ECM in Control Panel > Services, select the service and click View
  3. Call the method to instantiate the service (in this example getWSCLIENTESOAP) that is also described in the Control Panel > Services > View option
  4. Call the service method (in the example ExistCod)

 

function beforeTaskSave(colleagueId,nextSequenceId,userList){

var A1_COD = hAPI.getCardValue("A1_COD");

 

if (A1_COD == ""){

   throw "The Code field is mandatory"

}

 

var wsService = ServiceManager.getService("WSCLIENTE");

var serviceHelper = wsService.getBean();

var serviceLocator = serviceHelper.instantiate("localhost.WSCLIENTELocator");

var service = serviceLocator.getWSCLIENTESOAP();

var ret = false;

 

try {

   ret = service.EXISTCOD(A1_COD);

}

catch(erro){

   throw erro.message

}

 

if (!ret) {

   throw "Customer already exists";

}

}

MVC

The validation process of a MVC routine is performed using the FWWSMODEL WebService (http://hostexemplo:8080/ws/fwwsmodel.apw?wsdl) with the invocation of the GETXMLDATADETAIL method that returns the XML of the model to populate the data and the VLDXMLDATA method that will validate the XML.

 

#include "totvs.ch"

User Function WSMVC()

Local cName := "TOTVS_FWMVC"

Local cURL := "http://hostname:8080/ws/fwwsmodel.apw?wsdl "

Local cDescription := "TOTVS MVC Service for integrating routines/programs with ECM"

Local aService := FWWFGetService(cName)

 

If Empty(aService) .and. FWWFPutService(cName,cUrl,cDescription)

   MsgInfo("Service successfully registered")

EndIf

Return

 

Validation on the beforeTaskSave event script:

 

function beforeTaskSave(colleagueId,nextSequenceId,userList){

var wsService = ServiceManager.getService("TOTVS_FWMVC");

var serviceHelper = wsService.getBean();

var serviceLocator = serviceHelper.instantiate("br.com.totvs.webservices.fwwsmodel_apw.FWWSMODELLocator");

var service = serviceLocator.getFWWSMODELSOAP();

var err = {message:"", empty:true};

var ret, xml;

 

try {

   ret = service.GETXMLDATADETAIL([],"MATA030_MVC");

   xml = new XML(new String(new java.lang.String(ret)).replace(/<\?.*\?>/g,""));

}

catch(erro){

   throw erro.message;

   return;

}

 

updateXMLFields(xml,err);

 

if (err.message.length > 0){

   throw err.message;

}

else if (err.empty){

   throw "Fill out form";

}

else{

   eval("xml.@Operation = 3");

   try{

      service.VLDXMLDATA([],"MATA030_MVC",new java.lang.String(xml.toXMLString()).getBytes());

   } catch(e){

      throw e.message;

   }

}

}

 

function updateXMLFields(node,err){

var list = node.children();

var name,value;

 

for (var i=0;i<list.length();i++){

   switch (Trim(eval("list[i][email protected]()"))){

      case "FIELDS":

         updateXMLFields(list[i],err);

         break;

      default:

         name = list[i].name().localName;

         value = hAPI.getCardValue(name);

         if (value != null)

            list[i].value = convertValue(name,list[i],value,err);

         break;

   }

   if (err.message.length > 0)

      break;

}

}

 

function convertValue(name,struct,value,err){

var y,m,d,n,len,str,reg

var setYear = new Date().getFullYear().toString().substring(0,2);

var setDate = "dd/mm/yyyy";

var yCount = setDate.match(/yyyy/) ? 4 : 2;

var hasErr = false;

 

value = value.trim();

 

switch (eval("[email protected]()")){

case "D":

   if (value.replace("/","").trim() != ""){

      len = value.length();

      d = setDate.indexOf("d");

      m = setDate.indexOf("m");

      y = setDate.indexOf("y");

 

      if (d + 2 > len || m + 2 > len || y + yCount > len){

         hasErr = true;

      }

      else{

         str = value.substr(m,2) + value.substr(d,2);

         if (yCount == 4)

            str = value.substr(y,4) + str;

         else

            str = setYear + value.substr(y,2) + str;

 

         if (str.match(/((((19|20)(([02468][048])|([13579][26]))0229))|((19|20)[0-9][0-9])((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))/g))

            value = new java.lang.String(str);

         else

            hasErr = true;

      }

 

      if (hasErr)

         err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid date";

      else

         err.empty = false;

   }

   break;

case "N":

   len = eval("[email protected]().split(',')");

   if (len[1] == "0"){

      reg = new RegExp("^[0-9]{1,"+len[0]+"}$");

      str = value.replace(",","");

      if(!reg.test(str)){

         str = value.replace(".","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

 

      if (!hasErr){

         n = parseInt(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   else{

      reg = new RegExp("^0$|^0\.[0-9]{1,"+len[1]+"}$|^[0-9]{0,"+len[0]+"}(\.[0-9]{1,"+len[1]+"})?$");

      str = value.replace(".","").replace(",",".");

      if(!reg.test(str)){

         str = value.replace(",","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

            

      if (!hasErr){

         n = parseFloat(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   break;

default:

   if (value != "")

      err.empty = false;

   break;

}

return value;

}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

  • Sem rótulos