You need to sign in to do that
Don't have an account?
farah sherif
Guys I need help urgently with this trigger PLEASE
We need a trigger on the asset object on update to check all assets.
If on ALL Assets belonging to product family = "360 Suite" had their Status = "Canceled" , then on Account, update the 360 Account stage (API Name = Account_Status__c ) to "Cancel".
please if anyone can help with this one I would be so grateful
If on ALL Assets belonging to product family = "360 Suite" had their Status = "Canceled" , then on Account, update the 360 Account stage (API Name = Account_Status__c ) to "Cancel".
please if anyone can help with this one I would be so grateful
Please find below pseudo code:
TRIGGER_ON_ASSET(AFTER_UPDATE) {
Set < AccountId > setOfAcctountIds = new Set < Id >();
for(Asset a : triggerNew) {
if(a.PRODUCT_FAMILY == "360 Suite")
setOfAcctountIds.add(a.accountId);
}
Map<ID,List<Asset>> mapOfAccoutnIdVsAsset = new Map<ID,List<Asset>>();
for(Asset ast :[SELECT Id,...........FROM Asset WHERE PRODUCT_FAMILY = "360....." AND ACCOUNTID IN :setOfAcctountIds]){
if(!mapOfAccoutnIdVsAsset.containskey(ast .AccoutnId))
mapOfAccoutnIdVsAsset.put(ast .AccoutnId,new List<ASSET>());
mapOfAccoutnIdVsAsset .get(ast .AccoutnId).add(ast);
}
List<Account> acctoupdate = new List <Account>();
for(Id accid : mapOfAccoutnIdVsAsset .keySet()){
Account ac = new Account(Id = accid );
boolean ntcancelledrecord = false;
for(Asset a : mapOfAccoutnIdVsAsset .get(accid )){
if(a.STATUS != 'CANCELLED')
{
ntcancelledrecord = true;
break;
}
}
if(!ntcancelledrecord ){
ac.STATUS="Cancel"
acctoupdate .add(ac);
}
}
if(!acctoupdate.isEmpty())
update acctoupdate;
Cheers!!!
Hoping below code would solve your problem...
trigger updateAccountStatusToCancel on Asset (after update) {
Set<Id> setAccountIds = new Set<Id>();
for(Asset as : trigger.new){
If(as.ProductFamily == '360 Suite' && as.Status == 'Canceled'){
setAccountIds.add(as.AccountId);
}
}
List<Account> lstAccounts = new List<Account>([SELECT id , Account_Status__c FROM Account WHERE Id IN: setAccountIds]);
for(Account acc : lstAccounts){
acc.Account_Status__c = 'Cancel';
}
update lstAccounts;
}
Regards
can I have your email for further questions?
for(Asset ast :[SELECT Id FROM Asset WHERE Product_Family__c = '360 Suite' AND AccountId IN :setOfAccountIds]){
if(!mapOfAccountIdVsAsset.containskey(ast .AccountId))
mapOfAccountIdVsAsset.put(ast.AccountId,new List<Asset>());
mapOfAccountIdVsAsset .get(ast.AccountId).add(ast);
}
Hi Farah
The explanation for statements:
1. for(Asset ast :[SELECT Id FROM Asset WHERE Product_Family__c = '360 Suite' AND AccountId IN :setOfAccountIds]) --> Query the Asset record which have product family as '360 Suite' and there Account Id contain in the set.
2. if(!mapOfAccountIdVsAsset.containskey(ast .AccountId))
mapOfAccountIdVsAsset.put(ast.AccountId,new List<Asset>());
mapOfAccountIdVsAsset .get(ast.AccountId).add(ast);
} --->
If the Map doesnt contain the Account Id and its Asset record then it is added to the map.
Cheers!!!!