• PRADEEP YADAV 3
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 9
    Replies
trigger TriggerNew1Problem on Account(after insert)
{
    Set<String> setacc = new Set<String>();
    for(Account a : Trigger.new)
    {
        setacc.add(a.Name);
    }
    
    List<Account> acclist = [Select Id, Name, (Select Id, Name From Opportunities)
    From Account Where Name In : setacc
    ];
    List<Opportunity> opplist = new List<Opportunity>();
    
    Map<String, Account> nmap = new Map<String, Account>();
    for(Account a : acclist)
    {
        nmap.put(a.Name, a);
    }
    
    for(Account acc : Trigger.new)
    {
        if(nmap.get(acc.Name)!=null)
        {
            opplist.add(new Opportunity(AccountId =acc.Id, Name ='Duplicate Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));         
        }
        else if(nmap.get(acc.Name)==null)
        {
             opplist.add(new Opportunity(AccountId =acc.Id, Name ='First Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));
        }
    }
    if(opplist.size()>0)
    {
        insert opplist;
    }
}
when i inserted duplicate name in account then print duplicate opportunity name on the bases of account if not a duplicate account name then print first opportunity
But First Opportunity is not printed
When an account is inserted or updated,check whether any opportunity is linked to it or not,if not then create one whose name is ‘First Opportunity -<Account Name>’
When an account is inserted or updated,check whether any opportunity is linked to it or not,if not then create one whose name is ‘First Opportunity -<Account Name>’
trigger TriggerNew1Problem on Account(after insert)
{
    Set<String> setacc = new Set<String>();
    for(Account a : Trigger.new)
    {
        setacc.add(a.Name);
    }
    
    List<Account> acclist = [Select Id, Name, (Select Id, Name From Opportunities)
    From Account Where Name In : setacc
    ];
    List<Opportunity> opplist = new List<Opportunity>();
    
    Map<String, Account> nmap = new Map<String, Account>();
    for(Account a : acclist)
    {
        nmap.put(a.Name, a);
    }
    
    for(Account acc : Trigger.new)
    {
        if(nmap.get(acc.Name)!=null)
        {
            opplist.add(new Opportunity(AccountId =acc.Id, Name ='Duplicate Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));         
        }
        else if(nmap.get(acc.Name)==null)
        {
             opplist.add(new Opportunity(AccountId =acc.Id, Name ='First Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));
        }
    }
    if(opplist.size()>0)
    {
        insert opplist;
    }
}
when i inserted duplicate name in account then print duplicate opportunity name on the bases of account if not a duplicate account name then print first opportunity
But First Opportunity is not printed
When an account is inserted or updated,check whether any opportunity is linked to it or not,if not then create one whose name is ‘First Opportunity -<Account Name>’