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.
Implementing a method as an operation
To implement a method called 'DELRECORD' that deletes a sales order, the code is as follows:
- Create the context by using the 'AREAD' method.
- Use the standard 'ADELETE' method on the context that has been created.
$METHOD
Case ACTION
When "DELRECORD"
Gosub DELRECORD
...
Endcase
# The method called DELRECORD has an argument (ORDNUM)
# Let's first read the entity
$DELRECORD
ARET_VALUE=fmet this.AREAD(ORDNUM)
# If an error occur, stop: the "this" instance will not be filled, except for the AERROR structure
If find(ARET_VALUE,[V]CST_ERR, [V]CST_FATAL)
Return
Endif
# 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.ADELETE
Return
Implementing another method
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:
$METHOD
Case ACTION
When "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_REQUEST
Local File SALESORDER [ORD]
Local File MAILREQUESTS [MAIL]
Read [ORD]KEY=ORDNUM
If fstat<>0
ARET_VALUE=Fmet this.ASETERROR("","The order"-num$(ORDNUM)-"does not exist",[V]CST_AERROR)
Elsif [ORD]STAT=1
ARET_VALUE=Fmet this.ASETERROR("","The order"-num$(ORDNUM)-"has not been validated",[V]CST_AERROR)
Else
[MAIL]MAILDATE=date$
[MAIL]DOCTYPE=1
[MAIL]DOCKEY=ORDNUM
Write [MAIL]
If fstat
ARET_VALUE=Fmet this.ASETERROR("","The mail request could not been posted",[V]CST_AERROR)
Endif
Endif
Return