You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Draft handling lets users save unfinished work — for example, a half-filled form — without committing it to the active database state. With abap2UI5 you can drive RAP draft-enabled business objects directly through EML.
6
+
Draft handling lets users save unfinished work — for example a half-filled form — without writing through to the active database state. In abap2UI5 apps you drive RAP draft-enabled business objects directly through EML, just like any other entity (see also [CDS, EML](./cds.md)).
7
7
8
-
###Modify a Draft
9
-
Use `MODIFY ENTITIES` with the `IN LOCAL MODE` and the draft addition to create or update draft instances:
8
+
#### Create a Draft
9
+
Use `MODIFY ENTITIES` with `%is_draft = if_abap_behv=>mk-on`to create a new draft instance:
10
10
```abap
11
-
MODIFY ENTITIES OF i_salesordertp
12
-
ENTITY salesorder
13
-
CREATE
14
-
FIELDS ( salesordertype salesorganization )
15
-
WITH VALUE #( ( %cid = `0001`
16
-
%is_draft = if_abap_behv=>mk-on
17
-
%data = VALUE #(
18
-
SalesOrderType = `TA`
19
-
SalesOrganization = `1010` ) ) )
20
-
MAPPED DATA(ls_mapped)
21
-
FAILED DATA(ls_failed)
22
-
REPORTED DATA(ls_reported).
11
+
METHOD z2ui5_if_app~main.
12
+
13
+
MODIFY ENTITIES OF i_salesordertp
14
+
ENTITY salesorder
15
+
CREATE
16
+
FIELDS ( salesordertype salesorganization )
17
+
WITH VALUE #( ( %cid = `0001`
18
+
%is_draft = if_abap_behv=>mk-on
19
+
%data = VALUE #(
20
+
SalesOrderType = `TA`
21
+
SalesOrganization = `1010` ) ) )
22
+
MAPPED DATA(ls_mapped)
23
+
FAILED DATA(ls_failed)
24
+
REPORTED DATA(ls_reported).
25
+
26
+
COMMIT ENTITIES.
27
+
28
+
ENDMETHOD.
29
+
```
30
+
31
+
#### Read a Draft
32
+
Set `%key-IsActiveEntity = abap_false` to read the draft instead of the active record:
33
+
```abap
34
+
READ ENTITIES OF i_salesordertp
35
+
ENTITY SalesOrder
36
+
FIELDS ( SalesOrderType )
37
+
WITH VALUE #( ( %key-SalesOrder = `0000004711`
38
+
%key-IsActiveEntity = abap_false ) )
39
+
RESULT DATA(lt_drafts).
23
40
```
24
41
25
-
### Activate a Draft
26
-
Promote the draft to the active state with the `Activate` action and commit:
42
+
####Activate or Discard
43
+
Promote the draft to the active state with the `Activate` action, or throw it away with `Discard`:
27
44
```abap
28
45
MODIFY ENTITIES OF i_salesordertp
29
-
ENTITY salesorder
46
+
ENTITY SalesOrder
30
47
EXECUTE Activate
31
-
FROM VALUE #( ( %key = ls_key
32
-
%is_draft = if_abap_behv=>mk-on ) )
33
-
MAPPED DATA(ls_mapped_act)
34
-
FAILED DATA(ls_failed_act)
35
-
REPORTED DATA(ls_reported_act).
48
+
FROM VALUE #( ( %key-SalesOrder = `0000004711`
49
+
%key-IsActiveEntity = abap_false ) )
50
+
FAILED DATA(ls_failed)
51
+
REPORTED DATA(ls_reported).
36
52
37
-
COMMIT ENTITIES BEGIN
38
-
RESPONSE OF i_salesordertp
39
-
FAILED DATA(ls_save_failed)
40
-
REPORTED DATA(ls_save_reported).
41
-
COMMIT ENTITIES END.
53
+
COMMIT ENTITIES.
42
54
```
43
55
44
-
For more on EML in abap2UI5 apps, see [CDS, EML](./cds.md).
45
-
46
56
::: tip
47
-
Draft tables hold exclusive locks for the draft owner. Combine draft handling with [Locks](./locks.md) only when you also need backend lock objects outside of RAP.
57
+
A draft-enabled BO holds an exclusive lock for its owner as long as the draft exists, so the app can stay stateless and the user can resume work later. If you need additional locks for non-draft objects, see [Locks](./locks.md).
If you are on **S/4HANA** or **BTP ABAP Environment (Steampunk)** and the business object you want to edit is already shipped by SAP as a draft-enabled BO (e.g. `I_SalesOrderTP`), you do not build your own BO and you do not create a draft table. SAP ships both. Your abap2UI5 app simply calls the standard BO via EML.
55
-
56
-
This sidesteps the whole lock-during-think-time problem.
"Activate" pressed -> Activate (now VBAK is written through)
66
-
"Discard" pressed -> Discard (drops the draft, releases lock)
67
-
```
68
-
69
-
**When to use this:**
70
-
- The business object you need is already a released, draft-enabled SAP BO
71
-
- You want classic Fiori-style "edit a draft, activate later" UX in an abap2UI5 app
72
-
73
-
**Key idea:** the session can stay **stateless**. The draft survives between roundtrips in SAP's own draft-shadow table. The lock is held by the BO framework as long as the draft exists — closing the browser without activating or discarding leaves the draft so the same user can resume it on the next visit. No `set_session_stateful( )`, no `ENQUEUE_*`, no custom Z table — SAP does all of that.
74
-
75
-
**Caveat:** field names (`SalesOrder`, `SalesOrderType`) match the released `I_SalesOrderTP` on current S/4HANA. On older releases the BO name or fields may differ — check the released-objects list in your system.
76
-
77
-
**If no standard BO exists** for your object — for instance because you are editing a custom Z business object — you would have to define your own draft-enabled RAP BO (with its own `draft table z…_d`, `lock master`, etc.) and consume that via EML in the same way. That is a separate topic; see the official [SAP RAP draft documentation](https://help.sap.com/docs/abap-cloud/abap-rap/draft).
78
-
79
-
---
80
-
81
-
### example
82
-
83
-
```abap
84
-
* Scenario 7 — Consume a standard SAP draft-enabled BO via EML
85
-
*
86
-
* You are on S/4HANA or BTP ABAP Environment. The sales order is
87
-
* already exposed by SAP as a draft-enabled BO (I_SalesOrderTP). You
88
-
* do NOT build your own BO and you do NOT create a draft table — SAP
89
-
* ships both.
90
-
*
91
-
* The flow the user sees:
92
-
* on_init -> Edit (create or resume the draft)
93
-
* "Save Draft" pressed -> UPDATE (writes the draft only)
94
-
* "Save Draft" again -> UPDATE (still draft, VBAK untouched)
95
-
* ...
96
-
* "Activate" pressed -> Activate (now VBAK is written)
97
-
* "Discard" pressed -> Discard (drops the draft, releases lock)
98
-
*
99
-
* The session stays stateless. The draft survives in SAP's own
100
-
* draft-shadow table between roundtrips. The lock is held by the BO
101
-
* framework as long as the draft exists; closing the browser without
102
-
* discarding leaves the draft so the same user can resume it later.
103
-
*
104
-
* Field names below (SalesOrder, SalesOrderType) match the released
105
-
* CDS view I_SalesOrderTP on current S/4HANA. On older releases the
106
-
* BO name or fields may differ — check the released-objects list in
107
-
* your system.
108
-
109
-
CLASS z2ui5_test_lock_07 DEFINITION PUBLIC.
110
-
111
-
PUBLIC SECTION.
112
-
INTERFACES z2ui5_if_app.
113
-
114
-
DATA sales_order TYPE c LENGTH 10 VALUE `0000004711`.
115
-
DATA sales_order_type TYPE c LENGTH 4.
116
-
DATA draft_open TYPE abap_bool.
117
-
DATA status_text TYPE string.
118
-
119
-
PROTECTED SECTION.
120
-
DATA client TYPE REF TO z2ui5_if_client.
121
-
122
-
METHODS on_init.
123
-
METHODS on_event_save_draft.
124
-
METHODS on_event_activate.
125
-
METHODS on_event_discard.
126
-
METHODS draft_acquire.
127
-
METHODS draft_read.
128
-
METHODS view_display.
129
-
130
-
PRIVATE SECTION.
131
-
ENDCLASS.
132
-
133
-
134
-
CLASS z2ui5_test_lock_07 IMPLEMENTATION.
135
-
136
-
METHOD z2ui5_if_app~main.
137
-
138
-
me->client = client.
139
-
140
-
IF client->check_on_init( ).
141
-
on_init( ).
142
-
ELSEIF client->check_on_event( `SAVE_DRAFT` ).
143
-
on_event_save_draft( ).
144
-
ELSEIF client->check_on_event( `ACTIVATE` ).
145
-
on_event_activate( ).
146
-
ELSEIF client->check_on_event( `DISCARD` ).
147
-
on_event_discard( ).
148
-
ENDIF.
149
-
150
-
ENDMETHOD.
151
-
152
-
153
-
METHOD on_init.
154
-
155
-
draft_acquire( ).
156
-
draft_read( ).
157
-
view_display( ).
158
-
159
-
ENDMETHOD.
160
-
161
-
162
-
METHOD draft_acquire.
163
-
164
-
" Edit is idempotent: if the user already has a draft for this
165
-
" sales order, SAP returns it instead of failing.
166
-
167
-
MODIFY ENTITIES OF i_salesordertp
168
-
ENTITY SalesOrder
169
-
EXECUTE Edit
170
-
FROM VALUE #( ( %key-SalesOrder = sales_order
171
-
%param-PreserveChanges = abap_true ) )
172
-
FAILED DATA(failed)
173
-
REPORTED DATA(reported).
174
-
175
-
IF failed IS NOT INITIAL.
176
-
draft_open = abap_false.
177
-
status_text = `Could not open draft — locked by another user, or no authorization.`.
178
-
RETURN.
179
-
ENDIF.
180
-
181
-
COMMIT ENTITIES.
182
-
183
-
draft_open = abap_true.
184
-
status_text = `Draft open — changes are saved as a draft until you press Activate.`.
185
-
186
-
ENDMETHOD.
187
-
188
-
189
-
METHOD draft_read.
190
-
191
-
IF draft_open = abap_false.
192
-
RETURN.
193
-
ENDIF.
194
-
195
-
READ ENTITIES OF i_salesordertp
196
-
ENTITY SalesOrder
197
-
FIELDS ( SalesOrderType )
198
-
WITH VALUE #( ( %key-SalesOrder = sales_order
199
-
%key-IsActiveEntity = abap_false ) )
200
-
RESULT DATA(drafts).
201
-
202
-
IF drafts IS NOT INITIAL.
203
-
sales_order_type = drafts[ 1 ]-SalesOrderType.
204
-
ENDIF.
205
-
206
-
ENDMETHOD.
207
-
208
-
209
-
METHOD on_event_save_draft.
210
-
211
-
" Persists progress to the draft only. VBAK is NOT touched.
212
-
" The user can press Save Draft many times across many roundtrips.
0 commit comments