• Mohd. Kamran
  • NEWBIE
  • 30 Points
  • Member since 2018


  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hey guys,

I need help with the code below. It work, but when the second for is running I`m afraid that it cause a system error because it's runing many times the same consult. What I need to do is change this to use a Map<key,value> and stay with only one for and a Map<key,value>. How I do this?
 
public static void stopAccountDelete (List<Account> listAcc){

        try{
            for(Account acc : listAcc){
                for(Opportunity opp : [SELECT AccountId FROM Opportunity WHERE IsWon = true]){
                    if(acc.Id == opp.AccountId){
                        acc.Id.adderror('Account cannot be deleted, because exists a Opportunity associated!');
                    }
                }
            }
        }

        catch (DmlException e) {
            System.debug('DML error: ' + e.getMessage());
        }

        catch (Exception ex) {
            System.debug('System error: ' + ex.getMessage());
        }

    }
Thanks,
Junior, Jonas C.
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
Hey guys,

I need help with the code below. It work, but when the second for is running I`m afraid that it cause a system error because it's runing many times the same consult. What I need to do is change this to use a Map<key,value> and stay with only one for and a Map<key,value>. How I do this?
 
public static void stopAccountDelete (List<Account> listAcc){

        try{
            for(Account acc : listAcc){
                for(Opportunity opp : [SELECT AccountId FROM Opportunity WHERE IsWon = true]){
                    if(acc.Id == opp.AccountId){
                        acc.Id.adderror('Account cannot be deleted, because exists a Opportunity associated!');
                    }
                }
            }
        }

        catch (DmlException e) {
            System.debug('DML error: ' + e.getMessage());
        }

        catch (Exception ex) {
            System.debug('System error: ' + ex.getMessage());
        }

    }
Thanks,
Junior, Jonas C.