Format fields

Here is a sample function to format form fields within Floe emails

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
FUNCTION zfloe_format_form_fields.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_FIELD_NAME) TYPE  FIELDNAME OPTIONAL
*"     VALUE(IM_FIELD_TYPE) TYPE  /FLM/SFS_FIELD_TYPE OPTIONAL
*"     VALUE(IM_SEPARATOR) TYPE  FLAG OPTIONAL
*"     VALUE(IM_DATE_FORMAT) TYPE  CHAR1 OPTIONAL
*"  EXPORTING
*"     VALUE(EX_FORMATED_VALUE) TYPE  STRING
*"     VALUE(EX_SUBRC) TYPE  SYSUBRC
*"  CHANGING
*"     VALUE(CH_DATA) TYPE  /FLOE/VARS_T
*"----------------------------------------------------------------------
*
* Version History:
* Version 2 of this function supports the formatting of repeating fields. If you are attempting to format
* a repeating field the formatted value will be available in parameter ch_data, NOT in parameter ex_formated_value
*--------------------------------------------------------------------------------------------------------------*
*
* This function can be called from the FLOE data user-exit when FLOE is being triggered by FLM
* (ie by an FLM posting adaptor, or when FLOE is used to send an FLM  notification email for example)
* When FLOE is triggered by FLM, the FLOE data variable table will contain (among other variables)
* all the FLM form fields. This function can be used to format dropdown, date and time FLM form fields
* so that they can be included in the email in a user-friendly format. Time fields will be returned in
* the HH:MM:SS format, the format of date fields can be specified by import parameter IM_DATE_FORMAT,
* whereas for dropdowns we want to display the dropdown entry human readable description rather than the
* key. Please note however that dropdown descritpions are calculated at form render time and are not saved
* in the system, hence this function attempts to re-run the F4 user-exits to determine the descriptions
* and this may produce different results to those which were injected into the form. Moreover, dropdowns
* with entries determined on the form template rather than by the back end can not be formatted by this
* function. The following parameters are available in this function:
*
* Changing Parameter:
*   ch_data             : FLOE variable data table. All fields which are to be formatted must be included in
*                         this table. Please note that you may directly pass the FLOE data user-exit parameter
*                         ch_variables into this parameter, however this is not recomended as ch_variables
*                         is passed into other FLOE user-exits in which you may need to access the actual data
*                         rather than the formatted value.
*
* Import Parameters:
*   im_field_name      : If this parameter is left blank, the function will attempt to format all date, time
*                        and dropdown fields of the particular form type which are available in the ch_data
*                        table. Alternatively if you only wish to format one field, pass the field name into
*                        this parameter.
*   im_field_type      : It is possible that forms which were created using very old versions of FLM do not
*                        have the field type correctly defined in the data schema. If you are using this function
*                        to format only one field (by passing in im_field_name) and that fields' type is not
*                        defined correctly in the schema, pass in DROP to format it as a dropdown, DATE to format
*                        it as a date field or TIME to format it as a time field.
*   im_separator       : By default, the dropdown entry key will be substituted with the human-readable description
*                        for dropdowns. If instead you wish both the key and description separated by a separator
*                        pass in your choice of separator.
*   im_date_format     : Specifies the date format - the following formats are available:
*                           1	               DD.MM.YYYY
*                           2	               MM/DD/YYYY
*                           3	               MM-DD-YYYY
*                           4	               YYYY.MM.DD
*                           5	               YYYY/MM/DD
*                           6	               YYYY-MM-DD
*                           7                DD/MM/YYYY
*                           Any other value  5. May 2016
*
* Export Parameters:
*   ex_formated_value  : If you only wish to format one field (by passing in im_field_name) this export parameter
*                        will hold the formatted value for that field so that it can easily be accessed without
*                        having to do a read of ch_data. If you are using the function to format all applicable
*                        fields in ch_data, ex_formatted_value will be left blank. If you are formatting a repeating
*                        field, the formatted value must be read form parameter ch_data as paarameter ex_formated_value
*                        will hold the value of the field on the last row instance
*   ex_subrc           : Sub-return code which indicates potential errors
*--------------------------------------------------------------------------------------------------------------*

  TYPES: BEGIN OF ltyp_fieldname,
           field_name TYPE fieldname,
           field_type TYPE /flm/sfs_field_type,
         END OF ltyp_fieldname.
