eric.dizon
19th February 2016, 23:17
I am trying to use Dal.* commands that would trigger DAL validations on my code, but I cannot seem to find an example for searching a record and updating it after it found?

Please check if this code make sense?

dal.update("cxcsb010", tcxcsb010, l.res, TRUE DB.RETRY)
dal.set.field("cxcsb010.orno", cxcsp080.orno)
dal.set.field("cxcsb010.mser", l.clot)
dal.set.field("cxcsb010.mtmk", cxcsp080.ctst)
dal.set.field("cxcsb010.enmk", cxcsp080.cten)
dal.set.field("cxcsb010.tomt", cxcsp080.ctmt)
ret = dal.save.object("cxcsb010", l.err)
if ret = 0 then
commit.transaction()
else
abort.transaction()
dal.get.error.message(err)
message(err)
endif
endif

vahdani
20th February 2016, 01:19
You use a normal select statement to find the record that you want to change. This is basically the same as if you wanted to use the db.* functions. Following is your example but with DAL2 methods:

#include <bic_dam>

db.retry.point()
select cxcsb010.*
from cxcsb010 for update
where cxcsb010.xxxx = :yyyy
selectdo
dal.change.object("cxcsb010")
dal.set.field("cxcsb010.orno", cxcsp080.orno)
dal.set.field("cxcsb010.mser", l.clot)
dal.set.field("cxcsb010.mtmk", cxcsp080.ctst)
dal.set.field("cxcsb010.enmk", cxcsp080.cten)
dal.set.field("cxcsb010.tomt", cxcsp080.ctmt)
ret = dal.save.object("cxcsb010")
if ret = 0 then
commit.transaction()
else
abort.transaction()
show.dal.messages()
endif
selectempty
|Maybe add a new record??
dal.new.object("cxcsb010")
dal.set.field("cxcsb010.xxxx", yyyy)
...
ret = dal.save.object("cxcsb010")
if ret = 0 then
commit.transaction()
else
abort.transaction()
show.dal.messages()
endif
endselect

Ajesh
20th February 2016, 07:13
There is a command called dal.get.object and from the Programmers guide the descriptions given is


Syntax:
function long dal.get.object (string tbl.name, long lock [, string key_field1] [, string key_value1], ...)

Description

Reads a record of the given table. In case the lock parameter is set to true, the record is locked for update. This function accepts a list of primary key field/value pairs.

The sequence of the actions is as follows:

The key fields are set according to the passed list of fields
The before.get.object hook is called with the DAL_FIND option
The record is read or locked
Record level permission is checked
The after.get.object hook is called with the DAL_FIND option
In case this function is called in the context of a 4GL-Session for a table which is either defined as the maintable or a secondary table for this session, this function will check if the requested record is already present in the 4GL-Engine buffers. If so, the 4GL-Engine record buffer is fetched and will not be read from the database. This is especially useful when in a DAL script of a secondary table the corresponding maintable record is needed.

Note
The dal.get.object() function will return the table fields which are updated through the UI script or by the UI. However, in case these table fields are changed in a DAL script before the dal.get.object() function is called, the dal.get.object() will overwrite these changes.


Arguments
string tbl.name the table name of the DAL.

long lock if true the record is locked for update

[string key_field1]
[string key_value1]
... List of primary key field / value pairs in the format "ppmmm999.ffff", value. In case of array elements, specify the field as "ppmmm999.ffff(element)"


Return values
0 Record is read or locked for update
DALHOOKERROR One of the hooks blocked the read action
DALNOOBJPERM No record level permission
>0 The error code of the read action in case this failed (e.g. ENOREC)

Context
This function is implemented in the 4GL Engine and can be used in all script types.

Hooks called
before.open.object.set() if this is the first call to the DAL
before.get.object()
after.get.object()
Error Handling
In case a database error occurs (a return value greater than 0), then this function will set an error message. E.g. in case a record is not found then an error message is set. So if you want to ignore such an error, make sure that you reset the dal error message stack by calling dal.reset.error.messages() or dal.clear.error.messages().

