cancel
Showing results for 
Search instead for 
Did you mean: 

Changing the default value of a user defined field

Former Member
0 Kudos

Hey,

I have a small question.

I would like to update an UDF by code. I looked into it already but i can't manage to update it.

How do i get the FieldID of a UDF? I do it by walking through a DataTable but it's very slow. Is there any other way?

Can someone show me a little code snippet?

Gr, Jeroen Nijhuis

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi David,

Can other installed and active addons cause the problem?

Nussi
Active Contributor
0 Kudos

Hi,

maybe - iam not 100% sure.

if yes, only when the objects like UserFieldsMD, RecordSet (for example SELECT * FROM CUFD), UserTablesMD

are in use.

stop the other addons to test it, but i really think it's more a problem of your current addon.

lg David

Former Member
0 Kudos

Okay, David.

Well I really checked my code multiple times and I cant see any problem.

Are you willing to inspect my code? I am clueless atm..

Nussi
Active Contributor
0 Kudos

of course iam willing

how you wanna do it ?

do you have ICQ ? my number is in my Business Card when you click my name ...

lg David

Answers (9)

Answers (9)

Former Member
0 Kudos

I justed wanted to thank david for helping me out personally! Great thanks!

Former Member
0 Kudos

I think I should email you the source of my add-on..

If you're not willing to place your emailaddress at the forums, you can contact me on my emailaddress at my business card.

I really appreciate your help.

Former Member
0 Kudos

Okay, I really checked my code twice and very accurate,

and i can't find any other bobsCOM object that is not nulled or not being collected. The weird thing is also that I do not use the same com object anywhere else in the code... It's the first time im using it..

Nussi
Active Contributor
0 Kudos

Jeroen,

something in a another procedure locks the adding process - that's for sure.

no recordset and/or another object is allowed to "access" the udf tables at the moment when you add fields/tables.

this rules is for every function, not only the function you add the code !

and the object destroying from the forum friends above is correct

lg David

Former Member
0 Kudos

I checked the full code and I cannot find any other object that doesnt get nulled...

Former Member
0 Kudos

There could be some object which u might have not noticed.

Vasu Natari.

Former Member
0 Kudos

Hmm, ok guys, please help me out.. I still get the error, while i am clearing all other com objects.. please have a look at the full method of code..


