0% found this document useful (0 votes)
1K views15 pages

Generic Object Services

The document discusses how to add new options to the Generic Object Services (GOS) toolbar in SAP and handle those added options. It provides code snippets to add a new "Sales Orders List" option to the GOS toolbar for customers, and then how to call custom business logic by inheriting from the base GOS service class and redefining methods. The document also shows how to retrieve attachment details and contents that are associated with GOS toolbar objects.

Uploaded by

PAY16
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views15 pages

Generic Object Services

The document discusses how to add new options to the Generic Object Services (GOS) toolbar in SAP and handle those added options. It provides code snippets to add a new "Sales Orders List" option to the GOS toolbar for customers, and then how to call custom business logic by inheriting from the base GOS service class and redefining methods. The document also shows how to retrieve attachment details and contents that are associated with GOS toolbar objects.

Uploaded by

PAY16
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

http://www.sdn.sap.

com/irj/sdn/gos

Generic Object Services (GOS) Toolbar Part 1 : Add New Option in the Toolbar
Eloborates the option to add a new Menu Entry in the GOS toolbar

Generic Object Services (GOS) Toolbar offers the functions which are common to many SAP
applications. This powerful toolbar provides much functionality like create attachments, notes, mails; send
mails; list attachments and many more. You can find more functionality on Generic Object Services at
SAP Help on Generic Object Services.

The default GOS toolbar options are somewhat like this:

Sometimes, we need to add some new toolbar entries to facilitate the business requirement. For example,
in the Generic object toolbar add the new option like "Document List" which is generic to all business
objects and of course we need to handle that. In this post, we will see the steps and rules to be followed
to add a new Entry in the toolbar. In the next, post we will see how we can handle the added menu option
to fulfill our custom requirement.

To be more specific in explaining the requirement, we will take a business requirement to add a new
menu "Sales Orders List" for the GOS toolbar for Customer Master. At first we will see how we can just
bring this menu option at very last position. Like:
To bring the new entry (menu option) in this GOS toolbar, we need to maintain that in the
table SGOSATTR. This table can be maintained using SM30. 

 Class for Gen. Services: This is the driver class when we select the object service (menu
option) from the GOS toolbar. For time being we will keep this as the
CL_GOS_SRV_ATTACHMENT_LIST.
 Service Type: We will select 1 because we want Single entry menu option on the GOS toolbar.
There are options to create a main menu entry and submenus under that main menu by selecting
proper service type.
 Next Service: We will clear the value in this since we want our button at very last level. This next
service field must be set when we need to put the custom option in between other options.
 Control: We will select this, since we want to generate our output in the Custom Container.
 Commit Work: We will not select this option because we don’t want to commit the work.

Along with creating a new entry this way, we need to set this service as next service of the previous
service. In this case, our new service will come just under the "Help for object services" we need to
modify that service like this:
By selecting this option Sales Orders list from the GOS toolbar, we will see the attachment list being
generated by the class CL_GOS_SRV_ATTACHMENT_LIST:

NOTE: You might see the option grayed out if you don’t have any attachment created for that particular
customer. To create: Select the Create.. > Create Attachment. Select any file from your PC to add as
attachment.

To be able to bring that option in between of others, we need to play around with the Next Service value
of the buttons. For example, I want to put my button just under the "Attachment List" option and above
"Private Note". 
For this we need to:
1.Set the new option as the next Service for the "Attachment List"

2.Set the “Private Note” as the next service for the new option
3.Remove the next service from the option which will come at last. This is very important. 

After following these steps, the toolbar options will look like:

In the next post, we will see how we can use our custom class to generate our fully customized code i.e.
Instead of the class CL_GOS_SRV_ATTACHMENT_LIST, we will use custom class ZCL_SO_LIST.

Generic Object Services (GOS) Toolbar Part 2 : Handle added Service in the Toolbar
Describes the way to handle the added service in the Generic Object service Toolbar

