{"id":17405053,"url":"https://github.com/jongpie/apexvalidationrules","last_synced_at":"2026-02-25T23:02:02.180Z","repository":{"id":46439328,"uuid":"145098346","full_name":"jongpie/ApexValidationRules","owner":"jongpie","description":"An Apex library for validating field values on SObject records, using configurable custom metadata types","archived":false,"fork":false,"pushed_at":"2021-10-12T21:54:01.000Z","size":232,"stargazers_count":31,"open_issues_count":2,"forks_count":11,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-10T21:50:57.233Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Apex","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jongpie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-17T09:06:04.000Z","updated_at":"2025-05-02T04:20:31.000Z","dependencies_parsed_at":"2022-08-12T12:50:55.061Z","dependency_job_id":null,"html_url":"https://github.com/jongpie/ApexValidationRules","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/jongpie/ApexValidationRules","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongpie%2FApexValidationRules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongpie%2FApexValidationRules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongpie%2FApexValidationRules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongpie%2FApexValidationRules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jongpie","download_url":"https://codeload.github.com/jongpie/ApexValidationRules/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jongpie%2FApexValidationRules/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29844845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T22:37:40.667Z","status":"ssl_error","status_checked_at":"2026-02-25T22:37:25.960Z","response_time":61,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-16T20:22:44.043Z","updated_at":"2026-02-25T23:02:02.140Z","avatar_url":"https://github.com/jongpie.png","language":"Apex","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apex Validation Rules for Salesforce\n\n[![Deployment](https://github.com/jongpie/ApexValidationRules/actions/workflows/deploy.yml/badge.svg)](https://github.com/jongpie/ApexValidationRules/actions/workflows/deploy.yml)\n[![codecov](https://codecov.io/gh/jongpie/ApexValidationRules/branch/main/graph/badge.svg?token=1DJPDRM3N4)](https://codecov.io/gh/jongpie/ApexValidationRules)\n\n---\n\n\u003ca href=\"https://githubsfdeploy.herokuapp.com/?owner=jongpie\u0026repo=ApexValidationRules\u0026ref=main\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Deploy to Salesforce\" src=\"https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png\"\u003e\n\u003c/a\u003e\n\nAn Apex library for validating field values on `SObject` records, using configurable custom metadata types\n\n1. Create validation rules via `RecordValidationRule__mdt` custom metadata type\n2. Call `RecordValidator` class in your Apex code or call `FlowRecordValidator` in your Flow to validate the your records pass your validation rules\n\nThis is useful in scenarios where standard validation rules cannot be used\n\n1. On-demand validation: standard validation rules only run on during DML statements. Apex Validation Rules can be executed at any point in your code.\n2. Apex-only contexts: standard validation rules cannot run in some contexts, such as \"after delete\" trigger contexts or after workflow field updates/approval processes.\n\n3. Apex-only contexts: standard validation rules cannot run in some contexts, such as \"after delete\" trigger contexts or after workflow field updates/approval processes\n\n## Examples\n\n### Simple Apex Example: Validating Account Name\n\nAs a simple example, you can setup a custom rule to validate that an Account record does not have the name 'Some Account'. This is configured with:\n\n-   Creating 1 rule in `RecordValidationRule__mdt`\n-   Creating 1 condition in `RecordValidationRuleCondition__mdt` and relating it to the parent rule\n-   Calling the `RecordValidator` class within Apex, which leverages the custom metadata records\n\nThis screenshot shows what the rule \u0026 condition look like once they've been configured\n\n![Example Validation Rule: Account Name](./assets/validation-rule-example-account-name.png)\n\nOnce the rule \u0026 condition have been setup, you can then call the Apex class to validate the record. In this case, the Apex is using the name 'Some Account', which is not allowed based on how the rule \u0026 condition have been setup\n\n```java\n    Account someAccount = new Account(Name = 'Some Account');\n    new RecordValidator(someAccount).validate();\n```\n\nWhen you run `new RecordValidator(someAccount).validate();`, it will throw an exception to indicate that the Account is not valid based on the configured rule.\n\n![Example Validation Rule: Account Name Exception](./assets/validation-rule-example-account-name-exception.png)\n\nIf you want to simply see if a record has any validation errors (but you don't want to throw an exception), you can use the `getResults()` method like this:\n\n```java\n    Account someAccount = new Account(Name = 'Some Account');\n    Boolean shouldThrowAnException = false;\n    List\u003cRecordValidator.ValidationRuleResult\u003e results = new RecordValidator(someAccount).validate(shouldThrowAnException);\n\n    // For each returned result, show the result details - in this particular example, there will be only 1 result\n    for (RecordValidator.ValidationRuleResult result : results) {\n        System.debug('result.hasError==' + result.hasError);\n        System.debug('result.errorMessage==' + result.errorMessage);\n        System.debug('result.conditions==' + result.conditions);\n        System.debug('result.conditionsLogic==' + result.conditionsLogic);\n        System.debug('result.conditionsLogicType==' + result.conditionsLogic);\n    }\n```\n\n### Complex Apex Example: Validating Multiple Fields on Account\n\nYou can also setup multiple conditions for a rule, using 1 of 3 types of logic (similar to setting up custom conditions for list views, reports and other standard functionality within Salesforce)\n\n1. AND: all conditions must be met\n2. OR: any conditions can be met\n3. Custom: you can write your own complex logic, such as `(1 AND (2 OR 3))` - the `RecordValidator` class will automagically parse your conditions based on the sort order you specify for each condition (stored in the field `RecordValidationRuleCondition__mdt.SortOrder__c`)\n\nIn this example, custom logic is used to validate that the fields `AccountNumber` and `Site` are populated when `AccountSource` == 'Web'\n\n![Example Validation Rule: Multiple Account Fields](./assets/validation-rule-example-account-mulitple-fields.png)\n\n```java\n    Account someAccount = new Account(Name = 'My New Account', Type = 'Web');\n    new RecordValidator(someAccount).validate();\n```\n\nWhen you run `new RecordValidator(someAccount).validate();`, it will throw an exception to indicate that the Account is not valid based on the configured rule.\n\n![Example Validation Rule: Multiple Account Fields](./assets/validation-rule-example-account-mulitple-fields-exception.png)\n\nSame thing applies here: you can simply call `getResults()` instead of `validate()` if you want to see the results (instead of throwing an exception).\n\n```java\n    Account someAccount = new Account(Name = 'My New Account', Type = 'Web');\n    Boolean shouldThrowAnException = false;\n    List\u003cRecordValidator.ValidationRuleResult\u003e results = new RecordValidator(someAccount).validate(shouldThrowAnException);\n\n    // For each returned result, show the result details - in this particular example, there will be only 1 result\n    for (RecordValidator.ValidationRuleResult result : results) {\n        System.debug('result.hasError==' + result.hasError);\n        System.debug('result.errorMessage==' + result.errorMessage);\n        System.debug('result.conditions==' + result.conditions);\n        System.debug('result.conditionsLogic==' + result.conditionsLogic);\n        System.debug('result.conditionsLogicType==' + result.conditionsLogic);\n    }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjongpie%2Fapexvalidationrules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjongpie%2Fapexvalidationrules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjongpie%2Fapexvalidationrules/lists"}