Before Insert :
==================
Q::Which Trigger Context variables are allowed in before insert.
Ans :
-------------------------------------------------------------------------------
Trigger.New Trigger.NewMap Trigger.old Trigger.oldMap
--------------------------------------------------------------------------------
before insert Yes No No No
Q:: Which Operations are allowed on Trigger.New in before insert trigger
Ans:
Trigger.New
----------------------------
Read Write SOQL DML
----------------------------
Yes Yes No No
Scenario
==>>Basic Level Trigger scenario's on before insert
Scenario 1: When ever we are trying to insert new Account record with industry
as banking then Annualrevenue should be set as 50000;
Code :
trigger scenario1 on Account (before insert) {
for(Account a:Trigger.New){
if(a.industry=='Banking'){
a.annualRevenue=50000;
}
}
}
Test Class :
@isTest
private class Scenario1Test {
@isTest
static void test(){
Integer count=[select count() from Account];
Account a1=new Account(Name='aaa',Industry='Banking');
try{
insert a1;
}catch(Exception e){
System.debug(e);
}
Integer size=[select count() from Account];
System.assertEquals(count+1,size);
Account acc=[select id, AnnualRevenue from Account where id=:a1.id];
System.assertEquals(acc.annualRevenue,50000);
}
}
Scneario 2: When ever new Account is creatd with out phone no it should throw
error message
Trigger :
trigger scenario2 on Account (before insert) {
for(Account a:Trigger.New){
if(a.phone=='' || a.phone==null){
a.addError('Phone no is a must ');
}
}
}
Test Class :
@isTest
private class scenario2 {
@isTest
static void testme(){
Integer count=[select count() from Account];
Account a=new Account(Name='aaa');
try{
insert a;
}catch(Exception e){
System.debug(e);
}
Integer size=[select count() from Account];
System.assertEquals(count,size);
}
}
Scneario 3:When ever new Application is created with Application Type as New
1. If pancard no is already existing in the black list then
set application status as rejected
2.If the pancard no is not available in black list set the application
status as pending
Solution:
Object :Application
Fields : Type : PicKList (New ,cancel,Block)
Status : PickList( Approved,Rejected,Pending)
Pancard: Text
Object: BlackList
Fields : Name : Text
Trigger Code:
trigger scenario3 on Application__c (before insert) {
List<String> pancards=new List<String>();
for(Application__c ap:Trigger.New){
if(ap.Type__c=='New')
pancards.add(ap.Pancard__c);
}
List<BlackList__c> black=[select id,Name from BlackList__c where name in :pancards];
Set<String> panList=new Set<string>();
if(black.size()!=0){
for(BlackList__c b:black){
panList.add(b.name);
}
}
for(Application__c a:Trigger.New){
if(panlist.contains(a.pancard__c)){
a.status__c='Rejected';
}else{
a.status__c='Pending';
}
}
}
Test Class :
@isTest
private class Scenario3 {
testmethod static void testme(){
BlackList__c b1=new BlackList__c();
b1.Name='1234';
insert b1;
Application__c ap1=new Application__c();
ap1.Applicant_Name__c='aaa';
ap1.Type__c='New';
ap1.Pancard__c='1234';
insert ap1;
Application__c res1=[select id,status__c from Application__c where id=:ap1.id];
System.assertEquals(res1.Status__c,'Rejected');
Application__c ap2=new Application__c();
ap2.Applicant_Name__c='aaa';
ap2.Type__c='Block';
ap2.Pancard__c='4567';
insert ap2;
Application__c res2=[select id,status__c from Application__c where id=:ap2.id];
System.assertEquals(res2.Status__c,'Pending');
Application__c ap3=new Application__c();
ap3.Applicant_Name__c='aaa';
ap3.Type__c='New';
ap3.Pancard__c='3456';
insert ap3;
Application__c res3=[select id,status__c from Application__c where id=:ap3.id];
System.assertEquals(res3.Status__c,'Pending');
}
}
Scenario 4: When ever new Account is created successfully corresponding contact
should be created using the details from Account
Trigger :
trigger scenario4 on Account (after insert) {
List<Contact> cons=new List<Contact>();
for(Account a:Trigger.New){
Contact c=new Contact();
c.lastname=a.name;
c.phone=a.phone;
c.accountid=a.id;
cons.add(c);
}
insert cons;
}
TestClass :
@isTest
private class scenario4Test {
@isTest(seealldata=true)
static void testme(){
Integer accCount=[select count() from Account];
Integer conCount=[select count() from Contact];
Account a=new Account(Name='aaa',Phone='123');
try{
insert a;
}catch(Exception e){
System.debug(e);
}
Integer count=[select count() from Account];
Integer size=[select count() from Contact];
System.assertEquals(acccount+1,count);
System.assertEquals(conCount+1,size);
Contact con=[select accountid from Contact where accountid=:a.id];
System.assertEquals(con.lastname,a.name);
System.assertEquals(con.phone,a.phone);
System.assertEquals(con.accountid,a.id);
}
}
Scenario 5: When ever a new Opportunity is created .Then share that record with
user
Note : before you write the trigger
OWD: opporutnity :Private
Trigger:
trigger scenario5 on Opportunity (after insert) {
User u=[select id from User where alias='kshar'];
List<OpportunityShare> records=new List<opportunityShare>();
for(Opportunity op:Trigger.New){
OpportunityShare share=new OpportunityShare();
share.OpportunityId=op.id;
share.rowCause='Manual';
share.OpportunityAccessLevel='Edit';
share.UserOrGroupId=u.id;
records.add(share);
}
insert records;
}
Tuesday, 12 April 2016
some more triggers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment