Skip to Content

Prerequisites

You should have read the Weblogs “From Function Module to JCo Application (JCo Client)”: From Function Module to JCo Application – Part 1 of 3, From Function Module to JCo Application – Part 2 of 3 and From Function Module to JCo Application – Part 3 of 3 to know about the SAP Java Connector (JCo) working as a client.

Then download the Ostermiler Java Utilities from http://ostermiller.org/utils/ and install them in your CLASSPATH. I’m using the Ostermiler Java Utilities to parse the CSV and to create the initial password which is needed to create the User via BAPI_USER_CREATE1.</p>

Set alert defaults

I create the users for a CRM system. We defined some Alerts which are created when changes in an Opportunity where made. By default this alerts are sent via E-Mail. This function module will avoid this:


function z_salert_set_default.

*”—-


“Lokale Schnittstelle:

*”  IMPORTING

*”     VALUE(USERNAME) TYPE  BAPIBNAME-BAPIBNAME

*”  TABLES

*”      RETURN STRUCTURE  BAPIRET2 OPTIONAL

*”—-


  data: gs_personal     type   salrtdlvry,

    wa_return like line of return,

    wa_userexist_return like line of return.

*

  • Approve that the User exist

*

  call function ‘BAPI_USER_EXISTENCE_CHECK’

    exporting

      username = username

    importing

      return   = wa_userexist_return.

*

  • BAPI_USER_EXISTENCE_CHECK returns always TYPE = ‘I’

  • because of that we had to check the message number

*

  if wa_userexist_return-number = ‘088’.

*

  • Write username into gs_personal

*

    gs_personal-uname = username.

*   Do database update

    modify salrtdlvry from gs_personal.

    commit work and wait.

*

  • Check return and fill return table

*

    if sy-subrc = 0.

      wa_return-type = ‘S’.

    else.

      wa_return-type = ‘E’.

    endif.

  else.

    wa_return = wa_userexist_return.

    wa_return-type = ‘E’.

  endif.

  append wa_return to return.

endfunction.

</pre>

Set password inactive

We are using a SSO Environment with the SAP Enterprise Portal. Unfortunately the Function Module for User creation doesn’t support to do this setting when the user is created. So I’ve recorded a batch input with Transaction SHDB and created this function module automatically:


function z_bcsec_set_password_inactive.

*”—-


“Lokale Schnittstelle:

*”  IMPORTING

*”     VALUE(CTU) LIKE  APQI-PUTACTIVE DEFAULT ‘X’

*”     VALUE(MODE) LIKE  APQI-PUTACTIVE DEFAULT ‘N’

*”     VALUE(UPDATE) LIKE  APQI-PUTACTIVE DEFAULT ‘L’

*”     VALUE(GROUP) LIKE  APQI-GROUPID OPTIONAL

*”     VALUE(USER) LIKE  APQI-USERID OPTIONAL

*”     VALUE(KEEP) LIKE  APQI-QERASE OPTIONAL

*”     VALUE(HOLDDATE) LIKE  APQI-STARTDATE OPTIONAL

*”     VALUE(NODATA) LIKE  APQI-PUTACTIVE DEFAULT ‘/’

*”     VALUE(USERNAME) LIKE  BDCDATA-FVAL

*”  EXPORTING

*”     VALUE(SUBRC) LIKE  SYST-SUBRC

*”  TABLES

*”      MESSTAB STRUCTURE  BDCMSGCOLL OPTIONAL

*”—-


subrc = 0.

perform bdc_nodata      using nodata.

perform open_group      using group user keep holddate ctu.

perform bdc_dynpro      using ‘SAPLSUU5’ ‘0050’.

perform bdc_field       using ‘BDC_CURSOR’

                              ‘USR02-BNAME’.

perform bdc_field       using ‘BDC_OKCODE’

                              ‘=PASS’.

perform bdc_field       using ‘USR02-BNAME’

                              username.

perform bdc_dynpro      using ‘SAPLSUU5’ ‘0400’.

perform bdc_field       using ‘BDC_CURSOR’

                              ‘G_PASSWORD1’.

perform bdc_field       using ‘BDC_OKCODE’

                              ‘=DEL’.

perform bdc_dynpro      using ‘SAPLSUU5’ ‘0400’.

perform bdc_field       using ‘BDC_CURSOR’

                              ‘G_PASSWORD1’.

perform bdc_field       using ‘BDC_OKCODE’

                              ‘=PASS’.

perform bdc_transaction tables messtab

using                         ‘SU01’

                              ctu

                              mode

                              update.

if sy-subrc <> 0.
  subrc = sy-subrc.
  exit.
endif.

perform close_group using     ctu.

endfunction.
include bdcrecxy.

Now let’s put this all together into a JCo application witch creates our Users in the ABAP System.

logon.properties

To make it easier for you to adopt your login information I’ve set up an external logon.properties file. This is the content:


jco.client.client=002
jco.client.user=developer
jco.client.passwd=XXXXXXXX
jco.client.ashost=gateway
jco.client.sysnr=00
jco.client.lang=EN
jco.client.abap_debug=0
jco.client.use_sapgui=0