As discussed in the post Generic Object Services (GOS) Toolbar Part 1 : Add New Option in the Toolbar,
today we will see how to call our custom business logic in the added new Service in GOS Toolbar. To be
able to process custom logic in the GOS toolbar service, we need to inherit out service class from the
GOS generic service class CL_GOS_SERVICE and put our logic in the inherited class.

Steps to follow:
1. Inherit a class from the CL_GOS_SERVICE:

2. Maintain the Entry in the Table SGOSATTR as discussed in the Generic Object Services (GOS)
Toolbar Part 1 : Add New Option in the Toolbar: 

3. Redefine the Method EXECUTE to process our custom business logic. Redefine the Method and
activate it. For demo purpose, we will not implement any logic in this but you can create a ALV or kind of
list as we get in the Attachement List.

Now, when we access the GOS toolbar, you will see the option “Sales Order List”. 
By accessing this option, you will get the empty screen like this:

You may have noticed that this Option is coming in all the GOS toolbars i.e. Toolbar in XD02 and XK02
and so on. To restrict the GOS toolbar service to only particular transaction, we need to redefine the
method CHECK_STATUS.

Code snippet for the CHECK_STATUS method:

Code Snippet to Redefine CHECK_STATUS


*& Redefinition of the Method CHECK_STATUS
METHOD CHECK_STATUS.
* Service active for customer
IF IS_LPORB-TYPEID = 'KNA1'.
EP_STATUS = MP_STATUS_ACTIVE.
ELSE.
EP_STATUS = MP_STATUS_INVISIBLE.
ENDIF.
ENDMETHOD.
Now, we will only get the service only in the XD02 and XD03 transactions.

Generic Object Services (GOS) Toolbar Part 3 : Add toolbar in Custom Program
Shows the code for how to add Generic Object service Toolbar in the Z program
Today, we will see how to get the GOS toolbar in the custom Report. 

GOS toolbar works on the Business Object. To be able get GOS toolbar, we need to have a Business
Object and Business Object Key. For example, to get the GOS toolbar for the customer, we need
Business object KNA1 and customer number as the key. So, if we want to have a GOS toolbar in our
custom transaction than we need to have a key before generating that. This is the reason; we don’t have
the GOS toolbar option in the standard SAP Create transactions like: XD01, VA01, MM01 and so on.

This test program will provide us the GOS toolbar for the Customer in our custom program (Z program).
This will provide us all the functionality of the GOS toolbar which are available in the XD02, XD03
transaction.

Code Snippet to Add GOS toolbar to Custom program


*& To Generate GOS toolbar in the Custom Transaction
*&-------------------------------------------------------------------*
REPORT ztest_np_gos.
DATA: lo_manager TYPE REF TO cl_gos_manager,
la_obj TYPE borident.
PARAMETERS: p_kunnr TYPE kna1-kunnr.
START-OF-SELECTION.
* Set object Key
la_obj-objtype = 'KNA1'.
la_obj-objkey = p_kunnr.
* GOS toolbar
CREATE OBJECT lo_manager
EXPORTING
is_object = la_obj
ip_no_commit = space
EXCEPTIONS
OTHERS = 1.
* To generate output
WRITE: 'GOS test for Customer'.

Now, we will get the GOS toolbar which will look like this:

Generic Object Services (GOS) Toolbar Part 4 : Get Attachements


Shows how to get the Attachement details attached to the GOS toolbar
Today, we will see how to get the attachement details. 

We will use the static method READ_LINKS from the class CL_BINARY_RELATION. We have to pass
what kind of the objects we want to get back from the all the attachements like Notes, Attachment,
External document etc.

This code will show how to get the NOTES attachments from the GOS toolbar.

Code Snippet to get the GOS attachments NOTES


