Showing posts with label ABAP. Show all posts
Showing posts with label ABAP. Show all posts

Jan 15, 2016

ABAP: Send Email with attached Internal Table XLS file

Function module that will take any internal table as input,  convert in to XLS format
and send out emails with the attachment.

 Add the appropriate import parameters.

Add appropriate table parameters.









Source Code
====================================================
function zinttab_excl_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(SENDER) TYPE  AD_SMTPADR
*"     REFERENCE(SUBJECT) TYPE  SO_OBJ_DES
*"  TABLES
*"      RECEPIENTS TYPE  BCSY_SMTPA
*"      RETURN TYPE  TABLE_OF_STRINGS OPTIONAL
*"      DATA_TO_SEND
*"      EMAIL_BODY TYPE  BCSY_TEXT
*"----------------------------------------------------------------------
*Function module that will take any internal table as input,  convert in to XLS format
*and send out emails with the attachment. :SATYABRATA SAHOO
*"----------------------------------------------------------------------
  if recepients[] is initial.
    return.
  endif.

  datalo_send_request   type ref to cl_bcs,
        lo_document       type ref to cl_document_bcs,
        lo_sender_id      type ref to if_sender_bcs,
        lo_recipient      type ref to if_recipient_bcs.
  datalo_bcs_exception  type ref to cx_bcs.
  datalv_bcs_message    type string,
        lv_send_to_all    type os_boolean,
        lv_fields(10).

  field-symbols< fs_recipient > type ad_smtpadr.

*******************************************************************************
* build the file content

  datalo_struct_type  type ref to cl_abap_structdescr,
        lo_element_type type ref to cl_abap_elemdescr.
  datalt_components   type cl_abap_structdescr=>component_table,
        ls_field        type dfies.
  field-symbols type abap_componentdescr.

  datalt_binary_text  type solix_tab,
        lv_text         type string,
        lv_size         type so_obj_len.

* build the header data
  lo_struct_type ?= cl_abap_typedescr=>describe_by_datadata_to_send ).
  lt_components   lo_struct_type->get_components).
  loop at lt_components assigning .
    if sy-tabix gt 1.
      concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
    endif.
    lo_element_type ?= -type.
    ls_field         lo_element_type->get_ddic_field).
    concatenate lv_text ls_field-scrtext_l into lv_text.
  endloop.
  concatenate lv_text cl_bcs_convert=>gc_crlf into lv_text.

* build the structure of the body
  field-symbols   type any,
                  type any.
  loop at data_to_send assigning .
    loop at lt_components assigning .
      if sy-tabix gt 1.
        concatenate lv_text cl_bcs_convert=>gc_tab into lv_text.
      endif.
      assign component -name of structure  to .
      lv_fields .
      condense lv_fields.
*      concatenate lv_text  into lv_text.
      concatenate lv_text lv_fields into lv_text.
    endloop.
    concatenate lv_text cl_bcs_convert=>gc_crlf into lv_text.
  endloop.

  try .
      cl_bcs_convert=>string_to_solix(
        exporting
          iv_string   lv_text
          iv_codepage '4103' "suitable for MS Excel, leave empty"
          iv_add_bom  abap_true
        importing
          et_solix    lt_binary_text
          ev_size     lv_size ).
    catch cx_bcs into lo_bcs_exception.
      lv_bcs_message lo_bcs_exception->get_text).
      append lv_bcs_message to return.
      exit.
  endtry.

*******************************************************************************
* send an email
  try .
      clearlo_send_requestlo_documentlo_sender_id.
      lo_send_request cl_bcs=>create_persistent).
      lo_document     cl_document_bcs=>create_document(
                                  i_type    'RAW'
                                  i_text    email_body[]
                                  i_subject subject ).

      if not data_to_send[] is initial.
        lo_document->add_attachment(
                   i_attachment_type    'XLS'
                   i_attachment_subject subject
                   i_attachment_size    lv_size
                   i_att_content_hex    lt_binary_text ).
      endif.

      lo_send_request->set_documentlo_document ).


      lo_sender_id    cl_cam_address_bcs=>create_internet_addresssender ).

      lo_send_request->set_senderlo_sender_id ).
*     add recipients
      loop at recepients assigning .
        clear lo_recipient.
        lo_recipient cl_cam_address_bcs=>create_internet_address ).
