cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

How can I get the "SOFPART" attribute value of the document type object in the promotion?

smcvr
14-Alexandrite

How can I get the "SOFPART" attribute value of the document type object in the promotion?

How can I get the "SOFPART" attribute value of the document type object in the promotion?

SOFPART contains multiple string values

 

I wrote a code to deal with this situation, but the attribute value is empty. Where am I making a mistake?

CODE:

 

try {
    wt.maturity.PromotionNotice pn = (wt.maturity.PromotionNotice) primaryBusinessObject;
    wt.fc.QueryResult qr = wt.maturity.MaturityHelper.service.getBaselineItems(pn);
    
    while (qr.hasMoreElements()) {
        java.lang.Object obj=(java.lang.Object)qr.nextElement();

        if (obj instanceof wt.doc.WTDocument) {
              wt.doc.WTDocument wtdoc = (wt.doc.WTDocument) obj;
            wt.content.ContentHolder contentHolder = wt.content.ContentHelper.service.getContents(wtdoc);
            wt.content.ContentItem item = wt.content.ContentHelper.getPrimary((wt.content.FormatContentHolder) contentHolder);
              com.ptc.core.lwc.server.PersistableAdapter persistableAdapter = new com.ptc.core.lwc.server.PersistableAdapter(wtdoc, null, java.util.Locale.getDefault(), new com.ptc.core.meta.common.UpdateOperationIdentifier());
                persistableAdapter.load("SOFPART");
                SOFPART = (java.lang.String) persistableAdapter.get("SOFPART");
        }
    }
} catch (Exception wte) {
    wte.printStackTrace();
}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @joe_morton 

In his case there is a issue with the retyping the result object.

SOFPART = (java.lang.String) obj2.get("SOFPART");

 If the attribute contains multi values the return object is an array with a string not a String so this is the case why his final variable is empty.

PetrH

View solution in original post

19 REPLIES 19
avillanueva
22-Sapphire I
(To:smcvr)

Is "SOFTPART" an IBA on the WTDocument? Or are you trying to get the name a soft typed sub-type of WTDocument? If its an IBA, I can post code for that. Might already be posted. Also looks like you are interrogating the primary content of the WTDocument. Why? Attributes should not be part of the content file.

smcvr
14-Alexandrite
(To:avillanueva)

Hi @avillanueva,

 

"SOFTPART" is an IBA in WTDocument

avillanueva
22-Sapphire I
(To:smcvr)
smcvr
14-Alexandrite
(To:avillanueva)

Hi @avillanueva ,

I tried to get the "SOFPART" value defined in the document added to the promotion with iba, but it did not work.

I don't understand where I'm making a mistake, can you give me an idea?

CODE:

try {
    wt.maturity.PromotionNotice pn = (wt.maturity.PromotionNotice) primaryBusinessObject;
    wt.fc.QueryResult qr = wt.maturity.MaturityHelper.service.getBaselineItems(pn);
    while (qr.hasMoreElements()) {
        java.lang.Object obj=(java.lang.Object)qr.nextElement();
        if (obj instanceof wt.doc.WTDocument) {
              wt.doc.WTDocument wtdoc = (wt.doc.WTDocument) obj;
            wt.content.ContentHolder contentHolder = wt.content.ContentHelper.service.getContents(wtdoc);
            wt.content.ContentItem item = wt.content.ContentHelper.getPrimary((wt.content.FormatContentHolder) contentHolder);
            java.util.Locale locale = wt.session.SessionHelper.getLocale(); 
               com.ptc.core.lwc.server.LWCNormalizedObject obj2 = new com.ptc.core.lwc.server.LWCNormalizedObject(wtdoc, null, locale, new com.ptc.core.meta.common.UpdateOperationIdentifier());
               obj2.load("SOFPART","Arac_Kodu");
                SOFPART = (java.lang.String) obj2.get("SOFPART");
                Arac_Kodu = (java.lang.String) obj2.get("Arac_Kodu");
        }
    }
} catch (Exception wte) {
    wte.printStackTrace();
}

 

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

What windchill version do you use? 

I would use new API PersistableAdapter 

