//拆分选项列表的共通方法 public without sharing class SplitOptionListUtil { public SplitOptionListUtil() { } //传递一个map进来 key是sobj对象 value是拼接好的选项列表api //1.根据sobj的id获取对象名 -> Id.getSObjectType().getDescribe().getName() //2.根据value获取选项列表对应的lable名 -> map> //3.查询当前的"报告书状况统计"表中是否存在对应的值 如果有全删除再添加 //4.获取对象上的字段值并创建map > ////key -> sobj对象 value: -> key :api对应的lable名称 value:内容集合 public void makeObject(Map> data){ System.debug('14:::data::'+data); //1.获取对象名 String objName = data.keySet().iterator().next().Id.getSObjectType().getDescribe().getName(); //2.获取对象上的 api->lable标签名的对应关系 Map schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType leadSchema = schemaMap.get(objName); Map apiLableMap = leadSchema.getDescribe().fields.getMap(); //3.全删全加 List soIds = new List(); Set columns = new Set(); for (SObject sobj : data.keySet()) { soIds.add(sobj.Id); for (String str : data.get(sobj)) { columns.add(str); } } //拼接待查询的字段 String columnStr = ''; if (columns.size()>0) { for (String str : columns) { columnStr += str +','; } } if (columnStr != '') { columnStr = columnStr.substring(0,columnStr.length() - 1); } //取得查找字段名 objName = objName.endsWith('__c')? objName : objName+'__c'; //3.1 获取所有存在的报告书状况统计对象 List delRpmObjList = new List(); String soql ='select id,Category__c,'+objName+' from ReportMemo__c where ' +objName + ' in :soIds'; List rmList = Database.query(soql); if (rmList != null && rmList.size() > 0) { for (SObject rep : rmList) { for (Sobject sobj : data.keySet()) { if (rep.get(objName) == sobj.Id) { for (String str : data.get(sobj)) { if (rep.get('Category__c') == apiLableMap.get(str).getDescribe().getLabel()) { delRpmObjList.add(rep); } } } } } } List delrpmList = (List)delRpmObjList; System.debug('delrpmList::'+delrpmList); if (delrpmList != null && delrpmList.size() > 0) { delete delrpmList; } //4.获取对象上的字段值 //创建集合 //key -> sobj对象 value: -> key :api对应的lable名称 value:内容集合 Map>> fieldMap = new Map>>(); for (SObject sobj : data.keySet()) { String key = ''; String value = ''; Map fieldsToValue = sobj.getPopulatedFieldsAsMap(); Map> sobjFieldMap = new Map>(); for (String fieldName : fieldsToValue.keySet()){ for (String apiName : data.get(sobj)) { if (fieldName == apiName) { System.debug('79::::apiName:'+apiName); value = (String) fieldsToValue.get(fieldName); key = apiLableMap.get(fieldName).getDescribe().getLabel(); sobjFieldMap.put(key, value.split(';')); } } } fieldMap.put(sobj, sobjFieldMap); } System.debug('fieldMap::'+fieldMap); //添加 List insrpmObjList = new List(); //key -> sobj对象 value: -> key :api对应的lable名称 value:内容集合 for (SObject sobj : fieldMap.keySet()) { Map> apiValMap = fieldMap.get(sobj); for (String category : apiValMap.keySet()) { for (String str : apiValMap.get(category)) { SObject rmc = new ReportMemo__c(); rmc.put('Category__c', category); rmc.put('Memo__c', str); rmc.put(objName, sobj.Id); insrpmObjList.add(rmc); } } } List insrpmList = (List)insrpmObjList; if (insrpmList != null && insrpmList.size() > 0) { insert insrpmList; } } }