top of page

Important Trigger Solved Questions

  • Writer: Abhilash Banpurkar
    Abhilash Banpurkar
  • Oct 13, 2022
  • 2 min read

Create Validation Rule on same Object.


trigger oppValidate on Opportunity(before insert,before update)
{
for(Task tsk:trigger.new){
if(tsk.Type==null)
tsk.addError('Please Fill Type')
}
}

Create Validation rule on Child object based on the Parent Object.



trigger oppValidate on Opportunity(before insert,before Update)
{
for(Opportunity Opp:Trigger.new)
{
if(opp.Accountid!=null && opp.Account.premium_Account__c!=True)
opp.addError('You can not Save opp');
}
}

Send Email When Parent record field will update based on the client record.




trigger updateAccountType on Contact(After insert, after update){
set<id>accids=new Set<id>();
for(Contact con:Trigger.new)
{
accids.add(con.accountid);
map<ID,account> accMap =new Map<ID,account>([Select ID,Account_type__c 
                                              from account Where ID 
                                                            IN:accids]);
for(contact conobj:Trigger.new)
{
accmap.get(conObj.Accountid).Account_Type__c=conobj.contact_type__c;
}
update accMap.values();
Messaging.SingleEmailMessage mail= new Messaging.singleEmailMessage();
String[] toAddresses=new String[]{'xyz@gmail.com'};
mail.setToAddresses(toAddresses);
mail.subject=update Contact;
mail.PlainTextBody='Your Account has been Updated';
Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail});
                                                            

Create Roll up Summary on Parent(Lookup relationship).




trigger rollupSummaryofcontact on Contact(after insert,after update,after 
                                           delete,after undelete)
  Set<id> accids =new Set<id>();
  List<Account> accUp= new List<Account>();
  if(Trigger.isInsert||trigger.isUpdate||trigger.isundelete)
  {
  for(Contact con:trigger.new)
  {
  accids.add(con.accountId);
  }
  }
  if(Trigger.isDelete)
  {
  for(contact con:trigger.old)
  {
  accids.add(Con.accountid);
  }
  }
List<account>acclist=newList<account([Selectid,Number_of_contacts__c                                                                              
                                 (select ID from Contacts) from Account 
                                     where ID IN:accids]); 
   for(account acc:acclist)
   {
   acc.Number_of_Contact__c=acc.contact.size();
   accup.add(acc);
   }
   update accup;                                                                          

Update Task based on parent object.



trigger updateTaskBasedon Case on Account(after insert,after update){
set<id>accids =new Set<id>();
set<id>caseids=new set<id>();
List<Task> updateTsk= new List<task>();
for(Account accobj:trigger.new)
{
if(accObj.NumberofLocation__c==0)
    accids.add(accObj.id);
    }
    List<Case> cslist=[Select id,accountID, Subject from case where accountid in:accids AND Subject ='Test insertCase&Task'Limit 1];
    for(Case csObj:cslist)
    {
    if(csobj.Subject=='Test InsertCase&Task')
        caseids.add(csObj.id);
        }
        List<Task> tsklist=[select id,Status from task where whatid 
                               IN:caseids AND status='open'];
        for(Task tskObj:tskList)
        {
        tskObj.status='Completed';
        updatetsk.add(tskObj);
        }
        update updatetsk;
        }                       

Create task releated to child object based on Parent Object.




trigger inserttaskandCase on Account(after insert){
List<Case> insertcs= new list<Case>();
List<Task> inserttsk =new List<Task>();
for(Account acc:trigger.new)
{
Case cs=new Case();
cs.Subject='Test'+acc.Name;
cs.accountID='open';
cs.Origin='Phone';
insertCS.add(cs);
}
insert insertCS;
for(Case csObj:insertCS)
{
task.tsk=new task();
tsk.Subject='Test Task';
tsk.Status='Open';
tsk.Priority='Normal';
tsk.whatid=csObj.ID;
insertTsk.add(tsk);
}
insert insert Tsk;
}

Recent Posts

See All
Important Trigger Solved Example

1 Update Child Object based on Parent Object Field. Trigger updatecontacttype on Account(after update){ Set<ID> accids =new Set<id>();...

 
 
 

Comments


bottom of page