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

Correct use of hasValue in a script

Former Member
0 Likes
502

Hi All,

We have implemented a project validation script that checks the length of a product category ID is 1 or gives the user an error. Our working script is:

ApplicationException ae = new ApplicationException(session);

catHome = IBeanHomeLocator.lookup(session, doc.getInternalCategory());

catBean = catHome.find(doc.getInternalCategory());

if (catBean.getCategoryId().length() != 1)

{

ae.chainAtEnd(doc.createApplicationException("INTERNAL_CAT", "CUSTOM", "prod_cat_1_error"));

}

if (ae.getChain() != null)

{

throw ae.getChain();

}

Due to a change in requirements it is now possible for the user to select no product category in the project. To make my script work I have been trying to use hasValue, however this keeps giving me the error: Attempt to resolve method: find() on undefined variable or class name: catHome

The change I made was:

ApplicationException ae = new ApplicationException(session);

if(hasValue(doc.getInternalCategory()))

{

catHome = IBeanHomeLocator.lookup(session, doc.getInternalCategory());

catBean = catHome.find(doc.getInternalCategory());

if (catBean.getCategoryId().length() != 1)

{

ae.chainAtEnd(doc.createApplicationException("INTERNAL_CAT", "CUSTOM", "prod_cat_1_error"));

}

}

if (ae.getChain() != null)

{

throw ae.getChain();

}

Is this incorrect use of hasValue ? What should I be using in this case?

Many Thanks

Dan

Accepted Solutions (1)

Accepted Solutions (1)

0 Likes

Hi Dan,

The usage of hasValue looks correct to me. Don’t see anything wrong. In fact, I copied your script and tried it and seems to be working ok. What version and SP are you on?

As a side note, instead of calling doc.getInternalCategory() multiple times, I’d store in a variable.

Something like…

intCat = doc.getInternalCategory();

if (hasValue(intCat)) {

catBean = IBeanHomeLocator.lookup(session, intCat).find(intCat);

}

Regards,

Vikram

Former Member
0 Likes

Hi Vikram,

Thank you for your advice on storing variables.

I am using version 9.0.04.00

The code works fine when a product category with ID length one is chosen and gives the user an error message correctly if they choose a product category with ID length two, however if the product category field is left blank I am getting the Attempt to resolve method: find() on undefined variable or class name: catHome error message.

Thanks

Dan

Former Member
0 Likes

Hi Vikram,

I spotted an issue in a different part of the script that was causing this.

Thank you for confirming the correct use of hasValue , the script has now met our new requirements.

Many thanks

Dan

0 Likes

Perfect. Thanks for the update.

Regards,

Vikram

Answers (0)