OrderedProperties.java

The logon.properties file is read by the OrderedProperties class which comes with the JCo examples:


import java.util.*;
import java.io.*;
public class OrderedProperties extends java.util.Properties {
  ArrayList orderedKeys = new ArrayList();
  public OrderedProperties() {
    super();
  }
  public OrderedProperties(java.util.Properties defaults) {
    super(defaults);
  }
  public synchronized Iterator getKeysIterator() {
    return orderedKeys.iterator();
  }
  public static OrderedProperties load(String name)
                                  throws IOException {
    OrderedProperties props = null;
    java.io.InputStream is =
      OrderedProperties.class.getResourceAsStream(name);
    if ( is != null ) {
      props = new OrderedProperties();
      props.load(is);
      return props;
    } else {
      if ( ! name.startsWith("/") ) {
        return load("/" + name);
      } else {
        throw new IOException("Properties could not be loaded.");
      }
    }
  }
  public synchronized Object put(Object key, Object value) {
    Object obj = super.put(key, value);
    orderedKeys.add(key);
    return obj;
  }
  public synchronized Object remove(Object key) {
    Object obj = super.remove(key);
    orderedKeys.remove(key);
    return obj;
  }
}

SAPCreateUser.java


/**

 

  • SAPCreateUser.java

 

  • (c) Copyright SITECO Beleuchtungstechnik GmbH, Traunreut, 2005.

 

  • All rights reserved.

 */

import com.sap.mw.jco.*;

import com.Ostermiller.util.*;

import java.io.*;

import java.util.*;

/**

 

  • @version 1.0

 

  • @author  SITECO Beleuchtungstechnik GmbH, Traunreut

 */

public class SAPCreateUser {

  // Name of the ClientPool

  static final String POOL_NAME = “Pool”;

  JCO.Client mConnection;

  // The repository we will be using

  IRepository repository;

  // Parameters

  private JCO.ParameterList input;

  private JCO.ParameterList tables;

  private String userExists;

  private String userExistNot;

  private String password;

   

  public SAPCreateUser()

  {

    try {

      JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);

      if (pool == null) {

        OrderedProperties logonProperties =

          OrderedProperties.load(“/logon.properties”);

        JCO.addClientPool(POOL_NAME,  // pool name

                          5,          // maximum number of connections

                          logonProperties);  // properties

      }

      mConnection = JCO.getClient(POOL_NAME);

      // Create a new repository

      repository = JCO.createRepository(“MYRepository”, POOL_NAME);

    }

