Solution Code Library
Solution Code Libraries are developed within a Solution SDK project and deployed automatically as part of the solution package. Unlike Centralized Code Libraries, they are tightly coupled with the solution lifecycle and typically assigned to Org or Org/Tag level for isolation.
Example Scenario
This example demonstrates creating a simple ApplicantValidator Code Library within a solution project and using it in a Fluent Function. The Code Library validates applicant data and is deployed with the solution to Journey Manager.
Steps to build the example
1. Configure solution project
Ensure your Solution SDK project is configured correctly. See Maven Setup for details.
2. Update pom.xml
Open your solution project's pom.xml and update the libPackageName and libClassName elements with your Code Library details:
<libPackageName>com.avoka.tm.library.validator</libPackageName>
<libClassName>ApplicantValidator</libClassName>
3. Configure services in app-package-def.json
Open the app-package-def.json configuration file and add the service JSON configuration file names under the services property:
{
"services": [
"ValidateApplicantFunc.json"
]
}
4. Scaffold service and Code Library
Run the following commands to create the service and Code Library:
# Create service classes and functions from configuration files
mvn initialize tm-sdk:app-scaffold-func
# Create Code Library specified in pom.xml
mvn initialize tm-sdk:code-lib-scaffold
Alternatively, scaffold the Code Library with explicit parameters:
mvn initialize tm-sdk:code-lib-scaffold \
-DlibPackageName="com.avoka.tm.library.validator" \
-DlibClassName="ApplicantValidator"
See Maven Commands for more options.
5. Implement the Code Library
ApplicantValidator.groovy
package com.avoka.tm.library.validator
import groovy.transform.TypeChecked
@TypeChecked
class ApplicantValidator {
boolean isValidEmail(String email) {
if (!email) return false
return email.matches(/^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$/)
}
boolean isValidAge(int age) {
return age >= 18 && age <= 120
}
String validateApplicant(String email, int age) {
List<String> errors = []
if (!isValidEmail(email)) {
errors.add("Invalid email format")
}
if (!isValidAge(age)) {
errors.add("Age must be between 18 and 120")
}
return errors.isEmpty() ? "Valid" : errors.join(", ")
}
}
6. Implement the service
ValidateApplicantFunc.groovy
package com.func
import com.avoka.tm.func.*
import com.avoka.tm.library.validator.ApplicantValidator
import com.avoka.tm.util.*
import groovy.transform.TypeChecked
@TypeChecked
class ValidateApplicantFunc {
public Logger logger
FuncResult invoke(FuncParam param) {
FormFuncResult result = new FormFuncResult()
// Instantiate Code Library
ApplicantValidator validator = new ApplicantValidator()
// Get applicant data from form
String email = param.formData.get("email")
int age = param.formData.get("age") as Integer
// Validate using Code Library
String validationResult = validator.validateApplicant(email, age)
logger.info("Validation result: " + validationResult)
if (validationResult != "Valid") {
result.addMessage(validationResult)
}
return result
}
}
7. Deploy to Journey Manager
Deploy the solution including Code Libraries:
mvn clean test initialize app-deploy
What this does
- Compiles and tests all code
- Type checks Code Libraries
- Packages application and Solution Code Libraries
- Deploys everything to Journey Manager
Note - Solution Code Libraries are deployed automatically with the solution. You do not deploy them separately like Centralized Code Libraries.
8. Verify deployment
After deployment:
- View Code Libraries in Journey Manager console to confirm the
ApplicantValidatorlibrary is available - Check Service Definition to ensure it's assigned to the correct Org/Tag
- Test the form to verify the validation logic works correctly
- Review Groovy Log in Transaction Details to see validation messages