*       add recipient with its respective attributes to send request
        lo_send_request->add_recipienti_recipient lo_recipient
                                        i_express   abap_true ).
      endloop.

      lo_send_request->set_status_attributesi_requested_status 'E'
                                              i_status_mail      'E' ).
      lo_send_request->set_send_immediatelyabap_true ).
      lv_send_to_all lo_send_request->sendi_with_error_screen abap_true ).

      if lv_send_to_all eq abap_true.
        datals_return like line of return.
        ls_return text-001"Success
        append ls_return to return.
      endif.
      commit work.
    catch cx_bcs into lo_bcs_exception.
      lv_bcs_message lo_bcs_exception->get_text).
      append lv_bcs_message to return.
      exit.
  endtry.
endfunction.

============================================

Ref-http://techsplice.com/

Dec 18, 2015

SAP ABAP FAQs- Part 3 (Object Oriented ABAP)

SAP OOABAP Interview Questions Answers

1. Explain about Object oriented programming?

Object oriented programming is one of the most popular methodologies in
software development. It offers a powerful model for creating computer programs.
It speeds the program development process, improves maintenance and enhances
Reusability of programs.

2. Object oriented Concepts?

Encapsulation:
It means combining data and code that acts up on data into one single unit of
organization, so that both are safe from out side interference. In OOAbap
encapsulation is achieved through classes.

Polymorphism:
It means many forms that mean the same method behaves differently in different
method calls.

Inheritance:
It means deriving a child class from base class; child class acquires all the
properties from base class.

Abstraction:
Abstraction simplifies a complex problem to a simpler problem by specifying and
modeling the class to the relevant problem scenario.
It simplifies the problem by giving the class its specific class of inheritance.
Composition also helps in solving the problem to an extent.

3. What is a class?

Classes are templates for objects. Conversely, you can say that the type of an
object is the same as its class. A class is an abstract description of an object.
You could say that it is a set of instructions for building an object. The attributes
of objects are defined by the components of the class, which describe the state
and behavior of objects.

4. What is an object?

An object is a combination of messages and data. Objects can receive and send
messages and use messages to interact with each other. The messages contain
information that is to be passed to the recipient object.

5. How many types of classes are there in OOABAP?

Public class
Private class
Final class
Singleton
class
Abstract class
Persistent class
Friend class

6. What is the difference between function group and classes?

We can create many instances of the same class with in a program, but we cannot
create many instances of function group.

7. What are the differences local & global classes?

Local classes are defined locally with in a program and the other programs can’t
access the same classes directly.
But global classes are not like that they are globally accessible from ABAP
environment. Global classes are centrally defined in a repository. Transaction code
for global classes is SE24(class builder).

8. What are the Components of a class?

Attributes and methods are components inside a class.

9. How to define a class locally?

class definition.
Public section.
Methods: m1 importing p1 type
Exporting p2 type
Changing p3 type
Returning p4 type
Exceptions .
Protected section.
Private section.
Endclass.
Class implementation.
Method m1.
Endmethod.
Endclass.

10. What is a constructor & types of constructors?

Constructor is a special method, which will be called automatically as and when
the object is created for a class, it can have only importing parameters but not
exporting, it is generally used to give some initial state to the object.
A class can contain have two types of constructors static and instance
constructors.

11. Difference between static and instance constructors?

Static constructor will be called only once, i.e. at the time of loading class in to
memory. Instance constructors are instance specific, these constructors will be
called as and when the new object is created for that class.

12. How to a create object for the class?

Data: ref type ref to .
Create object ref.
13. how to call a method?
Call method ref>
method_name .

14. What is static attribute & method?

Static attributes & methods are class specific, memory will be allocated only once
for Static attributes & methods irrespective of no. of objects created.
We can access the components with a reference variable i.e. by using class name
Call method =>menthod_name.

15. Can we instantiate a class within implementation of other class?
Yes
16. Can we put non declarative statement e.g. STARTOFSELECTION
within a class?

No, we can’t use.

17. How to create a global class?

With tcode SE24

18. How can we pass importing parameter?

Pass by value/pass by reference

19. Can we pass returning parameter by reference?

NO, only pass by value

20. Can a method call itself?
Yes

21. What is me variable?

It just like a selfreference, by this we can call methods that are with in same class
with out creating object.