*& Report ZTEST_NP_GOS_ATT
*& Read the GOS attachments
REPORT ztest_np_gos_note.
PARAMETERS: p_matnr TYPE mara-matnr.
START-OF-SELECTION.
* Get all the NOTE attached to Business object buseinss object key
DATA: gs_lpor TYPE sibflporb.
gs_lpor-instid = p_matnr.
gs_lpor-typeid = 'BUS1001006'.
gs_lpor-catid = 'BO'.
* attachment type selection
DATA: lt_relat TYPE obl_t_relt,
la_relat LIKE LINE OF lt_relat.
la_relat-sign = 'I'.
la_relat-option = 'EQ'.
la_relat-low = 'NOTE'. "For notes
APPEND la_relat TO lt_relat.
* Read the links
DATA: t_links TYPE obl_t_link,
la_links LIKE LINE OF t_links.
DATA: lo_root TYPE REF TO cx_root.
TRY.
CALL METHOD cl_binary_relation=>read_links
EXPORTING
is_object = gs_lpor
it_relation_options = lt_relat
IMPORTING
et_links = t_links.
CATCH cx_root INTO lo_root.
ENDTRY.

To test this program:


Go to MM02
Enter Material and go to Basic data view 1
In GOS toolbar, Create > Note

Now, run the above program to get the attachment details.

The same way we can select attachements by adding the relation option ATTA.
Like:
Code Snippet to get the GOS attachments
la_relat-sign = 'I'.
la_relat-option = 'EQ'.
la_relat-low = 'ATTA'. "Attachements
APPEND la_relat TO lt_relat.

Generic Object Services (GOS) Toolbar Part 5 : Get Note attachment contents
Shows how to get the contents of the NOTE attachements
Today, we will see how to get the contents of the NOTE attachements.

Sometimes, we need to give the options for the attachement List when we design the some application
with GOS toolbar. We will use the static method READ_LINKS from the class CL_BINARY_RELATION.

Code Snippet to Read the GOS attachments


*& Read the GOS attachments
REPORT ztest_np_gos_note.
PARAMETERS: p_matnr TYPE mara-matnr.
START-OF-SELECTION.
* Get all the NOTE attached to Business object
* business object key
DATA: gs_lpor TYPE sibflporb.
*
gs_lpor-instid = p_matnr.
gs_lpor-typeid = 'BUS1001006'.
gs_lpor-catid = 'BO'.
* attachment type selection
DATA: lt_relat TYPE obl_t_relt,
la_relat LIKE LINE OF lt_relat.
*
la_relat-sign = 'I'.
la_relat-option = 'EQ'.
la_relat-low = 'NOTE'.
APPEND la_relat TO lt_relat.
* Read the links
DATA: t_links TYPE obl_t_link,
la_links LIKE LINE OF t_links.
*
DATA: lo_root TYPE REF TO cx_root.
*
TRY.
CALL METHOD cl_binary_relation=>read_links
EXPORTING
is_object = gs_lpor
it_relation_options = lt_relat
IMPORTING
et_links = t_links.
CATCH cx_root INTO lo_root.
ENDTRY.
* Read NOTE contents
DATA l_folder_id TYPE soodk.
DATA l_object_id TYPE soodk.
DATA document_id TYPE sofmk.
* Get document id
READ TABLE t_links INTO la_links INDEX 1.
*
document_id = la_links-instid_b.
* Set folder
l_folder_id-objtp = document_id-foltp.
l_folder_id-objyr = document_id-folyr.
l_folder_id-objno = document_id-folno.
* Set Object
l_object_id-objtp = document_id-doctp.
l_object_id-objyr = document_id-docyr.
l_object_id-objno = document_id-docno.
* Read the document content
DATA document_content TYPE STANDARD TABLE OF soli.
*
CALL FUNCTION 'SO_OBJECT_READ'
EXPORTING
folder_id = l_folder_id
object_id = l_object_id
TABLES
objcont = document_content
EXCEPTIONS
active_user_not_exist =1
communication_failure =2
component_not_available = 3
folder_not_exist =4
folder_no_authorization = 5
object_not_exist =6
object_no_authorization = 7
operation_no_authorization = 8
owner_not_exist =9
parameter_error = 10
substitute_not_active = 11
substitute_not_defined = 12
system_failure = 13
x_error = 14
OTHERS = 15.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
To test this program:
Go to MM02
Enter Material and go to Basic data view 1
In GOS toolbar, Create > Note

