Side Effect Annotations: Examples

Side Effect Annotations: Examples

You define side effects either in the *MPC_EXT class or in the local annotation file.

Example: Annotating side effects in the method DEFINE of the class CL_MM_PUR_PO_AI_MAINT_MPC_EXT

Source Code

  
* define Side Effects for Purchase Order:
    DATA lo_ann_target TYPE REF TO /iwbep/if_mgw_vocan_ann_target.   " Vocabulary Annotation Target                     "#EC NEEDED
    DATA lo_annotation TYPE REF TO /iwbep/if_mgw_vocan_annotation.   " Vocabulary Annotation                            "#EC NEEDED
    DATA lo_collection TYPE REF TO /iwbep/if_mgw_vocan_collection.   " Vocabulary Annotation Collection                 "#EC NEEDED
    DATA lo_function   TYPE REF TO /iwbep/if_mgw_vocan_function.     " Vocabulary Annotation Function                   "#EC NEEDED
    DATA lo_fun_param  TYPE REF TO /iwbep/if_mgw_vocan_fun_param.    " Vocabulary Annotation Function Parameter         "#EC NEEDED
    DATA lo_property   TYPE REF TO /iwbep/if_mgw_vocan_property.     " Vocabulary Annotation Property                   "#EC NEEDED
    DATA lo_record     TYPE REF TO /iwbep/if_mgw_vocan_record.       " Vocabulary Annotation Record                     "#EC NEEDED
    DATA lo_reference  TYPE REF TO /iwbep/if_mgw_vocan_reference.    " Vocabulary Annotation Reference
 
 
    lo_reference = vocab_anno_model->create_vocabulary_reference( iv_vocab_id = '/IWBEP/VOC_COMMON'
                                                                  iv_vocab_version = '0001').
    lo_reference->create_include( iv_namespace = 'com.sap.vocabularies.Common.v1' ).
    lo_reference = vocab_anno_model->create_vocabulary_reference( iv_vocab_id = '/IWBEP/VOC_CORE'
                                                                  iv_vocab_version = '0001').
    lo_reference->create_include( iv_namespace = 'Org.OData.Core.V1' ).
 
    lo_ann_target = vocab_anno_model->create_annotations_target( 'MM_PUR_PO_AI_MAINTAIN.C_PurchaseOrderEnhWDType' ) ##NO_TEXT . "Add annotation term for VIPs introduced
 
    lo_annotation = lo_ann_target->create_annotation( iv_term = 'com.sap.vocabularies.Common.v1.SideEffects' ) ##NO_TEXT .
 
    lo_record     = lo_annotation->create_record( )  ##NO_TEXT.
    lo_property   = lo_record->create_property( 'SourceProperties' )   ##NO_TEXT.
    lo_collection = lo_property->create_collection( ).
 
    lo_collection->create_simple_value( )->set_property_path( 'Supplier' )  ##NO_TEXT .
    lo_collection->create_simple_value( )->set_property_path( 'CompanyCode' )  ##NO_TEXT .
    lo_collection->create_simple_value( )->set_property_path( 'DocumentCurrency' )  ##NO_TEXT .
    lo_collection->create_simple_value( )->set_property_path( 'PurchasingGroup' )  ##NO_TEXT.
    lo_collection->create_simple_value( )->set_property_path( 'PurchasingOrganization' )  ##NO_TEXT.

Example: User changes a source property and the system refreshes the price

Source Code

<Annotations Target="NAMESPACE.ENTITYTYPE">
<Annotation Term="com.sap.vocabularies.Common.v1.SideEffects" Qualifier="PriceChanged">
    <Record>
        <PropertyValue Property="SourceProperties">
            <Collection>
                <PropertyPath>Amount</PropertyPath>
                <PropertyPath>Discount</PropertyPath>
                <PropertyPath>Product</PropertyPath>
            </Collection>
        </PropertyValue>
      <PropertyValue Property="TargetProperties">
         <Collection>
            <PropertyPath>Price</PropertyPath>
         </Collection>
      </PropertyValue>
        <PropertyValue Property="EffectTypes" EnumMember="ValueChange" />
    </Record>
</Annotation>
</Annotations>

Example: User changes the supplier and the system refreshes the 1:1 navigation toSupplier

Source Code