if(pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED &&
                    pVal.ItemUID == "1" &&
                    !pVal.BeforeAction &&
                    pVal.FormUID == "e_Setup")
                {
                    SAPbobsCOM.Recordset rs = null;
                    SAPbobsCOM.UserFieldsMD umd = null;

                    try
                    {
                        SAPbouiCOM.Form frm = (SAPbouiCOM.Form)SBO_Application.Forms.Item(FormUID);
                        SAPbouiCOM.DataTable dt = (SAPbouiCOM.DataTable)frm.DataSources.DataTables.Item("SetupValues");
                        SAPbouiCOM.ComboBox cmb_lPrint = (SAPbouiCOM.ComboBox)frm.Items.Item("cmb_lPrint").Specific;
                        SAPbouiCOM.ComboBox cmb_aLev = (SAPbouiCOM.ComboBox)frm.Items.Item("cmb_aLev").Specific;
                        SAPbouiCOM.ComboBox cmb_aRMA = (SAPbouiCOM.ComboBox)frm.Items.Item("cmb_aRMA").Specific;
                        SAPbouiCOM.ComboBox cmb_aGov = (SAPbouiCOM.ComboBox)frm.Items.Item("cmb_aGov").Specific;
                        SAPbouiCOM.ComboBox cmb_sMail = (SAPbouiCOM.ComboBox)frm.Items.Item("cmb_sMail").Specific;
                        SAPbouiCOM.EditText internMail = (SAPbouiCOM.EditText)frm.Items.Item("edt_mAdres").Specific;

                        if (cmb_sMail.Selected.Value == "0" | cmb_sMail.Selected.Value == "3" && internMail.Value == "")
                        {
                            SBO_Application.MessageBox("U dient een emailadres op te geven", 0, "Ok", "", "");
                            frm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE;
                        }
                        else
                        {
                            dt.Columns.Item("U_PrintLbl").Cells.Item(0).Value = cmb_lPrint.Selected.Value;
                            dt.Columns.Item("U_Leverin").Cells.Item(0).Value = cmb_aLev.Selected.Value;
                            dt.Columns.Item("U_RmaReto").Cells.Item(0).Value = cmb_aRMA.Selected.Value;
                            dt.Columns.Item("U_GoedOntv").Cells.Item(0).Value = cmb_aGov.Selected.Value;
                            dt.Columns.Item("U_MailNoti").Cells.Item(0).Value = cmb_sMail.Selected.Value;

                            rs = (SAPbobsCOM.Recordset)company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                            rs.DoQuery("SELECT * FROM [@E_SETUP]");
                            
                            if (rs.RecordCount > 0)
                            {
                                StringBuilder setupQuery = new StringBuilder();

                                setupQuery.Append("UPDATE [@E_SETUP] SET U_PrintLbl = ");
                                setupQuery.Append(cmb_lPrint.Selected.Value);
                                setupQuery.Append(", U_Leverin = ");
                                setupQuery.Append(cmb_aLev.Selected.Value);
                                setupQuery.Append(", U_RmaReto = ");
                                setupQuery.Append(cmb_aRMA.Selected.Value);
                                setupQuery.Append(", U_GoedOntv = ");
                                setupQuery.Append(cmb_aGov.Selected.Value);
                                setupQuery.Append(", U_MailNoti = ");
                                setupQuery.Append(cmb_sMail.Selected.Value);
                                setupQuery.Append(", U_InternM = '");
                                setupQuery.Append(internMail.Value);
                                setupQuery.Append("' WHERE Name = 'Setup'");

                                rs.DoQuery(setupQuery.ToString());
                            }
                            else
                            {
                                StringBuilder setupQuery = new StringBuilder();

                                setupQuery.Append("INSERT INTO [@E_SETUP] values ('0', 'Setup','");
                                setupQuery.Append(cmb_lPrint.Selected.Value);
                                setupQuery.Append("','");
                                setupQuery.Append(cmb_aLev.Selected.Value);
                                setupQuery.Append("','");
                                setupQuery.Append(cmb_aRMA.Selected.Value);
                                setupQuery.Append("','");
                                setupQuery.Append(cmb_aGov.Selected.Value);
                                setupQuery.Append("','");
                                setupQuery.Append(cmb_sMail.Selected.Value);
                                setupQuery.Append("','");
                                setupQuery.Append(internMail.Value);
                                setupQuery.Append("')");                                

                                rs.DoQuery(setupQuery.ToString());    
                            }                            

                            rs.DoQuery("SELECT FieldID FROM CUFD Where AliasID = 'Print' AND TableID = 'ODLN'");

                            int errCode = 0;
                            string errMsg = "";

                            if (rs.RecordCount == 1)
                            {
                                umd = (SAPbobsCOM.UserFieldsMD)company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);

                                if (umd.GetByKey("ODLN", Convert.ToInt32(rs.Fields.Item("FieldID").Value.ToString())))
                                {
                                    umd.DefaultValue = cmb_lPrint.Selected.Value;
                                    if (umd.Update() != 0)
                                    {
                                        company.GetLastError(out errCode, out errMsg);
                                        Logger.Write("An error occured while updating Userfields\n" + errMsg, Category.General, Priority.Normal, 0, System.Diagnostics.TraceEventType.Error);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Write("An error occured while updating E-Loop preferences\n " + ex, Category.General, Priority.Normal, 0, System.Diagnostics.TraceEventType.Error);
                        SBO_Application.MessageBox("Fout: De gegevens zijn niet opgeslagen", 0, "Ok", "", "");
                    }
                    finally
                    {
                        umd = null;
                        rs = null;
                        GC.Collect();
                    }
                }

Former Member
0 Kudos

R u doing the same with all the other COM objects like ur company objects and others.. Plz check all the objects Used... as the problem may lie with any object.

Hope it Helps,

Vasu Natari.

Former Member
0 Kudos

It still gives me the same error after applying your code..

Former Member
0 Kudos

Hi,

Just follow what David has said, and in addition if ur trying to update a UDF in a UDT then u can use the following code snippet..

oUt.UserFields.Fields.Item("U_ud").Value = "XXX"

where oUT is a user table object declared as

Dim oUT As SAPbobsCOM.UserTable

Hope it helps,

Vasu Natari.

Former Member
0 Kudos

Okay, I know how to update a userfield for a document or object..

But what I mean is that I want to edit the UDF itself, like in:

Tools -> Customization Tools -> User-Defined Fields - Management -> "UDF" -> Update

And then edit the default value, only codewise...

Nussi
Active Contributor
0 Kudos

Jeron,

i got it and updated my last message:

use a RecordSet for this that selects from the CUFD table

oRecordSet.DoQuery("select fieldid from CUFD where aliasid='FieldNameWithoutU' and tableid='@table'")

than use the UserFieldsMD object

lg David

Former Member
0 Kudos

Okay, I tried your suggestion, and I get to the point to update the userfield, but it gives me the error:

"Ref count for this object is higher then 0"

How do I solve that?

My code:


rs.DoQuery("SELECT FieldID FROM CUFD Where AliasID = 'Print' AND TableID = 'ODLN'");

int errCode = 0;
string errMsg = "";

if (rs.RecordCount == 1)
{
    umd = (SAPbobsCOM.UserFieldsMD)company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);

    if (umd.GetByKey("ODLN", Convert.ToInt32(rs.Fields.Item("FieldID").Value.ToString())))
    {
        umd.DefaultValue = cmb_lPrint.Selected.Value;
        if (umd.Update() != 0)
        {
            company.GetLastError(out errCode, out errMsg);
            Logger.Write("An error occured while updating Userfields\n" + errMsg, Category.General, Priority.Normal, 0, System.Diagnostics.TraceEventType.Error);
        }
    }
}

Former Member
0 Kudos

Hi,

System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
         rs = Nothing
         GC.WaitForPendingFinalizers()
         GC.Collect()

try the above code and before using the Record set object as there is a problem with the object ur getting the error.

After use of ur DI objects call Garbage collector and dump the objects.

Hope it helps,

Vasu Natari.

Former Member
0 Kudos

Hi,

Ref count for this object is higher then 0 -- this is related to number of live COM objects.

If there is more than one component alive at same point of time, you will get this error.

I think you are not releasing the objects properly.

Release COM objects(like userfieldsMD,recordSet etc) using

System.Runtime.InteropServices.Marshal.ReleaseComObject(object)

You can search using ur error message. I answered it couple of times

Regards

Vishnu

Nussi
Active Contributor
0 Kudos

Hi,

ok - i missunderstood - you want to change the Default Value

use a RecordSet for this that selects from the CUFD table

oRecordSet.DoQuery("select fieldid from CUFD where aliasid='FieldNameWithoutU' and tableid='@table'")

lg David

Edited by: David Nussböck on Sep 8, 2008 12:04 PM