    catch (Exception ex) {

      ex.printStackTrace();

    }

  }

 

  protected void cleanUp() {

    JCO.removeClientPool(POOL_NAME);

  }

   

  // Retrieves and sales order list

  public void SAPCreateUser() {

    try {

      // Get a function template from the repository

      IFunctionTemplate ftemplate =

        repository.getFunctionTemplate(“BAPI_USER_EXISTENCE_CHECK”);

      IFunctionTemplate ftemplate2 =

        repository.getFunctionTemplate(“BAPI_USER_CREATE1”);

      IFunctionTemplate ftemplate3 =

        repository.getFunctionTemplate(“Z_SALERT_SET_DEFAULT”);

      IFunctionTemplate ftemplate4 =

        repository.getFunctionTemplate(“Z_BCSEC_SET_PASSWORD_INACTIVE”);

      // Read CSV

      InputStream is =

        SAPCreateUser.class.getResourceAsStream(“create-user-crm-server.csv”);

      // Parse CSV to Array

      CSVParser csv = new CSVParser(is);

      csv.changeDelimiter(‘;’);

      String[][] values = csv.getAllValues();

      // Loop at array

      for (int i=0; i<values.length; i++){

        // Create a function from the template

        JCO.Function function = new JCO.Function(ftemplate);

        // Fill in input parameters

        JCO.ParameterList input = function.getImportParameterList();

        input.setValue(values[i][15], “USERNAME”);

        System.out.println(values[i][15]);

        // Get a client from the pool

        JCO.Client client = JCO.getClient(POOL_NAME);           

        // Call the remote system

        client.execute(function);

        // Print return message

        JCO.Structure ret =

          function.getExportParameterList().getStructure(“RETURN”);

        System.out.println(“BAPI_USER_EXISTENCE_CHECK RETURN-TYPE: “

          + ret.getString(“TYPE”));

        System.out.println(“BAPI_USER_EXISTENCE_CHECK RETURN-MESSAGE: “

          + ret.getString(“MESSAGE”));

        //

        // The Return Type is always I so we hadto approve the Message Number

        //

        userExistNot = “124”;

        userExists = “088”;

        if ( userExistNot.equals(ret.getString(“NUMBER”))) {

          // Create a function from the template

          JCO.Function function2 = new JCO.Function(ftemplate2);

          // Fill in input parameters

          JCO.ParameterList input2 = function2.getImportParameterList();

          JCO.Structure ADDRESS = input2.getStructure(“ADDRESS”);

          ADDRESS.setValue( values[i][2], “PERS_NO”  );

          ADDRESS.setValue( values[i][3], “TITLE_P”  );

          ADDRESS.setValue( values[i][5], “LASTNAME”  );

          ADDRESS.setValue( values[i][6], “FIRSTNAME”  );

          ADDRESS.setValue( values[i][13], “TEL1_NUMBR”  );

          ADDRESS.setValue( values[i][14], “FAX_NUMBER”  );

          ADDRESS.setValue( values[i][15], “DEPARTMENT”  );

          ADDRESS.setValue( values[i][16], “E_MAIL”  );

         

          JCO.Structure COMPANY = input2.getStructure(“COMPANY”);

          COMPANY.setValue( values[i][0], “COMPANY”  );

         

          input2.setValue(values[i][15], “USERNAME”);

         

          JCO.Structure UCLASS = input2.getStructure(“UCLASS”);

          UCLASS.setValue( “AB” , “LIC_TYPE”  );

         

          password = new RandPass().getPass(8);

          System.out.println(password);

          JCO.Structure PASSWORD = input2.getStructure(“PASSWORD”);

          PASSWORD.setValue( password , “BAPIPWD”  );

          // Call the remote system

          client.execute(function2);

          // Get table containing the orders

          JCO.Table ret2 = function2.getTableParameterList().getTable(“RETURN”);

          // Print results

          if (ret2.getNumRows() > 0) {

            // Loop over all rows

            do {

              System.out.println(“—-


“);

              // Loop over all columns in the current row

              for (JCO.FieldIterator e = ret2.fields(); e.hasMoreElements(); ) {

                JCO.Field field = e.nextField();

                System.out.println(field.getName() + “:     ” + field.getString());

              }//for

            } while(ret2.nextRow());

          }

          else {

            System.out.println(“No results found”);

          }//if

          userExists = “true”;

        }

        if (

            userExists.equals(ret.getString(“NUMBER”))

            || userExists.equals(“true”)

          ) {

          // Create a function from the template

          JCO.Function function3 = new JCO.Function(ftemplate3);

          // Fill in input parameters

          JCO.ParameterList input3 = function3.getImportParameterList();

          input3.setValue(values[i][15], “USERNAME”);

          System.out.println(“Reset Alert Values for User: ” + values[i][15]);

          // Call the remote system

          client.execute(function3);

         

          // Print return message

          JCO.Table ret3 = function3.getTableParameterList().getTable(“RETURN”);

          if (ret3.getNumRows() > 0) {

            // Loop over all rows

            do {

              System.out.println(“—-


“);

              // Loop over all columns in the current row

              for (JCO.FieldIterator e = ret3.fields(); e.hasMoreElements(); ) {

                JCO.Field field = e.nextField();

                System.out.println(field.getName() + “:     ” + field.getString());

              }//for

            } while(ret3.nextRow());

          }

          // Create a function from the template

          JCO.Function function4 = new JCO.Function(ftemplate4);

          // Fill in input parameters

          JCO.ParameterList input4 = function4.getImportParameterList();

          input4.setValue(values[i][15], “USERNAME”);

          input4.setValue(“N”, “MODE”);

          System.out.println(“Passwort Inaktiv setzen für: ” + values[i][15]);

          // Call the remote system

          client.execute(function4);

          // Print return message

          JCO.Table ret4 =

            function4.getTableParameterList().getTable(“MESSTAB”);

          if (ret4.getNumRows() > 0) {

            // Loop over all rows

            do {

              System.out.println(“—-


“);

              // Loop over all columns in the current row

              for (JCO.FieldIterator e = ret4.fields(); e.hasMoreElements(); ) {

                JCO.Field field = e.nextField();

                System.out.println(field.getName() + “:     ” + field.getString());

              } //for

            } while(ret4.nextRow());

          }

        }

        // Release the client into the pool

        JCO.releaseClient(client);

        System.out.println(“—-


“);

      }

    }

    catch (Exception ex) {

        System.out.println(“Caught an exception:
” + ex);

    }

  }

  public static void main(String[] argv)

  {

    SAPCreateUser e = new SAPCreateUser();

    e.SAPCreateUser();

    e.cleanUp();

  }

}

</pre>

CSV Input File

The Input for the SAPCreateUser Program is the CSV File “create-user-crm-server.csv”. The layout of my input file was:

Company;;EmployeeNumber;Title;;LastName;FistNane;Department;Street;ZIP-Code;City;ISO-Country;Costcentre;Phone;Fax;Username;E-Mail

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Matthew Easlea
    Quick question,
    is it possible to do the same thing with LSMW?

    But sticking ot the java world – i always wondered why there isn’t an opensource project on sourceforge providing generic uploads from text files using JCo and an XML mapping file from Columns to BAPI params.

    (0) 
  2. Jean Michel Sommer
    Nice job, on ECC6 no problem.
    I don’t plan to use Java but on 4.6C do you know how to setup license field UCLASS (no present at this time in the BAPI used there)
    (0) 
    1. Jat Sha
      Hi,

      By any chanse do you know how to change users license data (UCLASS) in mass.

      I am not able to do this with BAPI_USER_CHANGE.

      Thanks

      (0) 

Leave a Reply