22. Can we have export parameter in Instance constructor? No

23. What is an abstract class?

Abstract class contains both abstract methods and normal methods, abstract
methods cannot implement in side abstract class, and instead these methods will
be implemented by child classes of that abstract class.

24. What is final class & Method?

Final classes can’t be inherited that means it can not have child classes and final
method of a class cannot be redefined.

25. What is an interface?
Interfaces contain only public methods with no implementation; these interfaces
are included in public section of classes and implement the methods of interfaces.

26. Can we implement interface in private section of any class?
No

26. What is alias?

Instead of specifying full name of interface methods, we can assign it a name
which can directly trigger.

28. What is a friend class?
Friend class is a class it can access private components of it’s friends class.

29. How to create an object for private class?

In general we can not create object for a private class, but we can access static
method of a private class so call that method using its class name and import that
object.
For example take one static method with an exporting parameter inside private
class and write object creation code in that static method and export that object.

30. What is a Singleton class?

Singleton classes can be instantiated only once, i.e. only one object is created for
Singleton classes.

31. What is a Persistent class?

A special class, the attributes of which are linked to database tables via objectrelational
mapping. Since Release 6.10 they can be created using the Mapping Assistant of the Class Builder.
The objects of persistent classes are managed by Object Services. An object in a
persistent class, the attributes of which are saved as database content after the

run time of an ABAP program.

Oct 6, 2015

SAP ABAP FAQs- Part 2

Q: Steps to create Enhancement Spot/ BADI

1. Go to se18.
2. Enter the name enhancement spot and click create.
3. On next screen click on create new BADI.
4. Once the BADI is created then expand the tree on the left side.
5. Then double click on interface and give the name of the interface.
6. On the next screen it will ask for interface methods and its level ,that is it static or instance ?
7. Go to se19, and then click on enhancement spot ,enter the name of enhacement spot created above and click on  implement.
8. On next pop up give the name of enhancement implementation.
9. On next screen give the name of BADI implementation and its implementation class and also BADI definition just created above and save.
10. Now back to se18 and there activate both enhancement spot and BADI implementaion.And see both BADI interface and implementation.

Q: How to find BADI/Exit calls in any transaction?

Sometimes it is not so easy to find a BADI method or user exit to implement at an exact momment of the execution of a program. Here is one of the ways that can be used to spot which BADI method or user exit should be implemented.
1. Enter SE24;
2. Enter the class name CL_EXITHANDLER;
3. Double-click the method GET_INSTANCE;
5. Set a break-point at command line CASE sy-subrc (near line 25);
6. Execute the transaction you want to analyse; It will stop at the break-point you have just set on class CL_EXITHANDLER when it finds any BADI method/Exit call.
7. On debug screen, type the field name exit_name. This field contains the BADI method/Exit name which is being called in the program on that momment.
8. Press F8 to see the next calls.

Q: How can I solve dump due to insufficient memory (SELECT)?

I am using a SELECT query on a database table. Since the number of records in the table is very large,
the program dumps due to insufficient memory. How can I solve this?

In this case you could use the PACKAGE SIZE addition in the SELECT query to process in limited amount of data,
thus avoiding the memory overloads.
Ex: 
SELECT *
FROM 
INTO TABLE itab
PACKAGE SIZE .
IF sy-subrc EQ 0.
\*" Process the n records
ENDIF.
ENDSELECT.

Q: How can I convert numerals into the corresponding text?

Use the Function Module SPELL_AMOUNT to convert the integer into text.
Ex: 
DATA v_int TYPE i VALUE '1000'.
DATA words LIKE SPELL.
CALL FUNCTION 'SPELL_AMOUNT'
EXPORTING
AMOUNT = v_int
LANGUAGE = SY-LANGU
IMPORTING
IN_WORDS = words.
WRITE words-word.

Q: How to convert a date to internal or external format?
Use the functions modules CONVERT_DATE_TO_EXTERNAL or CONVERT_DATE_TO_INTERNAL
to convert the date. When converting to external format, the date format from the user's user profile will be used.
When converting to internal format, the result will be in YYYYMMDD format.
Q: What are indexes?
 Indexes are described as a copy of a database table reduced to specific fields. This data exists in sorted form. This sorting form ease fast access to the field of the tables. In order that other fields are also read, a pointer to the associated record of the actual table are included in the index. The indexes are activated along with the table and are created automatically with it in the database.

