{"id":16981797,"url":"https://github.com/cesarparra/sobjectconverter","last_synced_at":"2026-03-18T23:03:54.368Z","repository":{"id":115969675,"uuid":"197063510","full_name":"cesarParra/SObjectConverter","owner":"cesarParra","description":"Apex library to assist with converting between different SObjects or cloning records.","archived":false,"fork":false,"pushed_at":"2019-07-16T22:18:44.000Z","size":18,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-14T17:03:37.784Z","etag":null,"topics":["apex","salesforce","sfdx","sobject"],"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/cesarParra.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-15T19:56:20.000Z","updated_at":"2024-03-19T17:37:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"0dde67c3-ca21-4e99-b97f-c8a5a93e26be","html_url":"https://github.com/cesarParra/SObjectConverter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesarParra%2FSObjectConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesarParra%2FSObjectConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesarParra%2FSObjectConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cesarParra%2FSObjectConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cesarParra","download_url":"https://codeload.github.com/cesarParra/SObjectConverter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244885515,"owners_count":20526293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["apex","salesforce","sfdx","sobject"],"created_at":"2024-10-14T02:06:29.682Z","updated_at":"2025-10-14T12:10:41.252Z","avatar_url":"https://github.com/cesarParra.png","language":"Apex","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apex SObjectConverter\n\nApex library to assist with converting between different SObjects or cloning records.\n\n## Basics\n\nA single class is in charge of orchestrating any record conversion or cloning operation, the conv_SObjectConverter.\n\nTo create a custom converter the `conv_SObjectConverter` should be extended and it's 2 abstract methods implemented:\n\n```java\npublic class MyCustomLeadToAccountConverter extends conv_SObjectConverter {\n  protected override Schema.SObjectType getTargetSObjectType() {\n        return Account.SObjectType;\n    }\n\n    protected override Map\u003cSchema.SObjectField, Schema.SObjectField\u003e getTargetFieldBySourceFieldMap() {\n        return new Map\u003cSchema.SObjectField, Schema.SObjectField\u003e {\n            Lead.Description =\u003e Account.Description, \n            Lead.NumberOfEmployees =\u003e Account.NumberOfEmployees\n        };\n    }\n}\n```\n\nSome additional optional methods are provided to create more powerful conversions:\n\n```java\nprotected virtual void beforeConvert(SObject source);\n```\n\nAllows you to hook into the conversion process before it begins, giving you the source SObject that will be converted.\n\n```java\nprotected virtual void afterConvert(SObject source, SObject resultRecord);\n```\n\nAllows you to hook into the conversion process after it ends, giving you the source converted SObject as well as the resulting record.\n\n```java\nprotected virtual Object onPopulateField(SObject sourceRecord, SObjectField sourceField, Object dataToTransfer);\n```\n\nCalled on each field being converted. Allows you to override the value (dataToTransfer) that will be populated on the target field.\n\n## Conversion Contexts\n\nTo power how fields are converted, `conv_SObjectConverter` uses Conversion Contexts (`conv_SObjectConverter.ConversionContext`).\n\nThese take care of converting data between fields which types might not match from the defined getTargetFieldBySourceFieldMap.\n\nBy default the following Conversion Contexts are used by the converter:\n\n| Conversion Context | Description |\n| ------------- | ------------- |\n| Same Context  | Converts fields that are of the same type.  |\n| Any To String  | Converts any data type to a String using String.valueOf() |\n| Number To Boolean  | Converts 0 to false, and any other number to true |\n| String To Boolean  | Converts the words \"yes\" and \"true\" (case insensitive) to true, and \"no\" and \"false\" to false. |\n\n### Custom Conversion Contexts\n\nYou can create your custom `ConversionContext` implementations which allows you to create more types of data conversions or override the default ones.\n\nTo create a custom context you can do the following:\n1.  Create an implementation of `conv_SObjectConverter.ConversionContext`\n\n2 methods should be implemented: \n\n#### getTransferableData\n\n```java\nObject getTransferableData(SObject sourceRecord, SObjectField sourceField);\n```\n\nImplementation of the logic applied when converting from one type to another. For example, in the case of the Any To String implementation, this is \n\n```java\npublic Object getTransferableData(SObject sourceRecord, SObjectField sourceField) {\n  return String.valueOf(sourceRecord.get(sourceField));\n}\n```\n\n#### meetsContextCriteria\n\n```java\nBoolean meetsContextCriteria(Schema.DisplayType sourceFieldType, Schema.DisplayType targetFieldType);\n```\n\nDetermines whether the context applies based on the field types.\n\n2. Register your context when your your `conv_SObjectConverter`.\n   \n```java\n// Assuming we have a custom conv_SObjectConverter.ConversionContext called CustomContext.\nnew conv_SObjectConverter()\n  .addConversionContext(new CustomContext())\n  .convert(myRecord);\n```\n\n\n## Example - Translating Records\n\nAn example implementation of a converter to translate record field values is provided in the `/sample` module. \n\nThat example shows the capabilities of cloning a record rather than converting by returning the same received `SObjectType` as the target and using all populated fields for the map. Using the `afterConvert` method the resulting SObject's name is renamed with an `_es` suffix to denote that it is a translated version of a different record.\n\n```java\npublic with sharing class Translator extends conv_SObjectConverter {\n    ...\n    Schema.SObjectType targetType;\n    Map\u003cSchema.SObjectField, Schema.SObjectField\u003e targetFieldBySourceFieldMap;\n\n    public Translator() {\n        this.targetFieldBySourceFieldMap = new Map\u003cSchema.SObjectField, Schema.SObjectField\u003e();\n    }\n\n    protected override void beforeConvert(SObject source) {\n        // Since this acts as a cloner, then the target type will always be the same as the source's.\n        this.targetType = source.getSObjectType();\n\n        this.populateMapBasedOnQueriedFields(source);\n     }\n\n    protected override Schema.SObjectType getTargetSObjectType() {\n        return this.targetType;\n    }\n\n    protected override Map\u003cSchema.SObjectField, Schema.SObjectField\u003e getTargetFieldBySourceFieldMap() {\n        return this.targetFieldBySourceFieldMap;\n    }\n\n    protected override Object onPopulateField(SObject sourceRecord, SObjectField sourceField, Object dataToTransfer) { \n      ...\n    }\n\n    ...\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcesarparra%2Fsobjectconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcesarparra%2Fsobjectconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcesarparra%2Fsobjectconverter/lists"}