我需要更新2个对象上的相同字段。当我更新对象A上的一组特定字段时,我想更新对象B上的相同字段。
我已使用json格式将此信息存储在自定义元数据中。
现在我将字段映射到地图mapFieldWiseSchema
和也我已经另一个地图存储该改变的字段只 地图> fieldChangedMapChanged
因此,第一个地图将返回: “ Filed1”(在对象A中):“ Filed1(在对象B中)”
第二地图将返回: ID:“ Field1”,“ filed2”,“ field3”
现在我很困惑如何映射文件更改的映射和文件映射。
下面是我的完整代码。
触发
如果(Trigger.isAfter){
if(!JobApplicationTriggerAction.backupJobAppSynced) {
Map<Id, List<String>> fieldChangedMap = new Map<Id, List<String>>();
if (Trigger.isUpdate) {
//Get fields which needs to be sync which are updated / changed
fieldChangedMap = JobAppFieldChange.filedChangeSet(Trigger.newMap, Trigger.oldMap);
if(fieldChangedMap != null){
JobApplicationTriggerAction jobapp = new JobApplicationTriggerAction();
jobapp.UpdateSyncJob(Trigger.newMap,fieldChangedMap,trigger.new);
}
}
}
}
public class JobAppFieldChange {
public static Map<Id, List<String>> filedChangeSet(Map<Id, Test__JobApp__c> NewMap, Map<Id, Test__JobApp__c> OldMap){
Map<Id, List<String>> fieldChangedMap = new Map<Id, List<String>>();
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Test__JobApp__c.fields.getMap();
List<String> fieldToSync = new List<String>{'Id', 'Name', 'Test__order__c','Test__Name__c','Test__status__c','Test__Test__c'};
for( Id jobAppId : NewMap.keySet()) {
Test__JobApp__c newjobApp = NewMap.get(jobAppId);
Test__JobApp__c oldjobApp = OldMap.get(jobAppId);
List<String> changedFieldSet = new List<String>();
for (String fieldName : fieldMap.keyset()) {
try {
if(newjobApp.get(fieldName) != oldjobApp.get(fieldName)){
if(fieldToSync.contains(fieldName)) {
changedFieldSet.add(fieldName);
}
}
} catch (Exception e) {
System.debug('Error: ' + e);
}
}
fieldChangedMap.put(jobAppId, changedFieldSet);
}
return fieldChangedMap;
}
}
public Set<id> ids {get;set;}
private Set<Id> JobappIds;
Map<Id, List<String>> fieldChangedMap;
string qry = '';
String strJSONBody;
map<Schema.SObjectField,Schema.SObjectField> mapFieldWiseSchema=new map<Schema.SObjectField,Schema.SObjectField>();
map<string, Schema.SObjectField> mapObjectWiseFieldWiseSchemaField=new map<string, Schema.SObjectField>();
public Map<string,string> getAllMappingTable(){
Map<string, string> MapMappingTable=new map<string,string>();
list<string> objects=new list<string>();
objects.add('Test__JobAppBackup__c');
objects.add('Test__JobApp__c');
for(string s:objects){
Map <String, Schema.SObjectType > m = Schema.getGlobalDescribe();
Schema.SObjectType sT = m.get(s);
Schema.DescribeSObjectResult r = sT.getDescribe();
Map<String, Schema.SObjectField> mapData = r.fields.getMap();
system.debug('Map Value-->' + mapData.values());
Schema.DescribeFieldResult F;
for(Schema.SObjectField sField:mapData.values()){
F= sField.getDescribe();
string sKey=s + F.getName();
mapObjectWiseFieldWiseSchemaField.put(sKey,sField);
}
}
system.debug('mapObjectWiseFieldWiseSchemaField' + mapObjectWiseFieldWiseSchemaField);
qry ='';
try{
Test__JobAppMap__mdt syncMetaMdt = [SELECT Test__JsonMap__c FROM Test__JobAppMap__mdt ];
if(syncMetaMdt != null) {
strJSONBody = syncMetaMdt.Test__JsonMap__c;
}
if (syncMetaMdt.Test__JsonMap__c != null ){
MapMappingTable =(Map<String, String>) JSON.deserialize(strJSONBody, Map<String, String>.class);
}
for(string sfieldname:MapMappingTable.keySet()){
string skey1='Test__JobApp__c' + sfieldname;
string sKey2='Test__JobAppBackup__c' + MapMappingTable.get(sfieldname);
system.debug('skey1' + sKey1);
system.debug('skey2' + sKey2);
if(mapObjectWiseFieldWiseSchemaField.get(skey1)!=null && mapObjectWiseFieldWiseSchemaField.get(skey2)!=null)
mapFieldWiseSchema.put(mapObjectWiseFieldWiseSchemaField.get(skey1),mapObjectWiseFieldWiseSchemaField.get(skey2));
}
system.debug('mapFieldWiseSchema' + mapFieldWiseSchema);
}catch(exception ex){
String msg = ex.getMessage();
//currentMsg = new ApexPages.Message(ApexPages.severity.ERROR, msg);
}
return MapMappingTable;
}
public PageReference UpdateSyncJob(map<Id, Test__JobApp__c> NewMap,Map<Id, List<String>> fieldChangedMapChanged, List<est__JobApp__c> jobAppList) {
getAllMappingTable();
list<sObject> lstsObjects=new list<sObject>();
list<Test__JobAppBackup__c> listJobBackupToUpdate=new list<Test__JobAppBackup__c>();
fieldChangedMap = fieldChangedMapChanged;
List<String> fieldChangeSet ;
List<String> jobbackupIds = new List<String>();
for(Thiru_Test__JobApp__c Jobapps : jobAppList){
}
if(listJobBackupToUpdate.size()>0)
{
Savepoint sp;
try
{
sp = Database.setSavepoint();
update listJobBackupToUpdate;
update lstsObjects;
}
catch(Exception e)
{
String msg = e.getMessage();
Database.rollback(sp);
ApexPages.Message currentMsg = new ApexPages.Message(ApexPages.severity.ERROR, msg);
ApexPages.addMessage(currentMsg);
}
}
return null;
}
我没有完成此操作。请帮助我编写映射filedchangeset和文件映射的逻辑。