*
  DATA: ls_floe_data    TYPE /floe/vars_s,
        lv_ccode        TYPE /flm/cust_code,
        lv_ftype        TYPE /flm/ftype_code,
        lv_flang        TYPE /flm/flang,
        lv_fver         TYPE /flm/fver,
        lv_fid          TYPE /flm/fid,
        lv_fid_var      TYPE /flm/id_var,
        ls_fpe          TYPE /flm/fpe,
        lv_cms_doc      TYPE /flm/cms_doc,
        lv_subrc        TYPE sysubrc,
        lv_xdata        TYPE xstring,
        lt_form_data    TYPE /flm/xml_tab_t,
        lt_fieldname    TYPE TABLE OF ltyp_fieldname,
        ls_fieldname    TYPE ltyp_fieldname,
        lv_fieldname    TYPE string,
        lv_tabix        TYPE sytabix,
        lv_format_value TYPE string,
        lt_f4_data      TYPE /flm/sfs_form_data_t,
        ls_f4_data      TYPE /flm/form_data,
        lv_length       TYPE i,
        lv_day(2),
        lv_month(2),
        lv_year(4).
*
* First get the FPE record
*
  READ TABLE ch_data INTO ls_floe_data WITH KEY var_code = /flm/core=>c_return_node.
  IF sy-subrc IS NOT INITIAL.           "Try lower-case FLM_RETURN if nothing found
    READ TABLE ch_data INTO ls_floe_data WITH KEY var_code = /flm/core=>c_return_node.
    IF sy-subrc IS NOT INITIAL.
      ex_subrc = 1.                     "Unable to read FLM_RETURN, so bail out
      RETURN.
    ENDIF.
  ENDIF.
*
  CALL METHOD /flm/user=>split_flm_return
    EXPORTING
      im_flm_return = ls_floe_data-value
    IMPORTING
      ex_ccode      = lv_ccode
      ex_ftype      = lv_ftype
      ex_fver       = lv_fver
      ex_toption    = lv_flang
      ex_fid        = lv_fid
      ex_fid_var    = lv_fid_var
      ex_cms_doc    = lv_cms_doc.
*
  SELECT SINGLE * FROM /flm/fpe INTO ls_fpe
    WHERE ccode  EQ lv_ccode
     AND  ftype  EQ lv_ftype
     AND  flang  EQ lv_flang
     AND  fver   EQ lv_fver
     AND  id     EQ lv_fid
     AND  id_var EQ lv_fid_var.

* Bail out if unable to read the FPE record
  IF sy-subrc IS NOT INITIAL.
    ex_subrc = 2.
    RETURN.
  ENDIF.
*
* Next, get the form data
*
  CALL METHOD /flm/hds=>get_schema_from_cms
    EXPORTING
      im_cms_doc      = lv_cms_doc
    IMPORTING
      ex_data_xstring = lv_xdata
      ex_subrc        = lv_subrc.

* Bail out if unable to read the data
  IF lv_subrc IS NOT INITIAL.
    ex_subrc = 3.
    RETURN.
  ENDIF.
*
  CALL METHOD /flm/core=>get_cms_doc_from_xsd
    EXPORTING
      im_xstring    = lv_xdata
    IMPORTING
      ex_data_nodes = lt_form_data.
*
* Determine which fields we want to format - if a field has been passed in, just format that, otherwise
* format all drop-downs, data and time fields
*
  IF im_field_name IS NOT INITIAL.
*
    SELECT SINGLE field_name field_type FROM /flm/fdd_fld INTO CORRESPONDING FIELDS OF ls_fieldname
      WHERE ccode      EQ ls_fpe-ccode
       AND  ftype      EQ ls_fpe-ftype
       AND  flang      EQ ls_fpe-flang
       AND  fver       EQ ls_fpe-fver
       AND  field_name EQ im_field_name.
*
* There may be scenarios (specially for very old forms), where the field type hasn't been correctly defined
* in the schema, but you still wish to format it - to get around such issues, users may pass in the type they
* want their single field to be formatted as
*
    IF im_field_type IS NOT INITIAL.
      ls_fieldname-field_type = im_field_type.
    ENDIF.
*
    ls_fieldname-field_name = im_field_name.
    APPEND ls_fieldname TO lt_fieldname.
*
  ELSE.
*
    SELECT field_name field_type FROM /flm/fdd_fld INTO CORRESPONDING FIELDS OF TABLE lt_fieldname
      WHERE ( field_type EQ 'DROP' OR field_type EQ 'DATE' OR field_type EQ 'TIME' )
       AND  ccode      EQ ls_fpe-ccode
       AND  ftype      EQ ls_fpe-ftype
       AND  flang      EQ ls_fpe-flang
       AND  fver       EQ ls_fpe-fver.
