我正在对象(Object1__c)上写一个触发器,它将分割插入Object1__c的单个行 分成多行并插入另一个对象,即Object2__c
基本上,对应于12个月(一年)数据的单行必须分成另一个对象的12个月数据行。
目前,我正在使用粗俗的分割方式,如下所示。
有没有有效的方法来实现这个目标?
Trigger trgSplit on Object1__c (After Insert){
List<Object2__c> object2List = new List<Object2__c>;
For(Object1__c nc : [select Account_Number__c, Account_Name__c, Y1M01__c,Y1M03__c,Y1M04__c,Y1M05__c,Y1M06__c,Y1M07__c,Y1M08__c,Y1M09__c,Y1M10__c,Y1M11,Y1M12__c]) {
Object2__c pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M01__c;
pc.Record_Date__c = date.parse('01/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M02__c;
pc.Record_Date__c = date.parse('02/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M03__c;
pc.Record_Date__c = date.parse('03/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M04__c;
pc.Record_Date__c = date.parse('04/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M05__c;
pc.Record_Date__c = date.parse('05/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M06__c;
pc.Record_Date__c = date.parse('06/01/2014');
object2List.add(pc);
.............................................
//and so on
//upto
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M11__c;
pc.Record_Date__c = date.parse('11/01/2014');
object2List.add(pc);
pc = new Object2__c();
pc.Account_Number__c = nc.Account_Number__c;
pc.Account_Name__c = nc.Account_Name__c;
pc.Total__c = nc.Y1M12__c;
pc.Record_Date__c = date.parse('12/01/2014');
object2List.add(pc);
}
insert object2List;
}
答案 0 :(得分:0)
您可以通过属性名称nc.Y1M01__c
直接获取sObject字段,或者通过底层sobject的get方法nc.get('Y1M01__c')
获取。由于您可以动态传递字符串,因此现在可以使用循环来摆脱重用代码:
List<Object2__c> object2List = new List<Object2__c>;
For(Object1__c nc : [select Account_Number__c, Account_Name__c, Y1M01__c,Y1M03__c,Y1M04__c,Y1M05__c,Y1M06__c,Y1M07__c,Y1M08__c,Y1M09__c,Y1M10__c,Y1M11,Y1M12__c]) {
//loop thru each month, 1...12
for(Integer i = 1; i < 13; i++) {
//get the month and prefix a zero if necessary
String month = String.valueOf(i);
month = (month.length() == 1 ? '0' : '') + month;
//build the date
Date recDate = Date.parse(String.format('{0}/01/2014', new List<String> { month }));
//get the field api name
String y1mXX = String.format('Y1M{0}__c', new List<String> { month }));
//get the field value, you will need to cast it to the appropriate data type
Decimal y1mXXval = Decimal.valueOf( String.valueOf( nc.get(y01mXX) ) );
//since you are not using the new Object2__c anymore just pass it directly into the list
object2List.add(new Object2__c(
Account_Number__c = nc.Account_Number__c,
Account_Name__c = nc.Account_Name__c,
Total__c = y1mXXval,
Record_Date__c = recDate
));
}
}
insert object2List;