• Pankaj Yadav 29
  • NEWBIE
  • 5 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 0
    Replies
Set<String> setname = new Set<String>();
     for(Opportunity opp : Trigger.New)
     {
         setname.add(opp.Name);
     }
    List<Opportunity> opplist = [SELECT ID, Name, StageName From Opportunity Where Name In:setname And (stageName='closed won' OR stageName='closed lost')];
    Map<String, Opportunity> mapopp = new Map<String, Opportunity>();
    Map<String, Opportunity> mapopen = new Map<String, Opportunity>();
    for(Opportunity opploop : opplist)
    {
       mapopp.put(opploop.Name, opploop);
       mapopen.put(opploop.StageName, opploop);
    }
    for(Opportunity oppnew : Trigger.New)
    {
        if(mapopp.containsKey(oppnew.Name) && mapopen.containsKey(oppnew.StageName))
        {
            oppnew.addError('same name with two opportunity');
        }
    }
This trigger is working but my doubt another way doing that type of question 
List<AggregateResult> agr = [Select 
        Sum(Amount), Calendar_Year(createdDate) From Account Where  isClosed=true And id=:userinfo.getuserId()  Group By Calendar_Year(createdDate)];
        Map<Integer, decimal> mapdetail = new Map<String, decimal>();
        for(AggregateResult a : agr)
        {
            mapdetail.put((Integer)a.get('expr1'), (Decimal) a.get('expr0'));
        }
        System.debug('sum of the year'+mapdetail);

 
trigger OpportunityOpen on Account(before delete)
{
        Set<Id> setacc = new Set<Id>();
        for(Account a : trigger.old)
        {
            setacc.add(a.Id);
        }

        List<Account> acclist = [Select Id, Name,
        (Select AccountId, Name, StageName From Opportunities)From Account Where Id In :setacc];
        for(Account a : acclist)
        {
            for(Opportunity opp : acclist.Opportunities)
            {
                if(opp.StageName=='closedwon')
                {
                    a.addError('you should not delete the account because a closedwon opportunity');
                }
            }
        }
}