Skip to Content

Initialize Entity with default attributes

Hi all,

I'm trying to create an Extension to add automatically two attributes %Name%+" Id" and %Name%+" Name" to every entity I create. However I'm failing at it, and I suspect it is due lack of knowledge and maybe someone can help. The script I am using, following https://archive.sap.com/discussions/thread/3558516, is

dim myattr
set myattr = obj.Attributes.CreateNew
myattr.name = obj.Name+" Id"
myattr.code = obj.Code+"_ID"
myattr.comment = Now()
myattr.datatype = "Integer"
dim myattr2   
set myattr2 = obj.Attributes.CreateNew
myattr2.name = obj.Name+" Name"
myattr2.code = obj.Code+"_NAME"
myattr2.comment = Now()
myattr2.datatype = "Integer"

I've created an extension and in Profile/Entity/Event Handlers I initially used the Initialize event handler. The two attributes were created as "Entity17 Id" and "Entity17 Name". Then I tried the event handler Validate, and it somehow works. The problem is that the attributes are not included when I open the properties dialog, but when I close it. So I have to open it, do some silly change, like a space in the comment, and close it and it worked. However, the next time I open the properties dialog and close it again, it tries to create the attributes again, so it fails.

Is there any way to do this better? Ideally the best scenario would be to create an Entity, then I change the default name, and when I go to Attributes tab, I find the attributes "myEntity Id" and "myEntitity Name" already created.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Jun 27 at 09:36 AM

    There are several options here.

    1. You could add your script as a Method, enhancing it to make sure it doesn't try adding those attributes if they already exist. Now add the method to a menu, so you can right-click an entity to add the attributes
    2. Let the model validation do it for you. Add a custom check that fails if one or both of those attributes is missing, with an autofix script that adds the attributes in the right place in the attributes collection if they don't already exist

    Add comment
    10|10000 characters needed characters exceeded

  • Jun 27 at 12:26 PM

    Thank you, George McGeachie, for your reply. However my knowledge on extending PD is not that deep. I have no idea on how to implement the option 2, but at least I found how to create the method as you suggested with code

    Sub %Method%(obj)
       ' Implement your method on <obj> here
    dim i as integer = 0
    for each attr in obj.Attributes
    if attr.name = attr.name = obj.Name+" Id" then
    i = i + 1
    end if
    next attr
    if i = 0 then
    dim myattr
    set myattr = obj.Attributes.CreateNew
    myattr.name = obj.Name+" Id"
    myattr.code = obj.Code+"_ID"
    myattr.comment = Now()
    myattr.datatype = "Integer"
    end if
    set i = 0
    for each attr in obj.Attributes
    if attr.name = attr.name = obj.Name+" Name" then
    i = i + 1
    end if
    next attr
    if = 0
    dim myattr2   
    set myattr2 = obj.Attributes.CreateNew
    myattr2.name = obj.Name+" Name"
    myattr2.code = obj.Code+"_NAME"
    myattr2.comment = Now()
    myattr2.datatype = "Varchar(50)"
    end if
    End Sub 

    But I don't how to call that method from a right-click menu item. I've checked all the possible options of the metaclass Entity, but I didn't find anything there to be added such a right click menu element. I browsed the documentation but it is not really good, tbh. Could you give me a hint? I'd really appreciate it.

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 30 at 08:55 AM

    Perhaps you could use better the Initialize event handler. It should create your attributes during the creation of the Entity. And if you want to make sure, your attributes won

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 30 at 08:57 AM

    Perhaps you could use better the Initialize event handler. It should create your attributes during the creation of the Entity. And if you want to make sure, your attributes won`t be delete later on, combine it with Validate handler (which is performed, as you correctly wrote, after each silly change).

    Ondrej

    Add comment
    10|10000 characters needed characters exceeded