https://www.ptc.com/en/support/article/CS138252?source=search

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ,

 

Thank you.
I changed the code but still the values are empty

 

try {
    wt.maturity.PromotionNotice pn = (wt.maturity.PromotionNotice) primaryBusinessObject;
    wt.fc.QueryResult qr = wt.maturity.MaturityHelper.service.getBaselineItems(pn);
    while (qr.hasMoreElements()) {
        java.lang.Object obj=(java.lang.Object)qr.nextElement();
        if (obj instanceof wt.doc.WTDocument) {
              wt.doc.WTDocument wtdoc = (wt.doc.WTDocument) obj;
            wt.content.ContentHolder contentHolder = wt.content.ContentHelper.service.getContents(wtdoc);
            wt.content.ContentItem item = wt.content.ContentHelper.getPrimary((wt.content.FormatContentHolder) contentHolder);
              com.ptc.core.lwc.server.PersistableAdapter obj2 = new com.ptc.core.lwc.server.PersistableAdapter(wtdoc, null, wt.session.SessionHelper.getLocale(), new com.ptc.core.meta.common.DisplayOperationIdentifier());
               obj2.load("SOFPART","Arac_Kodu");
                SOFPART = (java.lang.String) obj2.get("SOFPART");
                Arac_Kodu = (java.lang.String) obj2.get("Arac_Kodu");
        }
    }
} catch (Exception wte) {
    wte.printStackTrace();
}

 

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

Do your SOFTPART IBA definition is exactly "SOFTPART "?

I don't believe that the function does not support a multi value attributes.  

Perhaps not. If not you could use the another method that I have mentioned in the provided link by @avillanueva . 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ,

 

SOFTPART is multiple values but Vehicle_Code is a single value and both are empty

smcvr
14-Alexandrite
(To:smcvr)

By the way, when I want to get the value of the part type object with this code, it gets it, but not the attribute value of the document type object.

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

try to use null to all other inputs

 

com.ptc.core.lwc.server.PersistableAdapter obj2 = new com.ptc.core.lwc.server.PersistableAdapter(wtdoc,null,null,null);

 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ,

 

Unfortunately, it didn't happen.

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

Does exist the parameter on the WTDocument type? And what is the internal definition of the attribute? 

PetrH

HelesicPetr
21-Topaz II
(To:smcvr)

HI @smcvr 

I don't know what you have done wrong but in my case it works. 

The multivalue attribute returns an array of a string

HelesicPetr_0-1707310193512.png

 

PS> your catch (Exception wte)  can case that the parameters are empty because you don't know about the error that you can not cast the Array to a string 😄

 

PetrH 

joe_morton
17-Peridot
(To:smcvr)

To me, this code looks like it should work. Here's some debugging tips:

 

  • it doesn't look like you're using contentHolder or item - maybe try removing those
  • when you do obj2.load - make sure you are using the internal names of the attributes exactly. I believe these are case-sensitive
  • is the SOFPART attribute added to WTDocument, or to a subtype? If it's on a subtype, you may need to add logic to ensure you are only dealing with WTDocuments of that subtype. I've used code that does something like this:
String subtype = doc.getDisplayType().getLocalizedMessage(java.util.Locale.US);
		
		if (subtype.equals("XXXX")){
  • Additionally, you can always debug further by printing lines out to the methodserver logs. Ex: System.out.println("debug message");

Hope that's helpful!

Hi @joe_morton 

In his case there is a issue with the retyping the result object.

SOFPART = (java.lang.String) obj2.get("SOFPART");

 If the attribute contains multi values the return object is an array with a string not a String so this is the case why his final variable is empty.

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr 

 

When I took the attribute value as an array, the code worked.

 

Thank you for your ideas 🙂

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

I have an advice for you.

If your code does not work, always open a Windchill log method server and check the errors. 

The error with the retyping had to be there because you use a try with catch (Exception wte)

PetrH

Thanks, I should refactor that API and update my code.

d_graham
17-Peridot
(To:smcvr)

Why not clearly state the problem when you initially post?

Your post is a bit ambiguous. 

Top Tags