public class NewAgencyOpportunityController { static string sobjectType = 'Agency_Opportunity__c'; @AuraEnabled public static ControllerResponse Init(string rid, string record_type_id){ system.debug('rid='+rid+',length='+(rid==null?'null':rid.length()+'')); system.debug('record_type_id='+record_type_id+',length='+(record_type_id==null?'null':record_type_id.length()+'')); ControllerResponse res = new ControllerResponse(); Map data = new Map(); res.Data = data; Agency_Opportunity__c ao = null; List layout = null; if(string.isBlank(rid)){ layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id, sobjectType); data.put('layout', Json.serialize(layout)); }else{ ao = [select RecordTypeId from Agency_Opportunity__c where id = :rid]; if(ao == null){ res.Message = 'id不存在'; return res; } record_type_id = ao.RecordTypeId; system.debug('record_type_id is fresh ='+ao.RecordTypeId); layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id, sobjectType); data.put('layout', Json.serialize(layout)); List fieldApiList = new List(); /* for (LayoutDescriberHelper.LayoutSection ls : layout.layoutSections) { for (LayoutDescriberHelper.LayoutField lf : ls.layoutFields) { if (lf.fieldAPI != '') { System.debug('lf.fieldAPI='+lf.fieldAPI+' fieldType='+lf.fieldType); fieldApiList.add(lf.fieldAPI); } } } */ for( Metadata.LayoutSection s: layout){ system.debug('s = ' + s); for( Metadata.LayoutColumn c: s.layoutColumns){ system.debug(c); if(c.layoutItems != null){ for( Metadata.layoutItem item: c.layoutItems){ system.debug(item); fieldApiList.add(item.field); } } } } system.debug('layout = ' + layout); system.debug(fieldApiList); ao = database.query(SoqlHelper.DistinctQueryFields('select id, Agency_Contact__r.AWS_Data_Id__c, ' + string.join(fieldApiList, ',') + ' from ' + sobjectType + ' where id = :rid')); System.debug('ao: ' + ao); System.debug('ao.Agency_Contact__r.AWS_Data_Id__c' + ao.Agency_Contact__r.AWS_Data_Id__c); data.put('data', ao); } data.put('fields', SObjectHelper.GetFieldInfos(sobjectType)); data.put('staticResource', Json.serialize(PIHelper.getPIIntegrationInfo('Agency_Contact__c'))); res.IsSuccess = true; return res; } @AuraEnabled public static ControllerResponse Save(Map data,string transId,Id recordTypeId){ system.debug('data='+data); system.debug(!data.containsKey('Id') ); system.debug( data.get('Id') == null); //NewAndEditBaseController.Response response = NewAndEditBaseController.save(new Agency_Contact__c(),Json.serialize(data),transId, !data.containsKey('Id') || data.get('Id') == null ); //ControllerResponse r = new ControllerResponse(); Sobject sobj = new Agency_Opportunity__c(); ControllerResponse r = SaveCore(sobj, data, transId,recordTypeId); if (r.IsSuccess) { r.Data = new Map{ 'recordId'=> sobj.Id }; } return r; } public static ControllerResponse SaveCore(Sobject sobj, Map data,string transId,Id recordTypeId ) { Integer index = 0; string sobjectTypeValue = sobj.getSObjectType().getDescribe().getName(); System.debug('sobjectTypeValue:'+sobjectTypeValue+' Info:' + JSON.serialize(data)); //1. Prepare the payload for opportunity Map fieldAPIToTypeMap = SobjectHelper.GetFieldMap(sobjectTypeValue); System.debug('fieldAPIToTypeMap = ' + fieldAPIToTypeMap); ControllerResponse r = new ControllerResponse(); //2. Save Record Process Savepoint sp = Database.setSavepoint(); try{ for(string fieldAPI : fieldAPIToTypeMap.keySet()){ if(data.containsKey(fieldAPI)){ Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType(); String fieldValue = String.valueOf(data.get(fieldAPI)); if(String.valueOf(fielddataType)=='DATE'){ sobj.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-'))); }else if(String.valueOf(fielddataType)=='DATETIME'){ if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){ fieldValue = fieldValue.replace('T',' '); sobj.put(fieldAPI, Datetime.valueOfGmt(fieldValue)); }else if(String.isNotBlank(fieldValue)) { fieldValue = fieldValue.replace('/', '-') + ':00'; sobj.put(fieldAPI, Datetime.valueOf(fieldValue)); }else{ sobj.put(fieldAPI, null); } }else if(String.valueof(fielddataType)=='CURRENCY'|| String.valueof(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){ sobj.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', ''))); }else { sobj.put(fieldAPI, fieldValue); } } } if (recordTypeId != null) { sobj.put('RecordTypeId',recordTypeId); } System.debug('sobj = ' + sobj); if(!Test.isRunningTest()){ upsert sobj; } //System.debug('respzhj = ' + resp); r.IsSuccess = true; return r; }catch(DmlException e) { System.debug(e.getNumDml()); System.debug(e.getDmlFields(index)); System.debug(e.getDmlId(index)); System.debug(e.getDmlIndex(index)); System.debug(e.getDmlMessage(index)); System.debug(e.getDmlStatusCode(index)); System.debug(e.getDmlType(index)); system.debug(e.getMessage()); system.debug(e.getStackTraceString()); System.debug('into catch'+e.getMessage()); Database.rollback(sp); r.IsSuccess = false; r.message ='保存失败,原因:'+ e.getDmlMessage(index); PIHelper.saveTransLog(sobjectTypeValue,'awsDataId',sobj.Id,transId, Json.serialize(data) ,'failed',r.message); return r; }catch(Exception e) { System.debug('into catch'+e.getMessage()); Database.rollback(sp); r.IsSuccess = false; r.message = e.getMessage()+e.getStackTraceString(); PIHelper.saveTransLog(sobjectTypeValue,'awsDataId',sobj.Id,transId, Json.serialize(data) ,'failed',r.message); return r; } } @AuraEnabled public static ControllerResponse getAWSDataIds(String agencyHospitalId){ ControllerResponse r = new ControllerResponse(); List conAWSIds = new List(); List agencyContact = new List([select Id,AWS_Data_Id__c from Agency_Contact__c where Agency_Hospital__c =:agencyHospitalId and AWS_Data_Id__c!='']); for(Agency_Contact__c ac : agencyContact){ conAWSIds.add(ac.AWS_Data_Id__c); } r.Data = conAWSIds; r.IsSuccess = true; return r; } @AuraEnabled public static ControllerResponse searchAgencyContacts(String awsAgencyContactIds,String accountId){ system.debug('awsAgencyContactIds = ' + awsAgencyContactIds); ControllerResponse resp = new ControllerResponse(); resp.IsSuccess = false; Map awsIdToContactMapTemp = new Map(); if(!checkNullString(awsAgencyContactIds)){ List awsDataIds = (List) JSON.deserialize(awsAgencyContactIds, List.class); List conListTemp = new List(); if(!checkNullString(accountId)){ Agency_Hospital_Link__c act = [select id from Agency_Hospital_Link__c where id = :accountId]; conListTemp = new List([select Id,AWS_Data_Id__c, Agency_Hospital__r.Name,Department_Class__r.Name from Agency_Contact__c where Agency_Hospital__c =: act.Id and AWS_Data_Id__c in:awsDataIds]); }else { conListTemp = new List([select Id,AWS_Data_Id__c,Agency_Hospital__r.Name,Department_Class__r.Name from Agency_Contact__c where AWS_Data_Id__c in:awsDataIds]); } for(Agency_Contact__c con:conListTemp){ awsIdToContactMapTemp.put(con.AWS_Data_Id__c,con); } } System.debug('awsIdToContactMapTemp = ' + awsIdToContactMapTemp); if(awsIdToContactMapTemp.keySet().size()>0){ resp.IsSuccess = true; resp.Data = JSON.serialize(awsIdToContactMapTemp);// PI contact info } return resp; } public static Boolean checkNullString(String inputString){ if(String.isEmpty(inputString)||String.isBlank(inputString)){ return true; } return false; } }