INDEXES are helpful for accessing data from the database tables.
PRIMARY INDEX: This is automatically generated the movement we create a database table.It is only for the key fields in the table.
SECONDARY INDEX : This is given in addition to the Primary Index already existing for the table ,so as to improve the data retrieval consistently, so as a result the performance is also increased.Secondary Index is also helpful in reducing the load on the Database.
Q: Difference between XVBAP and YVBAP in MV45AFZZ Include
YVBAP - Contains the line item data from Database VBAP
XVBAP - Contains the Current changes on the Line Items

Q:ABAP - how to read locked infotype records in HR ?

In HR master data maintenance, you can lock individual infotype datarecords. These data records are normally ignored during evaluationsusing PNPCE. If you set the PNP-SW-IGNORELOCKEDRECORDS switch to N (atthe INITIALIZATION or START-OF-SELECTION events), the report can howeverinstruct PNPCE to read locked data records too. Authorization Check: PNP_SW_SKIP_PERNR, PNP_GET_AUTH_SKIPPED_PERNRS.

INITIALIZATION.

pnp-sw-ignorelockedrecords = 'N'.

Q: How to test a RFC connection in Production?

In Production environment generally consultants do not have access to SM59. In that case use program RSRFCPIN to test the RFC destination. Most consultants must have SA38 authorization. 

Q: Enhancement section & Enhancement point 

Enhancement Point -
If you have written code using enhancement point your custom code will be executed along with the standard code.

Enhancement Section -

If you have written code using enhancement section, only your custom code will be executed replacing standard code. standard code will not be executed.

Q: What is 'Log data Changes' in Z-Table creation?


Table log history in SCU3 if the Table is logged for changes..> Technical settings-->Log data changes.

Q: Prerequisites for "For All Entries" ?

1.Duplicates are automatically removed from the resulting data set. Hence care should be taken that the unique key of the detail line items should be given in the select statement.

2.If the table on which the For All Entries IN clause is based is empty, all rows are selected into the destination table. Hence it is advisable to check before-hand that the first table is not empty.

3.If the table on which the For All Entries IN clause is based is very large, the performance will go down instead of improving. Hence attempt should be made to keep the table size to a moderate level.

Q: What are INDEX in a Table?

INDEXES are helpful for accessing data from the database tables.

PRIMARY INDEX: This is automatically generated the movement we create a database table.It is only for the key fields in the table.

SECONDARY INDEX : This is given in addition to the Primary Index already existing for the table ,so as to improve the data retrieval consistently, so as a result the performance is also increased.Secondary Index is also helpful in reducing the load on the Database.


Q: Advantages of Domains and Data Elements :
a)      Re-usability

b)      Foreign Key Relationships Can be maintained Because the Foreign Key Fields and Check table fields should have the Same Domain.

Q: Which is the better - JOINS or SELECT... FOR ALL ENTRIES...?

In most scenarios INNER JOIN performs better than FOR ALL ENTRIES, and should be used first. Only if there are performance issues should FOR ALL ENTRIES be considered, and careful measurements taken before and after to validate whether there really are performance gains. 
The effect of FOR ALL ENTRIES needs to be observed first by running a test program and analyzing SQL trace. Certain options set by BASIS can cause FOR ALL ENTRIES to execute as an 'OR' condition. This means if the table being used FOR ALL ENTRIES has 3 records, SQL Trace will show 3 SQL's getting executed. In such a case using FOR ALL ENTRIES is useless. However of the SQL Trace shows 1 SQL statement it's beneficial since in this case FOR ALL ENTRIES is actually getting executed as an IN List.
JOINS are recommended over FOR ALL ENTRIES. There is no real limit to the number of tables that can be joined; however greater complexity can make maintenance harder, and if there are problems with the join, make it harder to resolve them. If the JOIN is being made on fields which are key fields in both the tables, it reduced program overhead and increases performance. 

In some scenarios, you are presented with an internal table. In these situations, you may have no choice but to use FOR ALL ENTRIES.



Q: FAQ on OOP ABAP
http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=281773094

How to change Transport request from Released to Modifiable

Step 1: Go to SE38 – Execute Program RDDIT076.  Step 2: Give your released requests number and execute again. Step 3: After executing, yo...