You need to sign in to do that
Don't have an account?
Tyler Ditto
Summary Rollup Trigger
I am new apex programming and am struggling a little bit. I have pieced together some code in an attempt to build a rollup from the Assets to Contracts.
The trigger is supposed to be able to get sum all of the assets' summary_asset_cost__c fields that are related to the contract and post it to the base_product_rev__c field on the contract. I am getting an error when trying to call the relationship in the second query. Also please notfiy if this is the best way to accomplish this goal, as stated before I am not real skilled in apex programming.
trigger AssetCostRollup on Asset (after delete, after insert, after update) {
Set<id> contractIds = new Set<id>();
List<Contract> contractsToUpdate = new List<Contract>();
for (Asset item : Trigger.new)
contractIds.add(item.Asset);
if (Trigger.isUpdate || Trigger.isDelete) {
for (Asset item : Trigger.old)
contractIds.add(item.Contract);
}
// get a map of the contracts and the costs of the assets on the contract
Map<id,Contract> contractMap = new Map<id,Contract>([select id, summary_asset_cost__c from Asset where id IN :contractIds]);
// query the contracts and the related assets and sum the cost of the assets to the contracts base_product_rev__c field
for (Contract contract : [select Id, Name, summary_asset_cost__c,(select id from contract_lookup__r) from Asset where Id IN :contractIds]) {
contractMap.get(contract.Id).asset_summary_cost__c = contract.base_product_rev__c.sum();
contractsToUpdate.add(contractMap.get(contract.Id));
}
update contractsToUpdate;
}
}
The trigger is supposed to be able to get sum all of the assets' summary_asset_cost__c fields that are related to the contract and post it to the base_product_rev__c field on the contract. I am getting an error when trying to call the relationship in the second query. Also please notfiy if this is the best way to accomplish this goal, as stated before I am not real skilled in apex programming.
trigger AssetCostRollup on Asset (after delete, after insert, after update) {
Set<id> contractIds = new Set<id>();
List<Contract> contractsToUpdate = new List<Contract>();
for (Asset item : Trigger.new)
contractIds.add(item.Asset);
if (Trigger.isUpdate || Trigger.isDelete) {
for (Asset item : Trigger.old)
contractIds.add(item.Contract);
}
// get a map of the contracts and the costs of the assets on the contract
Map<id,Contract> contractMap = new Map<id,Contract>([select id, summary_asset_cost__c from Asset where id IN :contractIds]);
// query the contracts and the related assets and sum the cost of the assets to the contracts base_product_rev__c field
for (Contract contract : [select Id, Name, summary_asset_cost__c,(select id from contract_lookup__r) from Asset where Id IN :contractIds]) {
contractMap.get(contract.Id).asset_summary_cost__c = contract.base_product_rev__c.sum();
contractsToUpdate.add(contractMap.get(contract.Id));
}
update contractsToUpdate;
}
}
Try the following code. Please update the "ContractField" with the API name of the contract field on your Asset object
this updated code should work for you.