trigger scenario interview questions

1.when ever record is inserted to the account automaticaly inserted to the contact?
        
trigger trig1 on Account (before insert) 
{
    list<contact>c=new list<contact>();
for(account a:trigger.new)
{
contact ct=new contact();
ct.LastName=a.Name;
c.add(ct);    
}
    insert c;
}
    





create new account name of dup









account name dup is created








now go to contact tab and search the dup in contact tab 



2.write trigger in account for limit reached the records after creating two records for third record error message should come?


trigger trig2 on Account (before insert) 
{
integer count=0;
list<account>acc=[select id,name from account where createddate=today];    
for(account a:trigger.new)
{
count=acc.size();    
if(count>1)
{
a.adderror('limit');    
}    
}

}



create new record

create another record error message will come because limit is 1 only one record can be created.




3.write trigger for contact when ever we create or update  contact record  number of contacts created update shoulb be vissible in account field or if you delete olso number should be decreased?


trigger trigcountdel on Contact (after INSERT, after UPDATE, after DELETE) 
{
list <string> accountIds = new list <string>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}
    
    if(Trigger.isupdate){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
    

for(Account acc:[SELECT Id,Name,countcontact__c,(Select Id from Contacts) from Account where Id IN: accountIds])
{
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.countcontact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;


}





create a new contact

create new contact and account lookup select any record from account i selected count









now delete any record from contact that should have lookup with account 



 now in countcontact field the number is decresed 



3.can not insert/update/delete that user account object records


trigger trig3 on Account (before insert,before delete,before update)
{
if(trigger.isdelete)
{
for(account a:trigger.old)
{
a.adderror('you cant delete'); 

}

if(trigger.isupdate)
{
for(account a:trigger.new)
{
a.adderror('you cant update'); 
}     
}
 
if(trigger.isinsert)
{
for(account a:trigger.new)
{
a.adderror('you cant insert'); 
}     
}

}



                                         if you insert record it will show error                                                             message


    
if you delete it will show error message


if you update it will show error message

4.when ever contact created in lead automaticly created in account,contact and opportunity?



trigger trig4 on Lead (before insert) 
{
list<account>a=new list<account>();
list<contact>a1=new list<contact>();
list<opportunity>a2=new list<opportunity>();
for(lead l:trigger.new)
{
account ac=new account();
ac.Name=l.LastName;
contact c=new contact();
c.LastName=l.LastName;
 opportunity op=new opportunity();
 op.Name=l.LastName;
 op.StageName='closed won';
 op.CloseDate=system.today();   
a.add(ac);
a1.add(c);
a2.add(op);    
}
insert a;
insert a1;
insert a2;    
    
}
                                creat a new lead record

                                lead record is created in contact you can see                                            here 

                                  lead record created in opportunity


              lead record created in account





2 comments:

  1. It's very nice...Provide more examples

    ReplyDelete
    Replies
    1. thankyou for your comment.....sure i will provide more examples that will help you for your interview

      Delete