Pass default values - factory action in RAP

                                        Refer - Factory action (copy records) in RAP

Steps to pass default values.

Step 1: Change behavior definition of header interface view.

managed implementation in class zbp_iv_ab_hdr_stu13 unique;

strict ( 2 );

with draft;

define behavior for ZIV_AB_HDR_STU13 alias Student

persistent table ztl_ab_hdr_stu13

draft table ztl_ab_hdr_dra13

lock master

total etag LastChDate

authorization master ( instance )

etag master LastChDate

{

create ( authorization : global );

update;

delete;

field ( numbering : managed, readonly ) StuId;

association _attach { create; with draft; }

draft action Activate;

draft action Discard;

draft action Edit;

draft action Resume;

draft determine action Prepare;

factory action CopyStudent[1];

static factory action CreateInstance[1];

mapping for ztl_ab_hdr_stu13

{

StuId = stu_id;

StuName = stu_name;

StuAge = stu_age;

StuCourse = stu_course;

StuCourDur = stu_cour_dur;

StuStatus = stu_status;

StuGender = stu_gender;

StuDob = stu_dob;

LastChDate = last_ch_date;

LastChTime = last_ch_time;

}

}


define behavior for ZIV_AB_ITM_STU13 alias attach

persistent table ztl_ab_itm_stu13

draft table ztl_ab_itm_dra13

lock dependent by _Student

authorization dependent by _Student

//etag master <field_name>

{

update;

delete;

field ( readonly ) StuId;

association _Student { with draft; }

mapping for ztl_ab_itm_stu13

{

AttchId = attch_id;

StuId = stu_id;

Comments = comments;

Attchment = attchment;

Mimetype = mimetype;

Filename = filename;

}

}

    
        * Check and activate. 

Step 2: Change behavior definition of header consumption view.

projection;

strict ( 2 );

use draft;

define behavior for zcv_ab_hdr_stu13 alias Student

{

use create;

use update;

use delete;

use action Activate;

use action Discard;

use action Edit;

use action Resume;

use action Prepare;

use action CopyStudent;

use action CreateInstance;

use association _attach { create; with draft; }

}


define behavior for zcv_ab_itm_stu13 alias attach

{

use update;

use delete;


use association _Student{ with draft; }

}

    
        * Check and activate. 

Step 3: Implement CreateIntsance method.

CLASS lhc_ZIV_AB_HDR_STU13 DEFINITION INHERITING FROM cl_abap_behavior_handler.

PRIVATE SECTION.


METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION

IMPORTING keys REQUEST requested_authorizations FOR ziv_ab_hdr_stu13 RESULT result.


METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION

IMPORTING REQUEST requested_authorizations FOR ziv_ab_hdr_stu13 RESULT result.

METHODS copystudent FOR MODIFY

IMPORTING keys FOR ACTION student~copystudent.

METHODS createinstance FOR MODIFY

IMPORTING keys FOR ACTION student~createinstance.


ENDCLASS.


CLASS lhc_ZIV_AB_HDR_STU13 IMPLEMENTATION.


METHOD get_instance_authorizations.

ENDMETHOD.


METHOD get_global_authorizations.

ENDMETHOD.


METHOD CopyStudent.

" Copy records assigned to internal table.

DATA: lt_students TYPE TABLE FOR CREATE ziv_ab_hdr_stu13 .

READ ENTITIES OF ziv_ab_hdr_stu13 IN LOCAL MODE

ENTITY Student

ALL FIELDS WITH CORRESPONDING #( keys )

RESULT DATA(lt_students1)

FAILED failed.

LOOP AT lt_students1 ASSIGNING FIELD-SYMBOL(<ls_students1>).

APPEND VALUE #( %cid = keys[ KEY entity %key = <ls_students1>-%key ]-%cid

%is_draft = keys[ KEY entity %key = <ls_students1>-%key ]-%param-%is_draft

%data = CORRESPONDING #( <ls_students1> EXCEPT StuId )

) TO lt_students ASSIGNING FIELD-SYMBOL(<ls_students>).


