cancel
Showing results for 
Search instead for 
Did you mean: 

How to create udf on system form using DI API

former_member557264
Discoverer
0 Kudos

Hi Guys,

I wanna ask about how to create User defined feilds on system form using c# and how to make the add button add all fields at once.

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member416544
Participant
0 Kudos

Hi Hassan,

Create User defined feilds on system form using c#.

public void InitDeclareUdfs()
        {
CreateUserDefinedField("MyUDFName", "MyUDFDescription", BoFieldTypes.db_Alpha, 100, "OPMG", null, null);
        }

private void CreateUserDefinedField(string Name, string Descreption, 
BoFieldTypes dataType, int size, string tableName, Dictionary<string,
 string> dictionary, string defaultValue = "")
  {
  GC.Collect();
  var SboCompany = ServiceLocator.SboCompany;
  var recordset = (Recordset)SboCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
  var userField = (UserFieldsMD)SboCompany.GetBusinessObject(BoObjectTypes.oUserFields);
  recordset.DoQuery("SELECT FieldId FROM cufd where AliasId ='" + Name + "' and tableid = '" + tableName + "'");
  int Count = recordset.RecordCount;
  int ufId = 0;
  if (Count != 0)
  {
  ufId = Convert.ToInt32(recordset.Fields.Item(0).Value);
  }
  Marshal.ReleaseComObject(recordset);

  if (Count == 0)
  {
  userField.TableName = tableName;
  userField.Name = Name;
  userField.Description = Descreption;
  userField.Type = dataType;

  var vv = userField.ValidValues;
  if (dictionary != null)
  {
  var valids = new List<string>();
  for (int i = 0; i < vv.Count; i++)
  {
  vv.SetCurrentLine(i);
  valids.Add(vv.Value);
  }
  foreach (var pair in dictionary)
  {
  if (valids.Contains(pair.Key))
  {
  continue;
  }
  userField.ValidValues.Value = pair.Key;
  userField.ValidValues.Description = pair.Value;
  userField.ValidValues.Add();
  }
  }

  userField.DefaultValue = defaultValue;
  if (dataType != BoFieldTypes.db_Numeric)
  userField.Size = size;

  Marshal.ReleaseComObject(recordset);

  if (userField.Add() != 0)
  {
  //int transTemp4 = System.Convert.ToInt32(lErrCode);
  //SboCompany.GetLastError(out transTemp4, out sErrMsg);
  //Interaction.MsgBox(sErrMsg, (Microsoft.VisualBasic.MsgBoxStyle)(0), null);

  //Application.SBO_Application.SetStatusBarMessage("Error while trying 
to add the field [" + Name + " - " + tableName + "] - [" + 
Company.GetLastErrorDescription() + "]", BoMessageTime.bmt_Short, true);
  }
  else
  {

  //Application.SBO_Application.SetStatusBarMessage("User-defined field 
[" + Name + " - " + tableName + "] has been created successfully", 
BoMessageTime.bmt_Short, false);
  }

  Marshal.ReleaseComObject(userField);
  }
  }

Regards,

Chenna.

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Hassan,

Refer to the UserFieldsMD Object under SDK Help Center and you will be able to find the sample. Below is the sample which is adding UDF to "OCRD" Table (Business Partners Master Data):

Hope it helps!

Kind regards,

ANKIT CHAUHAN

SAP SME Support