Tip distinction between methods and operations and working in a stateless context
Unlike a method that works on an instance in stateful mode, an operation works in stateless mode. This means that the development partner must create the context in which it operates.
For technical reasons, the method is called on an instance, but this instance is not filled with a context. The context is given by the key value that must be controlled. When called as a service, no guarantee is given that the key really exists. See the examples below for additional information.
To implement a method called 'DELRECORD' that deletes a sales order, the code is as follows:
$METHODCase ACTIONWhen "DELRECORD"Gosub DELRECORD...Endcase# The method called DELRECORD has an argument (ORDNUM)# Let's first read the entity$DELRECORDARET_VALUE=fmet this.AREAD(ORDNUM)# If an error occur, stop: the "this" instance will not be filled, except for the AERROR structureIf find(ARET_VALUE,[V]CST_ERR, [V]CST_FATAL)ReturnEndif# Now we have our current order online, we can use the method (the return will indicate if there is an error)ARET_VALUE=fmet this.ADELETEReturn
Let's imagine that the method you want to implement does not require to read the class but just to update a line in the database.
For example, to trigger an operation that requests to send an email confirmation for a given sales order: the only action that has to be done for this email to be automatically sent at a later time by another task is to create a line in a 'MAILREQUEST' table that includes a document type (1=sales order, 2=sales invoice...), a document key, and the date of the request.
In this example, the "this" instance is neither used nor filled except if there is an error. In this case, the method 'ASETERROR' is called on the instance.
This method called 'SENDMAIL_REQUEST' would be implemented as follows:
$METHODCase ACTIONWhen "SENDMAIL_REQUEST"Gosub SENDMAIL_REQUEST...Endcase# The method called SENDMAIL_REQUEST has an argument (ORDNUM)# We just need to verify that this order exists and is in validated status (STAT=2)$SENDMAIL_REQUESTLocal File SALESORDER [ORD]Local File MAILREQUESTS [MAIL]Read [ORD]KEY=ORDNUMIf fstat<>0ARET_VALUE=Fmet this.ASETERROR("","The order"-num$(ORDNUM)-"does not exist",[V]CST_AERROR)Elsif [ORD]STAT=1ARET_VALUE=Fmet this.ASETERROR("","The order"-num$(ORDNUM)-"has not been validated",[V]CST_AERROR)Else[MAIL]MAILDATE=date$[MAIL]DOCTYPE=1[MAIL]DOCKEY=ORDNUMWrite [MAIL]If fstatARET_VALUE=Fmet this.ASETERROR("","The mail request could not been posted",[V]CST_AERROR)EndifEndifReturn