global without sharing class NewAndEditReportController { public List layoutSections{set;get;} public static Map schemaMap = Schema.getGlobalDescribe(); public static String sobjectTypeValue = 'Report__c'; public Boolean isNewMode{set;get;} public String rtTypeId {get; set;} public String AWSDataId{set;get;} public String contactId{set;get;}//For Lookup field public List encryptedAPIList{set;get;} public String staticResource {get; set;} public String staticResourceContact {get; set;} public String requiredFieldAPIListStr {get; set;} public String fieldAPIToLabelMapStr {get; set;} public String requiredErrorMsg{set;get;} public String contactsInfo {set;get;} public String PIPL_Input_Account_Error_Msg{set;get;} public String SaveAndNewButtonUrl{get;private set;} public String sobjectPrefix{get;private set;} public String sobjecttypeForFrontEnd{set;get;} public String sobjectId{set;get;} public NewAndEditReportController(ApexPages.StandardController controller) { sobjectId = [SELECT CustomObjectId,CustomObjectName FROM CustomObjectUserLicenseMetrics where CustomObjectName ='Report' limit 1].CustomObjectId; isNewMode = true; requiredErrorMsg = Label.Input_Required_Field_Msg; PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg; sobjecttypeForFrontEnd = sobjectTypeValue; //获取所有字段 List fieldList = new List(Schema.getGlobalDescribe().get('Report__c').getDescribe().fields.getMap().keyset()); // Add fields to controller. This is to avoid the SOQL error in visualforce page controller.addFields(fieldList); SObject obj = controller.getRecord(); if(obj.Id != null){ //更新 isNewMode = false; Report__c ReportData = [select Id,RecordTypeId,AWS_Data_Id__c,Practitioner1__c,Practitioner1__r.AWS_Data_Id__c, Practitioner2__c,Practitioner2__r.AWS_Data_Id__c,Practitioner3__c,Practitioner3__r.AWS_Data_Id__c, Practitioner4__c,Practitioner4__r.AWS_Data_Id__c,Practitioner5__c,Practitioner5__r.AWS_Data_Id__c, Person_In_Charge__c,Person_In_Charge__r.AWS_Data_Id__c from Report__c where id =: obj.Id]; system.debug('ReportData = ' + ReportData); rtTypeId = ReportData.RecordTypeId; AWSDataId = ReportData.AWS_Data_Id__c; Map sfIdToAWSIdMap = new Map(); if(ReportData.Practitioner1__r.Id != null && ReportData.Practitioner1__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Practitioner1__r.Id).subString(0,15),ReportData.Practitioner1__r.AWS_Data_Id__c); } if(ReportData.Practitioner2__r.Id != null && ReportData.Practitioner2__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Practitioner2__r.Id).subString(0,15),ReportData.Practitioner2__r.AWS_Data_Id__c); } if(ReportData.Practitioner3__r.Id != null && ReportData.Practitioner3__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Practitioner3__r.Id).subString(0,15),ReportData.Practitioner3__r.AWS_Data_Id__c); } if(ReportData.Practitioner4__r.Id != null && ReportData.Practitioner4__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Practitioner4__r.Id).subString(0,15),ReportData.Practitioner4__r.AWS_Data_Id__c); } if(ReportData.Practitioner5__r.Id != null && ReportData.Practitioner5__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Practitioner5__r.Id).subString(0,15),ReportData.Practitioner5__r.AWS_Data_Id__c); } if(ReportData.Person_In_Charge__r.Id != null && ReportData.Person_In_Charge__r.AWS_Data_Id__c!=null){ sfIdToAWSIdMap.put(String.valueof(ReportData.Person_In_Charge__r.Id).subString(0,15),ReportData.Person_In_Charge__r.AWS_Data_Id__c); } contactsInfo = JSON.serialize(sfIdToAWSIdMap); }else{ //新建 rtTypeId = ApexPages.currentPage().getParameters().get('RecordType'); obj.put('OwnerId',UserInfo.getUserId()); } LayoutDescriberHelper.LayoutWrapper LayoutWrapperValue = LayoutDescriberHelper.describeSectionWithFieldsWrapper(rtTypeId, 'Report__c','classic'); layoutSections = LayoutWrapperValue.layoutSections; List requiredFieldAPIList = LayoutWrapperValue.requiredFieldAPIList; Map fieldAPIToLabelMap = LayoutWrapperValue.fieldAPIToLabelMap; requiredFieldAPIListStr = JSON.serialize(requiredFieldAPIList); fieldAPIToLabelMapStr = JSON.serialize(fieldAPIToLabelMap); PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Report__c'); staticResource = JSON.serialize(piIntegration); encryptedAPIList = piIntegration.PIFields; staticResourceContact = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact')); sobjectPrefix = piIntegration.sobjectPrefix; } global class Response{ public String recordId{set;get;} public String message{set;get;} public String status{set;get;} } @RemoteAction global static Response saveReport(String reportJson,String transId,Boolean isNew) { System.debug('report Info:' + JSON.serialize(reportJson)); //System.debug('rtTypeId: ' + rtTypeId); //1. Prepare the payload for report Schema.SObjectType reportSchema = schemaMap.get(sobjectTypeValue); Map fieldAPIToTypeMap = reportSchema.getDescribe().fields.getMap(); Map fieldValueMap = (Map)JSON.deserializeUntyped(reportJson); Report__c reportInfo = new Report__c(); System.debug('自定义格式转换开始'); //自定义格式转换 for (String fieldAPI: fieldValueMap.keySet()) { System.debug('field API'+fieldAPI); Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType(); String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI)); if(String.isBlank(fieldValue)){ continue; } if(String.valueOf(fielddataType)=='DATE'){ reportInfo.put(fieldAPI, Date.valueOf(String.valueOf(fieldValueMap.get(fieldAPI)).replace('/', '-'))); }else if(String.valueOf(fielddataType)=='DATETIME'){ String dt = String.valueOf(fieldValueMap.get(fieldAPI)); if(String.isNotBlank(dt)&&dt.contains('T')){ dt = dt.replace('T',' '); reportInfo.put(fieldAPI, Datetime.valueOfGmt(dt)); } }else if(String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){ reportInfo.put(fieldAPI, Decimal.valueOf(String.valueOf(fieldValueMap.get(fieldAPI)))); } else if(String.valueof(fielddataType)=='BOOLEAN'){ reportInfo.put(fieldAPI, fieldValueMap.get(fieldAPI)); }else { reportInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI))); } } System.debug('自定义格式转换结束'); //2. Save Record Process String status = 'success'; Response resp = new Response(); Savepoint sp = Database.setSavepoint(); String rid = ''; try{ if(isNew){ System.debug('reportInfo = ' + reportInfo); insert reportInfo; }else{ System.debug('into update'); String awsDataId = (String)reportInfo.get('AWS_Data_Id__c'); System.debug('awsDataId = ' + awsDataId); report__c[] reports = [select id from report__c where AWS_Data_Id__c =:awsDataId]; System.debug('reports[0].id = ' + reports[0].id); reportInfo.put('Id',reports[0].id);//For testing; update reportInfo; } rid=reportInfo.Id; PIHelper.saveTransLog(sobjectTypeValue,(String)reportInfo.get('AWS_Data_Id__c'),rid,transId,reportJson ,status,''); resp.recordId = reportInfo.Id; // resp.message = 'success savereport'; resp.status = status; return resp; } catch(Exception e) { System.debug('into catch'+e.getMessage()); Database.rollback(sp); status = 'fail'; PIHelper.saveTransLog(sobjectTypeValue,(String)reportInfo.get('AWS_Data_Id__c'),rid,transId,reportJson,status,e.getMessage()); resp.message = e.getMessage(); resp.status = status; return resp; } } }