ENDLOOP.

"Create BO instance by copy.

MODIFY ENTITIES OF ziv_ab_hdr_stu13 IN LOCAL MODE

ENTITY Student

CREATE FIELDS ( StuName StuAge StuCourse StuCourDur StuStatus StuGender StuDob )

WITH lt_students

MAPPED DATA(mapped_create).

" Set values to front end.

mapped-student = mapped_create-student.

ENDMETHOD.


METHOD CreateInstance.

MODIFY ENTITIES OF ziv_ab_hdr_stu13 in LOCAL MODE

ENTITY Student

CREATE FROM VALUE #( for <ls_instance> in keys (

%cid = <ls_instance>-%cid

StuName = 'Ajay'

StuAge = '25'

StuCourse = 'CSE'

StuCourDur = 4

StuGender = 'M'

StuDob = sy-datum

%control = VALUE #(

StuName = if_abap_behv=>mk-on

StuAge = if_abap_behv=>mk-on

StuCourse = if_abap_behv=>mk-off

StuCourDur = if_abap_behv=>mk-off

StuDob = if_abap_behv=>mk-on

)

) )

MAPPED mapped

FAILED failed

REPORTED reported.


ENDMETHOD.


ENDCLASS.

    

        * Check and activate. 

Step 4: Change the consumption view and add action button.

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'Consumption view for Header'

@Metadata.ignorePropagatedAnnotations: true

define root view entity zcv_ab_hdr_stu13

provider contract transactional_query

as projection on ZIV_AB_HDR_STU13

{

@UI.facet:

[{

id : 'Student',

purpose: #STANDARD,

type: #IDENTIFICATION_REFERENCE,

label: 'Student',

position: 10

},

{

id : 'attach',

purpose: #STANDARD,

type: #LINEITEM_REFERENCE,

label: 'Attachments',

position: 20,

targetElement: '_attach'

}

]

@UI:

{

lineItem: [{ position : 1 , label : 'Student ID' },

{ position: 10 ,

type : #FOR_ACTION,

dataAction: 'CopyStudent',

label: 'CopyStudent' },

{ position: 15 ,

type : #FOR_ACTION,

dataAction: 'CreateInstance',

label: 'CreateInstance' }],

identification: [{ position: 10 , label : 'Student ID' }],

selectionField: [{ position: 10 }]

}

key StuId,

@UI:

{

lineItem: [{ position: 20 , label : 'Student Name'}],

identification: [{ position: 20 , label : 'Student Name' }],

selectionField: [{ position: 20 }]

}

StuName,

@UI:

{

lineItem: [{ position: 30 , label : 'Student Age'}],

identification: [{ position: 30 , label : 'Student Age' }]

}

StuAge,

@UI:

{

lineItem: [{ position: 40 , label : 'Student Course'}],

identification: [{ position: 40 , label : 'Student Course' }]

}

StuCourse,

@UI:

{

lineItem: [{ position: 50 , label : 'Student Course Duration'}],

identification: [{ position: 50 , label : 'Student Course Duration' }]

}

StuCourDur,

@UI:

{

lineItem: [{ position: 60 , label : 'Student Status'}],

identification: [{ position: 60 , label : 'Student Status' }]

}

StuStatus,

@UI:

{

lineItem: [{ position: 70 , label : 'Student Gender'}],

identification: [{ position: 70 , label : 'Student Gender' }]

}

StuGender,

@UI:

{

lineItem: [{ position: 80 , label : 'Student DOB'}],

identification: [{ position: 80 , label : 'Student DOB' }]

}

StuDob,

LastChTime,

LastChDate,

/* Associations */

_attach : redirected to composition child zcv_ab_itm_stu13

}

    
        * Check and activate. 

Step 5: Testing

        * Preview the application.

        * Click on create instance button.

        * Record is created.


            --> StuName,StuAge,StuDob behavior is ON so data is passed.

            --> StuCourse,StuCourseDur behavior is OFF, so data is not passed.

            --> behavior is not added to StuGender. By default taking as OFF.

********************************Thank you*****************************

Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).