<Annotations Target="NAMESPACE.ENTITYTYPE">
<Annotation Term="com.sap.vocabularies.Common.v1.SideEffects" Qualifier="SupplierChanged">
    <Record>
        <PropertyValue Property="SourceProperties">
            <Collection>
                <PropertyPath>Supplier</PropertyPath>
            </Collection>
        </PropertyValue>
      <PropertyValue Property="TargetEntities">
         <Collection>
            <NavigationPropertyPath>toSupplier</NavigationPropertyPath>
         </Collection>
      </PropertyValue>
        <PropertyValue Property="EffectTypes" EnumMember="ValueChange" />
    </Record>
</Annotation>
</Annotations>

Example: User changes a single property, and the system reads the whole entity due to field control

Source Code

<Annotations Target="NAMESPACE.ENTITYTYPE">
<Annotation Term="com.sap.vocabularies.Common.v1.SideEffects" Qualifier="PriceChanged">
    <Record>
        <PropertyValue Property="SourceProperties">
            <Collection>
                <PropertyPath>Status</PropertyPath>
            </Collection>
        </PropertyValue>
       <PropertyValue Property="TargetEntities">
         <Collection>
            <NavigationPropertyPath></NavigationPropertyPath>
         </Collection>
      </PropertyValue>
        <PropertyValue Property="EffectTypes" EnumMember="FieldControlChange" />
    </Record>
</Annotation>
</Annotations>

Example: Side effect on structural changes of a 1:n association

If any header information or other associated entity needs to be refreshed once a subitem has been created or deleted, you should add side effect annotations as shown in the example below:

Source Code

<Annotations Target="NAMESPACE.ENTITYTYPE">
<Annotation Term="com.sap.vocabularies.Common.v1.SideEffects" Qualifier="ReactOnItemCreationOrDeletion">
   <Record>
      <PropertyValue Property="SourceEntities">
         <Collection>
            <NavigationPropertyPath>toSalesOrderItems</NavigationPropertyPath>
         </Collection>
      </PropertyValue>
      <PropertyValue Property="EffectTypes" EnumMember="ValueChange"/>
      <PropertyValue Property="TargetProperties">
         <Collection>
            <PropertyPath>OverallAmount</PropertyPath>
         </Collection>
      </PropertyValue>
   </Record>
</Annotation>
</Annotations>

Example: Side effect after executing an action

After executing an action, but only if the returned entity is different from the entity for which the action was called, the related list binding is refreshed. Therefore, you need to define a side effect annotation for those cases in which any other entity or an association might be changed due to an action call. The target definition’s property path that may cover both properties and entities has to express a binding parameter name referring to the entity to which the action is bound.

Source Code

<Annotations Target="CA_OC_MANAGE_OR_ITEMS_SRV.CA_OC_MANAGE_OR_ITEMS_SRV_Entities/IssueOutput">
   <Annotation Term="com.sap.vocabularies.Common.v1.SideEffects">
      <Record>
         <PropertyValue Property="EffectTypes" EnumMember="ValueChange"/>
         <PropertyValue Property="TargetProperties">
            <Collection> 
                <PropertyPath>_it/to_OutputRequestItemStatus/OutputRequestItemStatus_Text</PropertyPath>
            </Collection>
         </PropertyValue>
      </Record>
   </Annotation>
</Annotations>

Example: Refresh the navigation target

In this case, when the item tax amount is changed, the navigation property leading to the root (to_SalesOrder) is updated.

Source Code

<Annotations Target="STTA_SALES_ORDER_WD_20_SRV.C_STTA_SalesOrderItem_WD_20Type">
               <Annotation Term="com.sap.vocabularies.Common.v1.SideEffects" Qualifier="TaxAmountChanged">
                      <Record>
                             <PropertyValue Property="SourceProperties">
                                    <Collection>
                                            <PropertyPath>TaxAmount</PropertyPath>
                                    </Collection>
                             </PropertyValue>
                             <PropertyValue Property="TargetEntities">
                                    <Collection>
                                            <NavigationPropertyPath>to_SalesOrder</NavigationPropertyPath>
                                    </Collection>
                             </PropertyValue>
                             <PropertyValue Property="EffectTypes" EnumMember="ValueChange"/>
                       </Record>
                </Annotation>