问题描述
我正在编写一个invocablemethod批处理顶点类,以将“ Order Package”对象上的多个货币字段汇总到其“ Order Location”的父对象。我以为我已正确添加了所有内容,但似乎从Package_Monthly_Recurring_Fees__c字段中将“ Package_Monthly_Recurring_Fees__c”和“ Package_One_Time_Fees__c”更新为相同的价格。有人可以帮助我确定为什么两个字段都使用相同的价格吗?任何帮助深表感谢!
global class OrderLocationRollupSummary implements Database.Batchable<sObject>,Schedulable {
//invocable Method
@invocableMethod(label='Rollup All Order Packages to Locations')
global static void rollupAllorderpackages(List<Order_Location_Package__c> orderpackages) {
rollupOrderPackages(orderpackages);
}
//Batchable Methods
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT Id FROM Order_New_Location__c]);
}
global void execute(Database.BatchableContext context,List<sObject> batch){
Set<Id> OrderLocationIds = new Set<Id>();
for (sObject ordloc : batch) {
OrderLocationIds.add(ordloc.Id);
}
summarizeOrderPackages(OrderLocationIds);
}
global void finish(Database.BatchableContext context) {}
//Schedulable Methods
global void execute(SchedulableContext context){
OrderLocationRollupSummary batchJob = new OrderLocationRollupSummary();
Database.executeBatch(batchJob);
}
//Static Methods
public static void rollupOrderPackages(List<Order_Location_Package__c> orderpackages) {
Set<Id> OrderLocationIds = new Set<Id>();
//Get Order Location Ids from specified orderpackages
for (Order_Location_Package__c ordpckg : orderpackages) {
OrderLocationIds.add(ordpckg.New_Location_Name__c);
}
if (OrderLocationIds.isEmpty() == false) {
/*Execute as a future call so that the user doesn't have to wait around for
the rollup to finish. Unless,already in a future or batch call state then
just perform the rollup.*/
if (System.isFuture() == false && System.isBatch() == false) {
summarizeOrderPackagesAsync(OrderLocationIds);
}
else {
new OrderLocationRollupSummary().summarizeOrderPackages(OrderLocationIds);
}
}
}
@future
public static void summarizeOrderPackagesAsync(Set<Id> OrderLocationIds) {
new OrderLocationRollupSummary().summarizeOrderPackages(OrderLocationIds);
}
//Public Methods
public void summarizeOrderPackages(Set<Id> OrderLocationIds) {
//Get Order Locations to Update
List<Order_New_Location__c> orderlocations = queryOrderLocationsById(OrderLocationIds);
Map<Id,double> results = getorderPackagesAmountsByLocationId(OrderLocationIds);
//Loop Order Locations and set Amounts
List<Order_New_Location__c> orderlocationsToUpdate = new List<Order_New_Location__c>();
for (Order_New_Location__c ordloc : orderlocations) {
double mrf = 0;
double otf = 0;
if (results.containsKey(ordloc.Id)) {
mrf = results.get(ordloc.Id);
otf = results.get(ordloc.Id);
}
//Determine if Amounts have Changed
if (ordloc.Package_Monthly_Recurring_Fees__c != mrf ||
ordloc.Package_One_Time_Fees__c != otf) {
ordloc.Package_Monthly_Recurring_Fees__c = mrf;
ordloc.Package_One_Time_Fees__c = otf;
orderlocationsToUpdate.add(ordloc); //Add location to collection to be updated
}
}
if(orderlocationsToUpdate.isEmpty() == false) {
Database.SaveResult[] saveResults = Database.update(orderlocationsToUpdate,false);
System.debug(saveResults);
}
}
//Private Methods
public Map<Id,double> getorderPackagesAmountsByLocationId(Set<Id> OrderLocationIds) {
Map<Id,double> resultsByOrderLocationId = new Map<Id,double>();
//Summarize Order Package Amounts by Order Location Id
AggregateResult[] results = aggregateOrderPackageAmounts(OrderLocationIds);
for (AggregateResult result : results) {
Id orderlocationId = (Id) result.get('OrderLocation');
double mrf = (double) result.get('MRFees');
double otf = (double) result.get('OTFees');
resultsByOrderLocationId.put(orderlocationId,mrf);
resultsByOrderLocationId.put(orderlocationId,otf);
}
return resultsByOrderLocationId;
}
//Query Methods
private List<Order_New_Location__c> queryOrderLocationsById(Set<Id> OrderLocationIds) {
return [SELECT
Id,Package_Monthly_Recurring_Fees__c,Package_One_Time_Fees__c
FROM
Order_New_Location__c
WHERE
Id IN :OrderLocationIds];
}
private AggregateResult[] aggregateOrderPackageAmounts(Set<Id> OrderLocationIds) {
return [SELECT
New_Location_Name__c OrderLocation,SUM(Monthly_Recurring_Fees__c) MRFees,SUM(One_Time_Fees__c) OTFees
FROM
Order_Location_Package__c
WHERE
New_Location_Name__c IN :OrderLocationIds
GROUP BY
New_Location_Name__c];
}
}
解决方法
在summaryOrderPackages方法中,以下代码将'mrf'和'oft'变量设置为相同的值。
if (results.containsKey(ordloc.Id)) {
mrf = results.get(ordloc.Id);
otf = results.get(ordloc.Id);
}
然后,您要将有问题的字段设置为mrf和otf变量(具有相同的值)。
//Determine if Amounts have Changed
if (ordloc.Package_Monthly_Recurring_Fees__c != mrf ||
ordloc.Package_One_Time_Fees__c != otf) {
ordloc.Package_Monthly_Recurring_Fees__c = mrf;
ordloc.Package_One_Time_Fees__c = otf;
orderlocationsToUpdate.add(ordloc); //Add location to collection to be updated
}
考虑在您的OrderLocationRollupSummary类中添加包装器和内部类以解决此问题:
public class Order_New_Location_Wrapper {
Id orderlocationId = {get;set;}
Double mrf = {get;set;}
Double otf = {get;set;}
public class Order_New_Location_Wrapper (Id arg1,Double arg2,Double arg3)
orderlocationId = arg1;
mrf = arg2;
otf = arg3;
}
然后在您的getOrderPackagesAmountsByLocationId方法中调用此新内部类:
public Map<Id,Order_New_Location_Wrapper> getOrderPackagesAmountsByLocationId(Set<Id> OrderLocationIds) {
//create a list of orderlocations to represent the collection objects coming from the wrapper
resultsByOrderLocationId = new Map<Id,Order_New_Location_Wrapper>();
//Summarize Order Package Amounts by Order Location Id
AggregateResult[] results = aggregateOrderPackageAmounts(OrderLocationIds);
for (AggregateResult result : results) {
Id orderlocationId = (Id) result.get('OrderLocation');
double mrf = (double) result.get('MRFees');
double otf = (double) result.get('OTFees');
resultsByOrderLocationId.add(orderlocationId,new OpportunityWrapper(orderlocationId,mrg,otf));
}
return resultsByOrderLocationId;
}
您还需要将自己的summaryOrderPackages方法更新为类似以下内容:
public void summarizeOrderPackages(Set<Id> OrderLocationIds) {
//Get Order Locations to Update
List<Order_New_Location__c> orderlocations = queryOrderLocationsById(OrderLocationIds);
Map<Id,Order_New_Location_Wrapper> results = getOrderPackagesAmountsByLocationId(OrderLocationIds);
//Loop Order Locations and set Amounts
List<Order_New_Location__c> orderlocationsToUpdate = new List<Order_New_Location__c>();
for (Order_New_Location__c ordloc : orderlocations) {
double mrf = 0;
double otf = 0;
if (results.containsKey(ordloc.Id)) {
mrf = (Order_New_Location__c) results.get(ordloc.Id).mrf;
otf = (Order_New_Location__c) results.get(ordloc.Id).otf;
}
//Determine if Amounts have Changed
if (ordloc.Package_Monthly_Recurring_Fees__c != mrf ||
ordloc.Package_One_Time_Fees__c != otf) {
ordloc.Package_Monthly_Recurring_Fees__c = mrf;
ordloc.Package_One_Time_Fees__c = otf;
orderlocationsToUpdate.add(ordloc); //Add location to collection to be updated
}
}
if(orderlocationsToUpdate.isEmpty() == false) {
Database.SaveResult[] saveResults = Database.update(orderlocationsToUpdate,false);
System.debug(saveResults);
}
}
,
仅通过更新一些方法就可以解决此问题,请参见下文。再次感谢您的帮助,罗伯特!
global class OrderLocationRollupSummary implements Database.Batchable<sObject>,Schedulable {
//Invocable Method
@InvocableMethod(label='Rollup All Order Packages to Locations')
global static void rollupAllorderpackages(List<Order_Location_Package__c> orderpackages) {
rollupOrderPackages(orderpackages);
}
//Batchable Methods
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT Id FROM Order_New_Location__c]);
}
global void execute(Database.BatchableContext context,List<sObject> batch){
Set<Id> OrderLocationIds = new Set<Id>();
for (sObject ordloc : batch) {
OrderLocationIds.add(ordloc.Id);
}
summarizeOrderPackages(OrderLocationIds);
}
global void finish(Database.BatchableContext context) {}
//Schedulable Methods
global void execute(SchedulableContext context){
OrderLocationRollupSummary batchJob = new OrderLocationRollupSummary();
Database.executeBatch(batchJob);
}
//Static Methods
public static void rollupOrderPackages(List<Order_Location_Package__c> orderpackages) {
Set<Id> OrderLocationIds = new Set<Id>();
//Get Order Location Ids from specified orderpackages
for (Order_Location_Package__c ordpckg : orderpackages) {
OrderLocationIds.add(ordpckg.New_Location_Name__c);
}
if (OrderLocationIds.isEmpty() == false) {
/*Execute as a future call so that the user doesn't have to wait around for
the rollup to finish. Unless,already in a future or batch call state then
just perform the rollup.*/
if (System.isFuture() == false && System.isBatch() == false) {
summarizeOrderPackagesAsync(OrderLocationIds);
}
else {
new OrderLocationRollupSummary().summarizeOrderPackages(OrderLocationIds);
}
}
}
@future
public static void summarizeOrderPackagesAsync(Set<Id> OrderLocationIds) {
new OrderLocationRollupSummary().summarizeOrderPackages(OrderLocationIds);
}
//Public Methods
public void summarizeOrderPackages(Set<Id> OrderLocationIds) {
//Get Order Locations to Update
List<Order_New_Location__c> orderlocations = queryOrderLocationsById(OrderLocationIds);
Map<Id,AggregateResult> results = getOrderPackagesAmountsByLocationId(OrderLocationIds);
//Loop Order Locations and set Amounts
List<Order_New_Location__c> orderlocationsToUpdate = new List<Order_New_Location__c>();
for (Order_New_Location__c ordloc : orderlocations) {
double mrf = 0;
double otf = 0;
if (results.containsKey(ordloc.Id)) {
AggregateResult ar = results.get(ordloc.Id);
mrf = (double)ar.get('MRFees');
otf = (double)ar.get('OTFees');
}
//Determine if Amounts have Changed
if (ordloc.Package_Monthly_Recurring_Fees__c != mrf ||
ordloc.Package_One_Time_Fees__c != otf) {
ordloc.Package_Monthly_Recurring_Fees__c = mrf;
ordloc.Package_One_Time_Fees__c = otf;
orderlocationsToUpdate.add(ordloc); //Add location to collection to be updated
}
}
if(orderlocationsToUpdate.isEmpty() == false) {
Database.SaveResult[] saveResults = Database.update(orderlocationsToUpdate,false);
System.debug(saveResults);
}
}
//Private Methods
public Map<Id,AggregateResult> getOrderPackagesAmountsByLocationId(Set<Id> OrderLocationIds) {
Map<id,AggregateResult> resultsByOrderLocationId= new Map<Id,AggregateResult>();
//Summarize Order Package Amounts by Order Location Id
AggregateResult[] results = aggregateOrderPackageAmounts(OrderLocationIds);
for (AggregateResult result : results) {
Id orderlocationId = (Id) result.get('OrderLocation');
resultsByOrderLocationId.put(orderlocationId,result );
}
return resultsByOrderLocationId;
}
//Query Methods
private List<Order_New_Location__c> queryOrderLocationsById(Set<Id> OrderLocationIds) {
return [SELECT
Id,Package_Monthly_Recurring_Fees__c,Package_One_Time_Fees__c
FROM
Order_New_Location__c
WHERE
Id IN :OrderLocationIds];
}
private AggregateResult[] aggregateOrderPackageAmounts(Set<Id> OrderLocationIds) {
return [SELECT
New_Location_Name__c OrderLocation,SUM(Monthly_Recurring_Fees__c) MRFees,SUM(One_Time_Fees__c) OTFees
FROM
Order_Location_Package__c
WHERE
New_Location_Name__c IN :OrderLocationIds
GROUP BY
New_Location_Name__c];
}
}