Now, run the above program to get the attached NOTE contents.
Generic Object Services (GOS) Toolbar Part 6 : Copy Attachments between objects
Copy GOS attachment from one object to another object
Sometimes it is required to copy the attachments from one object to another object. For example, we
want to attach the customer's purchase order to Sales Order and Billing document once the billing
document is generated. 

To copy the attachments between the objects, we can use the utility method COPY_LINKED_OBJECTS
of the class CL_GOS_SERVICE_TOOLS. We need to tell the method using IT_SERVICE_SELECTION,
which service needs to be performed i.e. for the attachment copy, we need to perform the "Attachment
Create" service. We also let the method know the the source and target.

Here is the Code snippet which copies the attachments between Customers.

Code Snippet to copy GOS Attachment

*& Purpose : To Copy the Attachment from One Object to Another


*
REPORT ztest_np_copy_gos_att.
DATA: lt_services TYPE tgos_sels. " Services table typ
DATA: ls_service TYPE sgos_sels. " Services structure type
DATA: ls_source TYPE sibflporb, " Source
ls_target TYPE sibflporb. " Target
START-OF-SELECTION.
* Service needs to be performed
ls_service-sign = 'I'.
ls_service-option = 'EQ'.
ls_service-low = 'PCATTA_CREA'.
APPEND ls_service TO lt_services.
* Source
ls_source-instid = 'CUST1'.
ls_source-typeid = 'KNA1'.
ls_source-catid = 'BO'.
* Target
ls_target-instid = 'CUST2'.
ls_target-typeid = 'KNA1'.
ls_target-catid = 'BO'.
* Copy the objects between Source and Target
cl_gos_service_tools=>copy_linked_objects(
is_source = ls_source
is_target = ls_target
it_service_selection = lt_services
).
*
COMMIT WORK.

Generic Object Services (GOS) Toolbar Part 7 : Relationship Browser


Relationships in GOS Toolbar
In the series of Generic Object Services(GOS) available in the SAP, we have discussed different usage of
the GOS toolbar. You can find all of these discussions at Tutorials > Generic Object Services (GOS).

Today, we'll see the another concept: Relationship browser. Relationships option is available in the GOS
Toolbar at GOS toolbar > Relationships.
This browser could display different linked objects like Sales Documents, Inbound IDOCs, Outbound
IDOcs etc. 

Now lets see how to add this Relationships in this relationship browser. For this purpose, we can use
method CREATE_LINK of the class CL_BINARY_RELATION or the FM BINARY_RELATION_CREATE. 

Method CREATE_LINK is an advanced version to create the Link which would be displayed in the
Relationship browser. Since the method CREATE_LINK is an improved version to create the links, it
doesn't support all the object models. For the remaining of the object models, we need to use the FM
BINARY_RELATION_CREATE. So, the question is how to know when to use FM. 

When the relation model is not supported by the method CREATE_LINK, it raises the exception
CX_OBL_MODEL_ERROR. So, we'll call the method CREATE_LINK inside the TRY ... CATCH ...
ENDTRY block. 

This Code Snippet shows how to use the method to create links in the relationship browser.

Code Snippet

*& Purpose : To Create Relation between Business Objects


