• PRADEEP YADAV 5
  • NEWBIE
  • 49 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 13
    Questions
  • 23
    Replies
trigger OpportunityOpen on Account(before delete)
{
    Map<Id, Account> mapacc = new Map<Id, Account>( [Select Id, Name, AnnualRevenue,
                            (Select AccountId, StageName From Opportunities)
                            From Account Where Id In : trigger.old]);
    for(Account a : trigger.old)
    {
        for(Opportunity opp : mapacc.get(a.Id).Opportunities)
        {
            if(opp.stageName=='closed won')
            {
                a.addError('associated open opportunities with account');
            }
        }
    }
}
When an opportunity is inserted check for it’s duplicacy on the basis of its name and the account to which it is related to i.e. If it has same name and it’s linked to same Account then append “Duplicate Opportunity” in the name
trigger Trigger1Problem on Account(after insert, after update) 
{
    List<Opportunity> opp = new List<Opportunity>();
    
    Map<ID, Account> accountmap = new Map<ID, Account>([Select Id, Name, (Select Id From Opportunities) From Account Where Id In :Trigger.New]);
    for(Account a : trigger.new)
    {
        if(accountmap.get(a.Id).Opportunities.size() == 0)
        {
            opp.add(new Opportunity(AccountId = a.Id, Name ='First Opportunity'+a.Name, StageName ='prospecting',CloseDate =System.today()));
        }

    }
    if(opp.size()>0)
    {
        Insert opp;
    }
}
Helper Class Which Way Doing
trigger Trigger1Problem on Account (after Insert, after Update)
{
    if(Trigger.isAfter)
    {
            if(Trigger.isInsert)
            {
                List<Opportunity>opp = new List<Opportunity>();
    
                Map<Id, Account>mapacc = new Map<Id, Account>
                ([Select Id, Name,(Select Id, Name From Opportunities) From Account Where Id In :trigger.new]);
    
            for(Account acc : trigger.new)
                {
                    if(mapacc.get(acc.Id).Opportunities.size() == 0)
                        {
                            opp.add(new Opportunity(AccountId =acc.Name, Name ='FirstOpportunity'+acc.Name, StageName = 'prospecting'
                            , CloseDate = System.today()));
                        }
                }
                insert opp;

            }
            else
            {
                if(Trigger.isUpdate)
                {
                    Map<Id, Account> nMap = new Map<Id, Account>();
                    
                    nMap = Trigger.newMap;
                    
                    List<Opportunity>opp = [Select Id, Name From Opportunity Where
                    Id In : nMap.keySet()];
                    
                    for(Opportunity oppup : opp)
                    {
                        Account a = nMap.get(oppup.Id);
                        
                        oppup.Name = a.Name;
                    }
                    update opp;
                }
            }
    
    }
    
}
no error but not inserted or not updated record doing this question 
1) context variable 
2) Helper class
whenever lead get saved with web then automatically convert it
trigger LeadConvert on Lead(after Insert, after Update)
    {
        List<String>LeadName = new List<String>();
        for(Lead myLead : trigger.new)
        {
            if(myLead.isconverted ==false && myLead.LeadSource == 'web')
            {
                Database.LeadConvert lc = new Database.LeadConvert();
                lc.setLeadId(myLead.Id);
                lc.convertedStatus = 'Closing-Converted';
                lc.setDoNotCreateOpportunity(true);
                Database.LeadConvertResult lcr = Database.convertLead(lc);
                System.assert(lcr.isSuccess());
            }   
        }
 }
try but lead not converted then how to converted automatically
trigger UpdateRecordMap on Update(after Update)
{
  Set<Id>accid = new Set<Id>();
    for(Account a : trigger.new)
    {
          accid.add(a.Id);
    }
    List<Contact>listcon = new List<Contact>();
    Map<Id, Account>mapacc = new Map<Id, Account>
    ([Select Id, BillingCity,
                            (Select Id, MailingState From Contacts) 
                    From Account Where Id In :accid]);
   for(Account a : trigger.new)
    {
             Contact con = new Contact();
             con.MailingState = a.BillingCity;
             listcon.add(con);
    }
    if(listcon.size()>0)
    {
        update listcon;
    }

}
Not Update the Record
trigger InsertBefore on Account (after insert) 
{
    List<Contact>contdetail = new List<Contact>();
  
  Map<Id, Account>mapaccount = new Map<Id, Account>([Select Id, name, phone, 
                              (Select Id,FirstName, LastName, phone From Contacts
                              )From Account Where Id In : Trigger.New]);
                              
  for(Account a : Trigger.New)
  {
         if(mapaccount.get(a.Id).contacts.size() == 0)
         {
             contdetail.add(new Contact(FirstName =a.Name, LastName =a.Name, Phone =a.phone, AccountId =a.Id));
         }
  }
}
trigger OpportunityBackup on Opportunity (after update) 
{

      List<My_Backup__c> lstToInsrt = new List<My_Backup__c>();      
    for(Opportunity op1 : Trigger.new)
    {
         if(trigger.oldMap.get(op1.Id).Name !=op1.Name)
         {
             My_Backup__c backup = new My_Backup__c();
             backup.Name = trigger.oldMap.get(op1.AccountId).Name;
             lstToInsrt.add(backup);
         }
    }
    if(lstToInsrt.size()>0)
    {
        update lstToInsrt;
    }
    
}
List<Student__c>obj1 = [Select Name,Student_Name__c From Student__c  Where  Student_Name__c Like '_r%' And
                               isDeleted = true All Rows];
        Database.emptyRecycleBin(obj1);
