cancel
Showing results for 
Search instead for 
Did you mean: 

Error in creating userkey ---- UserKeysMD

Former Member
0 Kudos

Hi,

I am trying to add a primary key through UserKeysMD in a user defined table.But It is showing the invalid field name error,

Pls help me

Accepted Solutions (1)

Accepted Solutions (1)

rasmuswulff_jensen
Active Contributor
0 Kudos

Try the following... that work for me:


UserKeysMD keyMd = (UserKeysMD)SboCompany.GetBusinessObject(BoObjectTypes.oUserKeys);
keyMd.TableName = "TableName";
keyMd.KeyName = "KeyName";
keyMd.Elements.ColumnAlias = "Field"; 
keyMd.Unique = BoYesNoEnum.tYES;
keyMd.Add();

Message was edited by: Rasmus Jensen

Answers (2)

Answers (2)

Former Member
0 Kudos

Make sure the field's names you're indexing (then Elements.ColumnAlias) are less o equal than 8 characters (it gives me a lot of headaches...)

Former Member
0 Kudos

Thanks JM,

But the field name is only 5 chars long. "CCode"

So it shold'mt be a problem

And the headache continues

Thanks for any Help you can provied.

Pedro Gomes.

AdKerremans
Active Contributor
0 Kudos

Hi Pedro,

Do you have the UserKey.Add line after the Unique line?

Regards

Ad

Former Member
0 Kudos

Hi Ad.

Yes i Do have the UserKey.Add

Thanks again for the help.

I still can't create the unique constraint

Pedro Gomes

Message was edited by: Pedro Gomes

Former Member
0 Kudos

Hi,

I've deleted my UDT an created it again this time in code

after i've added the UDF also in code

the same error ocurres when i try to create the unique constraint

code i used for creating the key.

oUserKeysMD = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserKeys)

oUserKeysMD.TableName = "MGCF"

oUserKeysMD.KeyName = "Key_CCode"

oUserKeysMD.Elements.ColumnAlias = "CCode"

oUserKeysMD.Unique = SAPbobsCOM.BoYesNoEnum.tYES

If oUserKeysMD.Add <> 0 Then

BO_Application.MessageBox(oCompany.GetLastErrorDescription())

End If

if anyone cam spot the bug please let me know

Thanks Pedro Gomes

former_member184566
Active Contributor
0 Kudos

Hi yash sharma

Must amke sure the table and the field exists in that database, then do not put in your code the "@" symbol or the "U_" symbol. Also the field should not have dupliacte data.Once you have added the key look in OUKD, this is where sap stores the MDKeys.

Here is sample code if you do it in vb

Dim UserKey As SAPbobsCOM.UserKeysMD

UserKey = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserKeys)

Try

UserKey.TableName = "SM_OPED"

UserKey.KeyName = "test"

UserKey.Elements.ColumnAlias = "Proc"

UserKey.Unique = SAPbobsCOM.BoYesNoEnum.tYES

lRetCode = UserKey.Add

If lRetCode <> 0 Then

oCompany.GetLastError(lRetCode, sErrMsg)

MsgBox(sErrMsg)

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

The above works and adds one key.

Hope this helps

Former Member
0 Kudos

Hi,

I have a UDT whith a UDF created throw the SAP BO Application, and need to enforce a unique constraint.

I used the sample code provided by Louis but

the UserKet.Add returns error code

-5002 : Invalid field name

UserKey.TableName = "MGCF" 'The UDT Name

UserKey.KeyName = "KeyCCode" 'The Key Name

UserKey.Elements.ColumnAlias = "CCode" 'The Column Name

UserKey.Unique = SAPbobsCOM.BoYesNoEnum.tYES

What Have i done wroung?

Thanks Pedro Gomes.