cancel
Showing results for 
Search instead for 
Did you mean: 

Static method calls with namespace qualifiers

former_member201819
Participant
0 Kudos

Dear community,

I'm currently facing a small problem in ABSL-coding, perhaps someone can help me:

After migrating our solution into a new tenant, static method calls like "Duration.ParseFromString("PT0H");" caused an error which stated it was neccessary to explicitly qualify the namespace as the data type "Duration" was found in several namespaces.

I was just told by a SAP dev (via support ticket causing delays on every other question), that I should follow the pattern:

var myResult = [namespace] :: [data type].[method];

Now when trying "var duration = apCommon::Duration.ParseFromString("PT0H");" I get the error "Object type 'apCommon' is not valid. Choose different object type."

>>Please see screenshot attached.

The documentation also doesn't provide a specific explanation on how to handle static method calls with namespace prefix.

Can anyone help me on this please?

Kind Regards

Pablo

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi Pablo,

Try as follows, specify as done in highlighted  area, Hope it should work.

Regards..

Hanu

former_member201819
Participant
0 Kudos

Dear Hanu,

thank you! That works fine.

Meanwhile, I also got an answer from another colleague, who suggested the following which also works for me (it's probably equivalent):

var duration = Library::Duration.ParseFromString("PT0H");

Kind Regards

Pablo

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Pablo,

the system detects naming conflicts that are caused when you use e.g.

1) an object (or data type) that exists with the same name in different namespaces:

In these cases you get an error message that the object was found in several namespaces and you need to use a namespace or alias to qualify the object.

E.g. 

import AP.Common.GDT as apCommonGDT;

element MyDuration : apCommonGDT:Duration;

Important is, that there is only one colon between the namspace alias and the data type.

2) if you use the same name for objects of different types e.g. you created a business object called  "Duration" and there is the SAP reuse library "Duration":

In this case you need to qualify the object with the object type.

E.g.

var duration = Library::Duration.ParseFromString("PT0H");

Please note that in this case two colons have to be entered between the object type and the object.

In general, the code completion helps you to resolve conflicts, so if you position the cursor on Duration in the first example, it provides you with the different options (e.g. namespaces), from which you can choose the correct one.

So to come back to your specific issue, it seems that there was some confusion because of the fact that you use the data type "Duration" (like in my first example) and the SAP reuse library "Duration" (second example). From my point of view, the qualification with the namespace would only be necessary for the usages of the data type. So probably your issue should also be solved if you remove the qualification for the reuse library e.g.

duration = Duration.ParseFromString("PT0H");

If I am wrong, then please make sure that you use only one colon between the namespace alias and the object.

The reason why the colleague recommended to qualify with the object type "Library" was probably caused by the typo in

duration = apCommon::Duration.ParseFromString("");

Because of the two colons, the system interpreted "apCommon" as object type, therefore it displayed the error message "Object type 'apCommon' is not valid.Choose different object type". And therefore it was recommeded to use the correct object type, which is "Library".

Hope this help.

Best regards,

Kornelia

former_member201819
Participant
0 Kudos

Hi Kornelia,

thank you for your detailed reply.

I'm a bit confused about your answer:

1) You refer to a variable definition whereas I was asking about a value assignment which is obviously handled differently by the system. But regarding this case, I think you're right when you say it should look like this:

var MyDuration : apCommonGDT:Duration;

This also worked for me before, my problem came up when I tried to use static methods of a BO.

2) I didn't create a BO with the same name as a standard SAP BO ("Duration" in this case).

You recommend to try no namespace specification as follows, but that initially led to the error message that I had to qualify the namespace which I tried afterwards:

duration = Duration.ParseFromString("PT0H");

The second suggestion...


duration = apCommon::Duration.ParseFromString("");

...is something I tried before which led to the error "Static reuse services cannot be called instance-based"(see my second variable "duration2" in the screenshot attached to my question), which doesn't make sense for me at all.

However, at least I don't have no error message anymore, thanks again and

Kind Regards

Pablo