discreate Account Record 
trigger AccountPhoneUpdate on Account (before Update) 
{
    Map<Id, Account>mapaccount = new Map<Id, Account>([Select Name, Phone From Account Where Id In : trigger.old]);
    
    List<Account>updatedaccount = new List<Account>();
    for(Account a : trigger.new)
    {
        Account oldphone =  mapaccount.get(a.Id);
        if(a.Phone != oldphone.Phone && a.phone !=Null)
        {
            updatedaccount.add(a);
        }
        
    }
    update updatedaccount;
        
    
}
In which scenario after event will be used in the trigger. please give me one example
Write a program to get the total number of records present in an object(Object should be passed as an argument).Problem dML
List<Student__c>obj1 = [Select Name,Student_Name__c From Student__c  Where  Student_Name__c Like '_r%' And
                               isDeleted = true All Rows];
        Database.emptyRecycleBin(obj1);
When an opportunity is inserted check for it’s duplicacy on the basis of its name and the account to which it is related to i.e. If it has same name and it’s linked to same Account then append “Duplicate Opportunity” in the name
trigger Trigger1Problem on Account(after insert, after update) 
{
    List<Opportunity> opp = new List<Opportunity>();
    
    Map<ID, Account> accountmap = new Map<ID, Account>([Select Id, Name, (Select Id From Opportunities) From Account Where Id In :Trigger.New]);
    for(Account a : trigger.new)
    {
        if(accountmap.get(a.Id).Opportunities.size() == 0)
        {
            opp.add(new Opportunity(AccountId = a.Id, Name ='First Opportunity'+a.Name, StageName ='prospecting',CloseDate =System.today()));
        }

    }
    if(opp.size()>0)
    {
        Insert opp;
    }
}
Helper Class Which Way Doing
trigger Trigger1Problem on Account (after Insert, after Update)
{
    if(Trigger.isAfter)
    {
            if(Trigger.isInsert)
            {
                List<Opportunity>opp = new List<Opportunity>();
    
                Map<Id, Account>mapacc = new Map<Id, Account>
                ([Select Id, Name,(Select Id, Name From Opportunities) From Account Where Id In :trigger.new]);
    
            for(Account acc : trigger.new)
                {
                    if(mapacc.get(acc.Id).Opportunities.size() == 0)
                        {
                            opp.add(new Opportunity(AccountId =acc.Name, Name ='FirstOpportunity'+acc.Name, StageName = 'prospecting'
                            , CloseDate = System.today()));
                        }
                }
                insert opp;

            }
            else
            {
                if(Trigger.isUpdate)
                {
                    Map<Id, Account> nMap = new Map<Id, Account>();
                    
                    nMap = Trigger.newMap;
                    
                    List<Opportunity>opp = [Select Id, Name From Opportunity Where
                    Id In : nMap.keySet()];
                    
                    for(Opportunity oppup : opp)
                    {
                        Account a = nMap.get(oppup.Id);
                        
                        oppup.Name = a.Name;
                    }
                    update opp;
                }
            }
    
    }
    
}
no error but not inserted or not updated record doing this question 
1) context variable 
2) Helper class
whenever lead get saved with web then automatically convert it
trigger LeadConvert on Lead(after Insert, after Update)
    {
        List<String>LeadName = new List<String>();
        for(Lead myLead : trigger.new)
        {
            if(myLead.isconverted ==false && myLead.LeadSource == 'web')
            {
                Database.LeadConvert lc = new Database.LeadConvert();
                lc.setLeadId(myLead.Id);
                lc.convertedStatus = 'Closing-Converted';
                lc.setDoNotCreateOpportunity(true);
                Database.LeadConvertResult lcr = Database.convertLead(lc);
                System.assert(lcr.isSuccess());
            }   
        }
 }
try but lead not converted then how to converted automatically
trigger UpdateRecordMap on Update(after Update)
{
  Set<Id>accid = new Set<Id>();
    for(Account a : trigger.new)
    {
          accid.add(a.Id);
    }
    List<Contact>listcon = new List<Contact>();
    Map<Id, Account>mapacc = new Map<Id, Account>
    ([Select Id, BillingCity,
                            (Select Id, MailingState From Contacts) 
                    From Account Where Id In :accid]);
   for(Account a : trigger.new)
    {
             Contact con = new Contact();
             con.MailingState = a.BillingCity;
             listcon.add(con);
    }
    if(listcon.size()>0)
    {
        update listcon;
    }

}
Not Update the Record
trigger InsertBefore on Account (after insert) 
{
    List<Contact>contdetail = new List<Contact>();
  
  Map<Id, Account>mapaccount = new Map<Id, Account>([Select Id, name, phone, 
                              (Select Id,FirstName, LastName, phone From Contacts
                              )From Account Where Id In : Trigger.New]);
                              
  for(Account a : Trigger.New)
  {
         if(mapaccount.get(a.Id).contacts.size() == 0)
         {
             contdetail.add(new Contact(FirstName =a.Name, LastName =a.Name, Phone =a.phone, AccountId =a.Id));
         }
  }
}
trigger OpportunityBackup on Opportunity (after update) 
{

      List<My_Backup__c> lstToInsrt = new List<My_Backup__c>();      
    for(Opportunity op1 : Trigger.new)
    {
         if(trigger.oldMap.get(op1.Id).Name !=op1.Name)
         {
             My_Backup__c backup = new My_Backup__c();
             backup.Name = trigger.oldMap.get(op1.AccountId).Name;
             lstToInsrt.add(backup);
         }
    }
    if(lstToInsrt.size()>0)
    {
        update lstToInsrt;
    }
    
}
List<Student__c>obj1 = [Select Name,Student_Name__c From Student__c  Where  Student_Name__c Like '_r%' And
                               isDeleted = true All Rows];
        Database.emptyRecycleBin(obj1);
discreate Account Record