Hello Phuoc Dinh,
Thank you for using Microsoft Dynamics Communities.
To create a custom validation rule, you need to:
• Add new member to the RetailTransactionValidationType extensible enum
• Create a class that extends RetailTransactionValidationBase
• For header validations, you need to create a method that resembles the one below:
[
RetailTransactionValidation(RetailTransactionValidationRuleType::TransactionLevel, "@Retail:ConsistencyCheckerCustomerAccountDescription"),
RetailTransactionValidationApplicableToAllTypes
]
public boolean validateCustAccount() {}
• For line validations, you need to create a method that resembles the one below:
[
RetailTransactionValidation(RetailTransactionValidationRuleType::LineLevel, "Validates if the serial number contains unprintable characters"),
RetailTransactionValidationApplicableToAllTypes
]
internal void validateSerialItemWithUnprintableChars(ItemInfo _itemInfo) {}
Whenever you detect a problem, you should use either:
• this.insertValidationErrorForTransaction(RetailTransactionValidationType::CUSTOM_ENUM, errorMessage)
for header level failures.
• this.insertValidationErrorForTransactionWithLineReference(RetailTransactionValidationType:: CUSTOM_ENUM, errorMessage, _itemInfo.LineNumber)
for line level failures.
By doing this, the validation failure will be caught by the framework and we will automatically mark the transaction as failed.
You can use RetailTransactionValidationHeader & RetailTransactionValidationLine as examples.
Note: once you do this, the new rule should automagically show up in Commerce Parameters > Transaction Validation.
Hope it helps.
Juliet Zhu
If my information answers your question please click 'Yes' against "Did this answer your question?" to help other community members.