*
  ENDIF.
*
* Now we've got the list of fields to format, loop around them and format
*
  LOOP AT lt_fieldname INTO ls_fieldname.
*
* We're not formatting based on the FLM data but on the FLOE data, so if a field which exists in the schema
* doesn't exist in the FLOE data table, ignore it
*
    CLEAR: ls_floe_data, lv_format_value.
*    READ TABLE ch_data INTO ls_floe_data WITH KEY var_code = ls_fieldname-field_name. "Version 2--
    LOOP AT ch_data INTO ls_floe_data WHERE var_code EQ ls_fieldname-field_name.       "Version 2++
*
      lv_tabix = sy-tabix.
      CLEAR lv_format_value.                                "Version 2++
*    IF ls_floe_data IS INITIAL.                            "Version 2--
*      CONTINUE.
*    ENDIF.
*
      CASE ls_fieldname-field_type.
*
        WHEN 'DROP'.
*
          lv_fieldname = ls_fieldname-field_name.
*
          CALL METHOD /flm/hds=>get_f4_entries_for_field
            EXPORTING
              im_ccode      = ls_fpe-ccode
              im_field_name = lv_fieldname
              im_fstatus    = ls_fpe-fstatus
              im_flang      = ls_fpe-flang
              im_user       = sy-uname
              im_fver       = ls_fpe-fver
              im_ftype      = ls_fpe-ftype
              im_doc        = ls_fpe-document
              im_form_data  = lt_form_data
              im_prev_page  = ''
              im_excel      = 'X'
            IMPORTING
              ex_f4_data    = lt_f4_data.
*
          READ TABLE lt_f4_data INTO ls_f4_data WITH KEY name = ls_floe_data-value.
          IF sy-subrc IS INITIAL.
            IF im_separator IS INITIAL.
              lv_format_value = ls_f4_data-value.
            ELSE.
              CONCATENATE: ls_floe_data-value ls_f4_data-value INTO lv_format_value SEPARATED BY im_separator.
            ENDIF.
          ENDIF.
*
        WHEN 'DATE'.
*
          lv_length = STRLEN( ls_floe_data-value ).
          IF lv_length EQ 8.
*
            lv_day = ls_floe_data-value+6(2).
            lv_month = ls_floe_data-value+4(2).
            lv_year = ls_floe_data-value+0(4).
*
            CASE im_date_format.
              WHEN '1'.
                CONCATENATE: lv_day lv_month lv_year INTO lv_format_value SEPARATED BY '.'.
              WHEN '2'.
                CONCATENATE: lv_month lv_day lv_year INTO lv_format_value SEPARATED BY '/'.
              WHEN '3'.
                CONCATENATE: lv_month lv_day lv_year INTO lv_format_value SEPARATED BY '-'.
              WHEN '4'.
                CONCATENATE: lv_year lv_month lv_day INTO lv_format_value SEPARATED BY '.'.
              WHEN '5'.
                CONCATENATE: lv_year lv_month lv_day INTO lv_format_value SEPARATED BY '/'.
              WHEN '6'.
                CONCATENATE: lv_year lv_month lv_day INTO lv_format_value SEPARATED BY '-'.
              WHEN '7'.
                CONCATENATE: lv_day lv_month lv_year INTO lv_format_value SEPARATED BY '/'.
              WHEN OTHERS.
*
                CALL FUNCTION 'CONVERSION_EXIT_LDATE_OUTPUT'
                  EXPORTING
                    input  = ls_floe_data-value
                  IMPORTING
                    output = lv_format_value.
*
            ENDCASE.
*
          ENDIF.
*
        WHEN 'TIME'.
*
          lv_length = STRLEN( ls_floe_data-value ).
          IF lv_length EQ 6.
            CONCATENATE: ls_floe_data-value+0(2) ls_floe_data-value+2(2) ls_floe_data-value+4(2) INTO lv_format_value
               SEPARATED BY ':'.
          ENDIF.
*
      ENDCASE.
*
      IF lv_format_value IS NOT INITIAL. "If we've formatted a value inside the loop, modify the data changing parameter
*
        ls_floe_data-value = lv_format_value.
        MODIFY ch_data INDEX lv_tabix FROM ls_floe_data.
*
        IF im_field_name IS NOT INITIAL. "If we're only doing one field, also fill the corresponding export parameter "Version 2+-
          ex_formated_value = lv_format_value.
        ENDIF.
*
      ENDIF.

    ENDLOOP.                                                "Version 2++
*
  ENDLOOP.
*
ENDFUNCTION.