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

how to create DynamicConstraint with beanshell (to write java logic more than one statement ) through impex

Former Member
0 Likes
953

Hi ,

I am creating an impex by using DynamicConstraints as below by refering OOB feature.

 INSERT_UPDATE DynamicConstraint; active[allownull = true, default = true]; id[unique = true]; severity(code, itemtype(code)); message[lang = en]; target[allownull = true]; type(code); language(code); expression; constraintGroups(id);
 ; ; ProductIsEXConstraint ; ERROR:Severity ; The attribute value should not be null. ; de.hybris.platform.core.model.product.ProductModel ; Product ; BEANSHELL ; return getProductTypeCategoryCode().getCode().equalsIgnoreCase("mattresses")? true:false ; pcmCoreAttributesCoverageGroup ;

Here Condition in the above scripts is specific to some particular products only but the error message is showing in core attributes irrespective of products.How to add condition to particular product category or how to write java logic with more than one statement in beanshell. Kindly let us know if anyone faced the same issue.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Likes

It worked with ? false:true. the validation message works when it returns false.I was testing reverse way.

Thanks Arvind

arvind-kumar_avinash
Active Contributor
0 Likes

Great! You are most welcome.

Former Member
0 Likes

Thanks Arvind,

Yes , I have followed the same syntax of ApprovalStatus attribute. getProductTypeCategoryCode() method will be available in productModel to find the category of the product. The issue here is validation is working for both mattresses and other products as well. I need to apply the validation to only same particular products. Kindly let me know if any issue.

arvind-kumar_avinash
Active Contributor
0 Likes

Did you try after removing ? true:false from your expression?

arvind-kumar_avinash
Active Contributor
0 Likes

You don't need ? true:false in the expression i.e. it should be just

 return getProductTypeCategoryCode().getCode().equalsIgnoreCase("mattresses")

The Creating Dynamic Constraints section states:

General regulation to write DynamicConstraint script, is that the expression must return object, which somehow can be evaluated as a boolean true or false value (logic or textual).

Another thing that I am not able to understand in your code is getProductTypeCategoryCode(). There is nothing like productTypeCategoryCode in OOTB ProductModel. Is this your custom attribute? If it is a custom attribute of some enumtype, are you sure the enumtype has <value code="mattresses"/>?

Please look at the code given below:

 INSERT_UPDATE DynamicConstraint; active[allownull = true, default = true]; id[unique = true]; severity(code, itemtype(code)); message[lang = en]; target[allownull = true]; type(code); language(code); expression; constraintGroups(id);
 ; ; ProductApprovedDynamicConstraint ; ERROR:Severity ; The attribute value for "Approval" should be "approved". ; de.hybris.platform.core.model.product.ProductModel ; Product ; BEANSHELL ; return getApprovalStatus().getCode().equalsIgnoreCase("approved") ; pcmCoreAttributesCoverageGroup ;

You can call getApprovalStatus() on a Product instance (you can test it by executing the following groovy code) because the OOTB ProductModel has an attribute, approvalStatus:

 product = modelService.create("Product")
 product.name='Test Product'
 product.code='12345'
 println product.getApprovalStatus()

But with the OOTB ProductModel, the following code will throw error:

 println product.getProductTypeCategoryCode()