You need to sign in to do that
Don't have an account?
farah sherif
i need help with this trigger because it's giving me an error
the trigger should be on account to check the assets and if ALL the Assets belonging to product family = "bla bla " had their status "Canceled" the trigger should update bla bla Account stage (API Name = Account_Status__c ) to "Cancel".
below is a trigger I wrote but it doesn't work
trigger cancelAsset on Account (after update) {
List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite'];
List<Asset> tech = [Select Id ,Status from Asset where Product_Family__c = 'Ad Tech'];
for(Account acc:Trigger.new){
Integer z=0;
for(Integer i=0 ; i<= suite.size() ;i++){
if(suite[i].Status == 'Canceled'){
z++;
}
}
if (z == suite.size()){
acc.Account_Status__c = 'Cancel';
}
update acc;
}
}
below is a trigger I wrote but it doesn't work
trigger cancelAsset on Account (after update) {
List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite'];
List<Asset> tech = [Select Id ,Status from Asset where Product_Family__c = 'Ad Tech'];
for(Account acc:Trigger.new){
Integer z=0;
for(Integer i=0 ; i<= suite.size() ;i++){
if(suite[i].Status == 'Canceled'){
z++;
}
}
if (z == suite.size()){
acc.Account_Status__c = 'Cancel';
}
update acc;
}
}
You are updating the same account record so you need to implement your code on before update and skip this external update statement.
Cheers!!!
The logic should be bulkied and follow the best practices.
Try the below code:
trigger cancelAsset on Account (before update) {
//List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite']; // This query will be retrieve all asset records from database. So we should use filters and optimise the query by getting assets only related to accounts being processed
List<Asset> suite = new List<Asset>();
set<ID> accIds = new set<Id>();
map<Id,Boolean> accIdStatusMap = new map<Id,Boolean>();
for(Account acc:Trigger.new){
accIds.add(acc.Id);
}
if(accIds != null && accIds.size() > 0){
suite = [select Id ,Status from Asset where Product_Family__c = '360 Suite' and AccountID in: accIds];
}
if(suite != null && suite.size() > 0){
for(Asset assetRec : suite){
if(assetRec.AccountId != null){
accIdStatusMap.put(assetRec.AccountId,true);
}
if(assetRec.Status != 'Canceled'){
accIdStatusMap.put(assetRec.AccountId,false);
}
else if(assetRec.Status == 'Canceled' && accIdStatusMap.conatinsKey(assetRec.AccountId) && accIdStatusMap.get(assetRec.AccountId)){
accIdStatusMap.put(assetRec.AccountId,true);
}
}
}
for(Account acc:Trigger.new){
if(accIdStatusMap.conatinsKey(acc.Id)){
acc.Account_Status__c = 'Cancel';
}
}
}
your code is giving me this error
Method does not exist or incorrect signature: void conatinsKey(Id) from the type Map<Id,Boolean>
It is incorrectly spelt as conatinskey.
It should be containsKey
dlrs_AssetTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0011900000epHTZAA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cancelAsset: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Asset.AccountId Trigger.cancelAsset: line 20, column 1: [] Class.dlrs.RollupService.Updater.updateRecords: line 1315, column 1 Class.dlrs.RollupService.UpdateWithoutSharing.updateRecords: line 1358, column 1 Class.dlrs.RollupService.updateRecords: line 1286, column 1 Class.dlrs.RollupService.handleRollups: line 892, column 1 Class.dlrs.RollupService.triggerHandler: line 311, column 1 Trigger.dlrs_AssetTrigger: line 7, column 1
The error that you are encountering is due to package trigger.
You need to investigate the issue related to package trigger and try to deactivate the package trigger and see
if(assetRec.AccountId != null){
accIdStatusMap.put(assetRec.AccountId,true);
}
what does this line of code do
you Should update your code as follow
Thanks,