REPORT zgos_relation_create.
*
CLASS lcl_relation DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF ty_related.
INCLUDE TYPE sibflporb.
TYPES:
relation TYPE oblreltype,
END OF ty_related.
TYPES:
ty_t_related TYPE STANDARD TABLE OF ty_related.
METHODS:
constructor
IMPORTING
obj_no TYPE sibfboriid
obj_type TYPE sibftypeid
obj_cat TYPE sibfcatid.
METHODS:
add_relation
IMPORTING
obj_no TYPE sibfboriid
obj_type TYPE sibftypeid
obj_cat TYPE sibfcatid
relation TYPE oblreltype.
METHODS:
create_relations.
PRIVATE SECTION.
DATA:
t_related TYPE ty_t_related, "sibflporbt,
my_prop TYPE sibflporb.
ENDCLASS. "lcl_relation DEFINITION
*
CLASS lcl_relation IMPLEMENTATION.
METHOD constructor.
* Set Properties of Referent
me->my_prop-instid = obj_no.
me->my_prop-typeid = obj_type.
me->my_prop-catid = obj_cat.
ENDMETHOD. "constructor
METHOD add_relation.
* Add Referenc
FIELD-SYMBOLS: <lfs_relat> LIKE LINE OF me->t_related.
APPEND INITIAL LINE TO me->t_related ASSIGNING <lfs_relat>.
<lfs_relat>-instid = obj_no.
<lfs_relat>-typeid = obj_type.
<lfs_relat>-catid = obj_cat.
<lfs_relat>-relation = relation.
ENDMETHOD. "add_relation
*
METHOD create_relations.
DATA: lwa_relate_key TYPE sibflporb.
FIELD-SYMBOLS: <lfs_relat> LIKE LINE OF me->t_related.
*
DATA:
ls_parent TYPE borident,
ls_related TYPE borident,
lv_relation TYPE binreltyp,
lx_obl TYPE REF TO cx_obl,
lp_errstr TYPE string,
lv_done TYPE flag.
* for each relation
LOOP AT me->t_related ASSIGNING <lfs_relat>.
MOVE-CORRESPONDING <lfs_relat> TO lwa_relate_key.
TRY.
* First try with new method to create the Link
CALL METHOD cl_binary_relation=>create_link
EXPORTING
is_object_a = my_prop
is_object_b = lwa_relate_key
ip_reltype = <lfs_relat>-relation.
lv_done = 'X'.
* If the Link can not be handled by this class, call the
* FM to create the link
CATCH cx_obl_model_error.
ls_parent-objkey = me->my_prop-instid.
ls_parent-objtype = me->my_prop-typeid.
ls_related-objkey = <lfs_relat>-instid.
ls_related-objtype = <lfs_relat>-typeid.
lv_relation = <lfs_relat>-relation.
*
CALL FUNCTION 'BINARY_RELATION_CREATE'
EXPORTING
obj_rolea = ls_parent
obj_roleb = ls_related
relationtype = lv_relation
EXCEPTIONS
no_model =1
internal_error = 2
unknown =3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
lv_done = 'X'.
ENDIF.
CATCH cx_obl INTO lx_obl.
lp_errstr = lx_obl->get_text( ).
MESSAGE lp_errstr TYPE 'S'.
ENDTRY.
*
IF lv_done = 'X'.
WRITE: / 'Relation added: ', <lfs_relat>-instid(10), <lfs_relat>-relation.
ENDIF.
ENDLOOP.
COMMIT WORK.
ENDMETHOD. "create_relations
ENDCLASS. "lcl_relation IMPLEMENTATION
*
START-OF-SELECTION.
DATA: o_relation TYPE REF TO lcl_relation.
* Sales Order as the Referent
CREATE OBJECT o_relation
EXPORTING
obj_no = '1000010010' " Sales Order Number
obj_type = 'BUS2032' " Sales Order
obj_cat = 'BO'.
* Add Delivery to Sales Order
o_relation->add_relation(
obj_no = '8000020000'
obj_type = 'LIKP'
obj_cat = 'BO'
relation = 'LINK' ). " Link
* Add IDOC to Sales Order
o_relation->add_relation(
obj_no = '1000012345'
obj_type = 'IDOC'
obj_cat = 'BO'
relation = 'IDC0' ). " IDOC
* Create Relations
o_relation->create_relations( ).

After successful program run, it will display the relationship in the browser of the Order.

You might also like