Note
This function is can be used in Update Sessions as well as Integrations via OpenWorld. However, be aware that since this function calls the before.get.object() and after.get.object() hooks it cannot match the performance of a BaanSQL query. So in most cases, it is advised to write your own query.



You have to test the usage and i think it should be something like



dal.get.object("cxcsb010",TRUE,"cxcsb010.orno","cxcsp080.orno","cxcsb010.mser","l.clot")



Writing the above code would mean the below thing
1) TRUE - Indicates the Record is locked for update
2) "cxcsb010.orno","cxcsp080.orno" - The Primary Key-Value Pair one
3) "cxcsb010.mser","l.clot" - The Primary Key-Value Pair two

You may include more or less of the primary key/value combination depending upon your requirement.

The Return Value of the dal method is specified as
0 - If the Record is locked for Update
<> 0 - Error in case of Record Reading.

eric.dizon
22nd February 2016, 18:41
Thanks vahdani. I tried it and it worked great! Thanks for your suggestion Ajesh too, but is there a distinct difference between what Vahdani suggested or they the same thing? If I need to make my code use just DAL2 methods I would consider it or should be using dal.get.object() instead?

Ajesh
23rd February 2016, 06:36
Hi Eric

Both the suggestions are different. You could use both the suggestions and both will work equally good. The thing with programming in general is that you can use 100 ways to achieve or get the same result. Of course there would be differences between each method, some would take slightly more memory or computation resources etc, i.e. If you really want to go in detail.

You could try both methods. I do not have a Baan Login now otherwise i would have tested my Code.



#include <bic_dam>


long ret.val,ret
string mess.code


db.retry.point()

ret.val = dal.get.object("cxcsb010",TRUE,"cxcsb010.orno","cxcsp080.orno","cxcsb010.mser","l.clot")

If ret.val <> 0 then
dal.get.error.message(mess.code)
return(DALHOOKERROR)
endif

dal.set.field("cxcsb010.mtmk", cxcsp080.ctst)
dal.set.field("cxcsb010.enmk", cxcsp080.cten)
dal.set.field("cxcsb010.tomt", cxcsp080.ctmt)
ret = dal.save.object("cxcsb010")
f ret = 0 then
commit.transaction()
else
abort.transaction()
show.dal.messages()
endif

vahdani
23rd February 2016, 09:41
One learns a new thing every day!
I have done DAL prorgamming since it was intruduced and I never knew anything about dal.get.object() function! I did a search: apparantly this is used mostly in standard BDE programing/DLLs.

Another thing I noticed was this comment in the documentation:
So in most cases, it is advised to write your own query.

benito
23rd February 2016, 15:52
One learns a new thing every day!
I have done DAL prorgamming since it was intruduced and I never knew anything about dal.get.object() function! I did a search: apparantly this is used mostly in standard BDE programing/DLLs.<snipped>

same here! need to try this one out next time :)

eric.dizon
23rd February 2016, 18:24
I've learned a lot from this thread. Thanks for opening my eyes to the power of DAL2 commands.

sakthi kj
3rd July 2018, 07:19
I am fresher...…...:confused:

what is the result of this query....give me result plss…..



#include <bic_dam>


long ret.val,ret
string mess.code


db.retry.point()

ret.val = dal.get.object("cxcsb010",TRUE,"cxcsb010.orno","cxcsp080.orno","cxcsb010.mser","l.clot")

If ret.val <> 0 then
dal.get.error.message(mess.code)
return(DALHOOKERROR)
endif

dal.set.field("cxcsb010.mtmk", cxcsp080.ctst)
dal.set.field("cxcsb010.enmk", cxcsp080.cten)
dal.set.field("cxcsb010.tomt", cxcsp080.ctmt)
ret = dal.save.object("cxcsb010")
f ret = 0 then
commit.transaction()
else
abort.transaction()
show.dal.messages()
endif

Ajesh
3rd July 2018, 08:39
A specific record from cxcsb010 is locked for update, the record with key fields, as the current value of "cxcsb010.orno","cxcsp080.orno","cxcsb010.mser","l.clot"

And then specific fields like cxcsb010.mtmk,cxcsb010.enmk,cxcsb010.tomt are updated for that specific locked record.