cancel
Showing results for 
Search instead for 
Did you mean: 

Adding auto increase number custom field to Sales Order Header

ayechan
Explorer
0 Kudos

Dear Experts,

I would like to add an auto increase numbering custom field with prefix value (eg. TEST-0000001, TEST-0000002, ...) in Sales Order header of s4hana cloud.

My current development is as following.

1. I added a custom field to Sales Order header screen using In-App Extensibility.

2. I created new custom BO to save current increase number.

3. Write logic in Modification of Header Custom Fields in Sales Documents (SD_SLS_MODIFY_HEAD) BAdI.

The logic is as following.

*set auto increase running no.

salesdocument_extension_out = salesdocument_extension_in.
DATA runningnum TYPE string.

IF salesdocument_extension_in-yy1_runningno_sdh IS INITIAL.
SELECT MAX( RunningNo ) FROM yy1_runningnumber INTO @DATA(current_max_id).
current_max_id = current_max_id + 1.

CONCATENATE 'TEST-' current_max_id INTO runningnum.
salesdocument_extension_out-yy1_runningno_sdh = runningnum.

* to save current increase number in Custom BO
CHECK cl_ble_http_client=>is_service_available(

communication_scenario = 'YY1_RUNNINGNUM_ACNK'

outbound_service = 'YY1_RUNNINGNUM_ACNK_REST'

) = abap_true.

* Creation of the HTTP client to access the outbound service

* Replace the values for communication_scenario and outbound_service as required.

DATA(lo_client) = cl_ble_http_client=>create(

communication_scenario = 'YY1_RUNNINGNUM_ACNK'

outbound_service = 'YY1_RUNNINGNUM_ACNK_REST'

).

* we get the csrf token to be able to make the POST call later

DATA(request) = cl_ble_http_request=>create( ).

DATA lv_s2 TYPE string VALUE '/YY1_RUNNINGNUMBER_CDS/YY1_RUNNINGNUMBER?$top=1'.

request->set_header_parameter( exporting name = 'X-CSRF-TOKEN' value = 'FETCH' ).

request->set_header_parameter( exporting name = 'Accept' value = 'application/json' ).

request->set_method( 'GET' ).

TRY .

DATA(response) = lo_client->send( request ).

DATA(lv_csrf) = response->get_header_parameter( 'x-csrf-token' ).
DATA(lv_lenght) = response->get_header_parameter( 'content-length' ).

*salesdocument_extension_out-yy1_dealid_sdh = l_v_status.

CATCH cx_ble_http_exception INTO DATA(l5).

data: lv_MSGV1(45) TYPE c,

lv_MSGV2(45) TYPE c.

lv_MSGV1 = l5->status_code.

*salesdocument_extension_out-yy1_csrf_sdh = lv_MSGV1.

lv_MSGV2 = l5->status_description.

ENDTRY.

DATA lv_json1 TYPE string VALUE '{"RunningNo":"'.

DATA lv_json2 TYPE string VALUE '"}'.

DATA lv_payload TYPE string.

CONCATENATE lv_json1 current_max_id lv_json2 INTO lv_payload.

*DATA: lv_payload TYPE string VALUE '{ "RunningNo": "10", "SalesOrder": "50" }'.

DATA(request1) = cl_ble_http_request=>create( ).

*DATA lv_s1 TYPE string VALUE 'YY1_RUNNINGNUMBER_CDS/YY1_RUNNINGNUMBER'.

request1->set_header_parameter( exporting name = 'x-csrf-token' value = lv_csrf ).

request1->set_content_type( 'application/json' ).

request1->set_method( 'POST' ).

request1->set_body( lv_payload ).

TRY .

DATA(response1) = lo_client->send( request1 ).

CATCH cx_ble_http_exception INTO DATA(lx1).

ENDTRY.

ENDIF.

 

This logic works for increasing number.

But, the problem here is the number is always increasing everytime I enter to Sales Order screen without saving or with saving.

So, the number keeps increasing even when I only enter to Sales Order screen just for looking information.

 

So, I would like to ask some questions.

For In-App Extensibility,

1. Is there any condition that can control to trigger the BAdI logic when only I click 'Save' button?

2. Is there any build-in auto increase number function that can be used in any BAdI?

 

For Developer Extensibility,

1. Is it possible to add an auto increase number custom field using ADT?

If it is possible to do, could you please share some information or give guidelines.

 

If there is any unclear points in my question, please ask me details.

And also, if there is any workarounds that can be fulfilled this request, please give me some advices.

Thank you.

Best Regards,

Aye Chan

Accepted Solutions (0)

Answers (0)