Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
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();
}
Solved! Go to Solution.
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
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.
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();
}
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
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();
}
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
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.
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
Hi @smcvr
Does exist the parameter on the WTDocument type? And what is the internal definition of the attribute?
PetrH
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
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
To me, this code looks like it should work. Here's some debugging tips:
String subtype = doc.getDisplayType().getLocalizedMessage(java.util.Locale.US);
if (subtype.equals("XXXX")){
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
Hi @HelesicPetr
When I took the attribute value as an array, the code worked.
Thank you for your ideas 🙂
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.
Why not clearly state the problem when you initially post?
Your post is a bit ambiguous.