Professional Documents
Culture Documents
TRIGGERS
TRIGGERS
age;
if(age==' '){
system.debug('hellooooooooooo');
}
string age;
if(age==null){
system.debug('hai::::::::::::::::');
}
salesforce apex null check
custom-rollup summary
========================================
========================================
=========
1. When ever new Account is inserted with Industry
as 'Banking ' then set the phone
as '888'
========================================
========================================
=========
Trigger:
}
}
@istest
public class edokati{
testmethod static void kkk(){
account obj = new account();
obj.name='gggg';
obj.industry='banking';
insert obj;
}
}
Test Class :
@isTest
private class PhoneTest {
testmethod static void testme(){
Account acc=new Account();
acc.Name='capital';
acc.Industry='Banking';
try{
insert acc;
}catch(Exception e){
}
Account a=[select id,phone from Account where
id=:acc.Id];
System.assertEquals(a.phone,'888');
}
}
========================================
========================================
========
2. When ever new lead is created , calculate the
leadscore using before inserting
Phone 10 0
Industry 20 0
========================================
========================================
========
Trigger :
Test Class :
@isTest
private class LeadScoreTest {
@isTest
static void testme(){
Lead ld=new Lead();
ld.lastname='satish';
ld.company='Capital';
ld.phone='111';
ld.email='javasfdc@gmail.com';
ld.industry='Banking';
try{
insert ld;
}catch(Exception e){
}
Lead newLead=[select LeadScore__c from
Lead where id=:ld.id];
System.assertEquals(newLead.LeadScore__c,40);
}
}
========================================
========================================
========
3.Create a Queue on Lead object as Banking Queue
with users ( satish) :
========================================
========================================
=======
4.When ever new Account is created with
AnnualReneue more than 50 Lacs and Industry
as Energy then create a new opportunity for this
account with
OpportunityName as AccountName
StageName as Prospecting
@isTest
private class AccountInsertTest {
@isTest
static void testme(){
Account a1=new Account();
a1.name='Test';
a1.Industry='Energy';
a1.AnnualRevenue=9000000;
try{
insert a1;
}catch(Exception e){
}
Integer count=[select count() from Opportunity];
if(a1.Industry=='Energy' &&
a1.AnnualRevenue>5000000)
System.assertEquals(count,1);
else
System.assertEquals(count,0);
}
}
========================================
========================================
=======
5. When ever new Account record is created with
Industry as "Education ' and
annualRevenue as 10 Lacs then add user karthic
as AccountTeamMember
AccountTeamMember :
5. OpportunityAccessLevel:
6. CaseAccessLevel :
7. COntactAccessLevel :
========================================
========================================
======
Trigger:
trigger AccountTeamTrig on Account (after insert) {
List<AccountTeamMember> teams=new
List<AccountTeamMember>();
User u=[select id from User where alias='knair'];
for(Account a:Trigger.New){
if(a.Industry=='Education' && a.AnnualRevenue
>1000000){
AccountTeamMember atm=new
AccountTeamMember();
atm.accountId=a.id;
atm.userId=u.id;
atm.TeamMemberRole='Account Manager';
atm.AccountAccessLevel='Edit';
teams.add(atm);
}
}
insert teams;
}
TestClass :
@isTest
private class AccountTeamTest {
@isTest
static void testme(){
User u=[select id from User where alias='knair'];
Account a=new Account();
a.Industry='Education';
a.name='capital';
a.AnnualRevenue=1300000;
try{
insert a;
}catch(Exception e){
System.debug(e);
}
AccountTeamMember atm=[select
userId,AccountId,AccountAccessLevel from
AccountTeamMember where AccountId=:a.id];
if(a.Industry=='Education' &&
a.AnnualRevenue>1000000){
System.assertEquals(atm.userId,u.id);
System.assertEquals(atm.AccountAccessLev
el,'Edit');
System.assertEquals(atm.TeamMemberRole,'
Account Manager');
}else{
System.assertEquals(atm,null);
}
}
}
========================================
========================================
======
OpportunityTeamMember :
OpportunityId :
UserId :
OpportunityAccessLevel :
TeamMemberRole :
Note : Before we implement this enable
opportunityTeams
setup
|----Build
|----Customize
|------Opportunitys
|------OpportunityTeams
1. Enable OpportuntiyTeam
========================================
========================================
=======
Trigger:
trigger opptyTeamTrig on Opportunity (after insert) {
List<OpportunityTeamMember> teams=new
List<OpportunityTeamMember>();
User u=[select id from User where alias='knair'];
for(Opportunity op:Trigger.New){
if(op.stageName=='closed Won'){
OpportunityTeamMember otm=new
OpportunityTeamMember();
otm.OpportunityId=op.id;
otm.UserId=u.id;
otm.TeamMemberRole='Account Manager';
otm.OpportunityAccessLevel='Edit';
teams.add(otm);
}
}
insert teams;
}
TestClass :
@isTest
private class OpportunityTeamTrig {
@isTest
static void testme(){
User u=[select id from User where alias='knair'];
Opportunity a=new Opportunity();
a.name='Test';
a.stageName='closed won';
a.closeDate=System.today()+5;
try{
insert a;
}catch(Exception e){
System.debug(e);
}
OpportunityTeamMember atm=[select
userId,OpportunityId,OpportunityAccessLevel from
OpportunityTeamMember where OpportunityId=:a.id];
if(a.stageName=='closed won' ){
System.assertEquals(atm.userId,u.id);
System.assertEquals(atm.OpportunityAccess
Level,'Edit');
System.assertEquals(atm.TeamMemberRole,'
Account Manager');
}else{
System.assertEquals(atm,null);
}
}
}
========================================
========================================
=======
7. When ever new Account record is created with
Account Type as 'prospect ' then
Trigger:
if(a.type=='Prospect'){
Contact c=new Contact();
c.lastname=a.name;
c.phone=a.phone;
c.accountId=a.id;
contacts.add(c);
}
}
insert contacts;
}
Test Class :
@isTest
try{
insert a;
}catch(Exception e){
}
Integer count=[select count() from
Contact];
if(a.Type=='Prospect'){
System.assertEquals(count,1);
}else{
System.assertEquals(count,0);
========================================
========================================
========
Ex:
Account ----AccountShare
Contact-----ContactShare
Opportunity---OpportuntiyShare
Customer__c-----Customer_Share
Transaction _ _ c------Transaction_share
Test Class :
@isTest
private class OpptyShareTest {
@isTest
static void testme(){
Opportunity op=new Opportunity();
op.stageName='closed won';
op.closeDate=system.today();
op.name='Test';
try{
insert op;
}catch(Exception e){
}
user u=[select id from user where alias='knair'];
OpportunityShare share=[select
OpportunityAccessLevel,OpportunityId,RowCause,Us
erOrGroupId FROM OpportunityShare where
OpportunityId=:op.id and UserOrGroupId=:u.id];
if(op.stageName=='Closed Won'){
System.assertEquals(share.OpportunityAcces
sLevel,'Edit');
System.assertEquals(share.RowCause,'Manu
al');
}else{
System.assertEquals(share,null);
}
}
}
========================================
========================================
======
9. when ever new Case is created with Priority as
'High' and origin as 'Email'
then share the record with karthic with edit
permission
========================================
========================================
======
Trigger:
Test Class :
@isTest
private class CaseShareTest {
@isTest
static void testme(){
Case c=new Case();
c.status='New';
c.Origin='Email';
c.Priority='High';
c.subject='Test';
try{
insert c;
}catch(Exception e){
}
User u=[select id from User where alias='knair'];
CaseShare cs=[select
CaseId,UserOrGroupId,CaseAccessLevel ,RowCaus
e from CaseShare where CaseId=:c.id and
userOrGroupId=:u.id];
if(c.origin=='Email' && c.Priority=='High'){
System.assertEquals(cs.CaseAccessLevel,'Edi
t');
System.assertEquals(cs.RowCause,'Manual');
}else{
System.assertEquals(cs,null);
}
}
}
========================================
========================================
=======
10. Customer__c : Customer_share
Loan__c : Loan_Share
Candidate__c : Candidate_share
Fields :
Note :RowCause=Schema.CustomObject_Share.Ro
wCause.Reason
Ex: Schema.Customer_Share.RowCause.Manual
Ex: Schema.Candidate_Share.RowCause.Owner
Example :
Object :Customer__c
Fields : Status : Approved
Rejected
Name : Text
Owd: Private
List<Customer_share> share=new
List<Customer_Share>();
User u=[select id from user where
username='kavya@capital.com'];
for(Customer__c c:Trigger.New){
if(c.status__c='Accepted'){
Customer_Share cs=new
Customer_Share();
cs.parentId=c.id;
cs.userOrGroupId=u.id;
cs.Accesslevel='Read';
cs.RowCause=Schema.Customer_Share.RowCause.
Manual;
share.add(cs);
}
}
insert share;
}
========================================
========================================
========
11.When ever the phone no of the account is
modified then set corresponding contacts
homephone as new Phone no of Acctount
========================================
========================================
========
class: AccountTriggerHandler :
Trigger :
TestClass :
@isTest
private class AccountPhoneTest {
testmethod static void testme(){
String oldPhone='111';
Account a1=new Account();
a1.name='Test';
a1.phone=oldPhone;
insert a1;
Contact c1=new Contact();
c1.accountId=a1.id;
c1.lastname='myla';
insert c1;
String newPhone='3445';
try{
a1.phone=newPhone;
update a1;
}catch(Exception e){
}
Contact c2=[select id,otherphone,Homephone
from Contact where id=:c1.id];
if(oldPhone!=newPhone){
System.assertEquals(c2.homePhone,newPho
ne);
System.assertEquals(c2.otherphone,oldphon
e);
}else{
System.assertEquals(c2.homephone,c1.home
phone);
System.assertEquals(c2.otherphone,c1.other
Phone);
}
}
}
========================================
========================================
========
Lost_Amount Currency
Scenario 1: When a new Opportunity record is
created for an account rollup the opportunity amount
to Corresponding Account
Total_Amount=Total_Amount+Opportunity
Amount
Won_Amount=Won_Amoutn+Opportuntiy
amount
Total_Amount=Total_Amount+Opportunity
Amount
Lost_Amount=Lost_Amoutn+Opportuntiy
amount
Total_Amount=Total_Amount+Opportunity
Amount
PipeLine_Amount=PipeLine_Amount+Opportuntiy
amount
then
Class :
if(accMap.containsKey(op.accountId)){
List<Opportunity>
optList=accMap.get(op.accountId);
optList.add(op);
accMap.put(op.accountId,optList);
}else{
List<Opportunity> ops=new
List<Opportunity>{op};
accMap.put(op.accountId,ops);
}
}
List<Account> accounts=[select
id,Total_Amount__c,Won_Amount__c,Lost_Amount_
_c,
PipeLine_Amount__c from Account where
Id in:accMap.keyset()];
for(Account a: accounts){
List<Opportunity>
oppList=accMap.get(a.id);
a.Total_Amount__c=a.Total_Amount__c+p.amount;
if(p.stageName=='Closed Won'){
a.Won_Amount__c=a.Won_Amount__c+p.amount;
}else{
a.Lost_Amount__c=a.Lost_Amount__c+p.amount;
}else{
a.pipeLine_Amount__c=a.pipeLine_Amount__c+p.a
mount;
}
}
}
}
update accounts;
}
public Static void
afterUpdate(Map<Id,Opportunity>
oldMap,Map<Id,Opportunity> newMap){
List<Opportunity> optyChanged=new
List<Opportunity>();
Map<Id,List<Opportunity>> accMap=new
Map<Id,List<Opportunity>>();
for(Id key: oldMap.keyset()){
Opportunity old=oldMap.get(key);
Opportunity newOpp=newMap.get(key);
if(old.stageName!=newOpp.stageName ){
if((old.stageName!='Closed Won' &&
old.stageName!='closed Lost')&&
(newOpp.StageName=='closed won' ||
newOpp.stageName=='Closed Lost')){
if(accMap.containsKey(newOpp.accountI
d)){
List<Opportunity>
myList=accMap.get(newopp.accountId);
myList.add(newOpp);
accMap.put(newopp.accountId,myList)
;
}else{
List<Opportunity> newList=new
List<Opportunity>();
newList.add(newOpp);
accMap.put(newOpp.accountId,newLi
st);
}
}
}
}
List<Account> accounts=[select
id,Total_Amount__c,Won_Amount__c,Lost_Amount_
_c,
PipeLine_Amount__c from Account where Id
in:accMap.keyset()];
for(Account a: accounts){
List<Opportunity>
oppList=accMap.get(a.id);
for(Opportunity p:oppList){
if(p.stageName=='Closed Won'){
a.Won_Amount__c=a.Won_Amount__c+p.amount;
a.pipeLine_Amount__c=a.pipeLine_Amo
unt__c-p.amount;
}else{
if(p.stageName=='Closed Lost'){
a.Lost_Amount__c=a.Lost_Amount__c+p.amount;
a.pipeline_Amount__c=a.pipeLine_A
mount__c-p.amount;
}
}
}
}
update accounts;
}
}
Trigger :
---------
trigger OptyRollUp on Opportunity (after insert,after
update) {
if(Trigger.isAfter && Trigger.isInsert){
OpptyTriggerHandler.afterInsert(Trigger.new);
}
if(Trigger.isAfter && Trigger.isUpdate){
OpptyTriggerHandler.afterUpdate(Trigger.oldMap
,Trigger.NewMap);
}
}
========================================
========================================
========
13 .When ever the phone field in the contact is
updated then Update the corresponding
Account records phone no as Contacts new Phone
no
========================================
========================================
========
trigger contactPhone on Conact(before update){
List<Contact> contacts=new List<Contact>();
Contact old=Trigger.oldMap.get(key);
Contact newCon=Trigger.newMap.get(key);
if(old.phone!=newCon.phone){
contacts.add(newCon);
}
update accs;
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----------------------
Scenario 14:
Object : Lead
Evernt : before Insert
Requirement : When ever new Lead is created with
lead source as Web then assign wilson as owner
-----------------------------------------------------------------------
-----------------------------------------------------------------------
--------------------
Triggers :
trigger OwnerAssign on Lead (before insert) {
User u=[select id from user where
username='wilson@dev.com'];
for(Lead my:Trigger.new){
if(my.leadsource=='Web'){
my.ownerId=u.Id;
}
}
}
Test Class :
@isTest
private class OwnerAssignTest {
@isTest
static void testme(){
Lead my=new Lead();
my.LastName='Kiran';
my.company='Capital';
my.AnnualRevenue=8000;
my.LeadSource='Web';
insert my;
User u=[select id from User where
username='wilson@dev.com'];
Lead l=[select ownerId from Lead
where id=:my.Id];
if(my.leadSource=='Web'){
System.assertEquals(l.ownerId,u.Id);
}
}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----------------------
Scenario 15:
Object : Account
Event : before insert
Requirement : When ever new Account is created
with Industry as 'banking' and Account Type as
'prospect'
then assign the wilson as the owner of the
record
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----------------------
@isTest
private class AccountInsertTest {
@isTest
static void testme(){
Account acc=new Account();
acc.Name='Testing';
acc.Industry='Banking';
acc.Type='Prospect';
insert acc;
User u=[select id from User where
username='wilson@dev.com'];
Account a=[select id,ownerId from Account
where id=:acc.id];
if(acc.Industry=='Banking' &&
acc.Type=='Prospect')
System.assertEquals(a.ownerId,u.id);
}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----
Scenario 16:
Object : Lead
Event : before Insert
Requirement : When ever new lead is created
calcualte the lead score
Field Name Entered Blank
FirstName : 10
AnnualRevenue : 20 0
Phone : 10 0
First Create : Custom Field
Lead Score : Number(4)
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----------
trigger LeadQualityTrig on Lead (before insert) {
for(Lead my:Trigger.new){
Integer count=0;
if(my.firstName!=''&&my.firstName!=null)
count=count+10;
if(my.AnnualRevenue!=null)
count=count+20;
if(my.phone!='' &&my.phone!=null)
count=count+10;
my.LeadScore__c=count;
}
}
@isTest
private class LeadQualityTest {
testmethod static void testme(){
Lead my=new Lead();
my.lastname='Test1';
my.firstName='Kiran';
my.annualRevenue=9000;
my.phone='123';
insert my;
Lead testlead=[select id ,LeadScore__c from
Lead where id=:my.Id];
if(my.firstName!=null&&my.FirstName!=''
&&my.AnnualRevenue!=null && my.Phone!
=''&&my.phone!=null){
System.assertEquals(testlead.leadScore__c,4
0);
}
}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
---
Scenario : 17
Object : Opportunity
Event : After Insert
Requirement : When ever new opportunity is created
with opportuntiy Stage as closed won share the
record with
user wilson
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-------------------
trigger OptyShare on Opportunity (after insert) {
User u=[select id from user where
username='students@batch0275.com'];
List<OpportunityShare> share=new
List<OpportunityShare>();
for(Opportunity p:Trigger.New){
if(p.stageName=='closed won'){
OpportunityShare os=new
OpportunityShare();
os.opportunityId=p.id;
os.OpportunityAccessLevel='Read';
os.UserorGroupId=u.id;
os.rowCause='Manual';
share.add(os);
}
}
insert share;
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----------
Scenario 18:
Object : Account
Event : After Insert
Requirement : When ever new account is creatd
with Industry as Energy and AnnualRevenue more
than 20 lac
add wilson as Team memner on the Account
AccountTeamMember :
AccountId
UserId
AccountAccessLevel
TeamMemberRole
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----------------
Apex Class :
Trigger:
trigger AccountTeamTrigger on Account (after insert)
{
AccountTeamHandler.afterInsert(Trigger.new);
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----------------------
Scenario 19:
Object :Contact
Event : Before Update
Requirement : When ever the contacts phone
numner is ,modified ,Contact old phone number
should ne
saved as Contacts other phone
-----------------------------------------------------------------------
-----------------------------------------------------------------------
------------------
Apex Class :
public class ContactTriggerHandler {
public static void beforeUpdate(Map<Id,Contact>
oldMap,Map<Id,Contact> newMap){
List<Id> modified=new List<Id>();
for(Id key:oldMap.keySet()){
Contact old=oldMap.get(key);
Contact newRec=newMap.get(key);
if(old.phone!=newRec.Phone){
newRec.otherPhone=old.phone;
}
}
}
}
Trigger :
trigger ContactUpdaterigger on Contact (before
update) {
ContactTriggerHandler.beforeUpdate(Trigger.oldMap
,Trigger.newMap);
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
---------------------
Scenario 20:
Create Custom Object :Course
Fields : Status : PicKList(Active,InActive)
Object :Course
Event : After Update
Requirement : When the status of the course is
changed to InActive then Delete all the child records
or course.
Object :Account
Test Class :
@isTest
private class OwerExample {
@isTest
static void testme(){
User kar=[select id from User where
username='developer@batch0265.com'];
User sa=[select id from User Where
Username='batch0265@capital.com'];
Account a1=new
Account(Name='Test1',Industry='Energy',AnnualReve
nue=30000);
Account a2=new
Account(Name='Test2',Industry='Banking',AnnualRev
enue=6000000);
try{
insert a1;
insert a2;
}catch(Exception e){
System.debug(e);
}
Account acc1=[select id ,ownerId from Account
where id=:a1.Id];
System.assertEquals(acc1.ownerId,kar.id);
Account acc2=[select id,ownerId from Account
where id=:a2.Id];
System.assertEquals(acc2.ownerId,sa.id);
}
}
========================================
====================================
Object :Opportunity
Scenario :
When a new opportunity is created with stageName
as 'Closed Won' then this
record should be shard with user karthic with read
access ;
========================================
=================================
Object : Account :
Scenario : When ever Account phone no is modified
udpate the corresponding
contats otherphone with modified Account's
phone no
Map<Id,Account> oldMap=Trigger.OldMap;
Map<Id,Account> newMap=Trigger.NewMap;
if(oldMap.get(aid).Phone!
=newMap.get(aid).Phone){
accIds.add(aid);
}
}
List<Contact> cons=new List<Contact>();
for(Account a:accs){
for(Contact c:a.contacts){
c.otherphone=newMap.get(a.id).phone;
cons.add(c);
}
}
update cons;
}
----------------
@isTest
private class PhoneTest {
testmethod static void testme(){
Account a1=new
Account(name='aaa',phone='111');
insert a1;
Contact c1=new
Contact(lastname='test',accountId=a1.id);
insert c1;
try{
a1.phone='555';
update a1;
}catch(Exception e){
System.debug(e);
}
Contact con=[select id ,otherphone from Contact
where accountId=:a1.id];
System.assertEquals(con.otherPhone,a1.phone)
;
}
}
========================================
====================================
Object : Account
Scenario :
When ever account industry field is modified to
Banking then add
karthic as accountTeam member
----------------------------------------------------------
Trigger :
========================================
========================================
=======================
Scenario 2:
========================================
========================================
========================
Object 1: Opportunity :
Create a Custm Fields : Field Name
DataType
---------------------------------------
Total_Opportunities__c :
Number
Total_Opportunity_Amount__c :
Currency
for(Opportunity opp=Trigger.new){
if(opp.accountId==null){
opp.addError('Opportuity Can not
created with out AccountId');
}
}
}
if(Trigger.isAfter && Trigger.isInsert){
Set<Id> accId=new Set<Id>();
for(Opportunity op:Trigger.New){
accId.add(op.accountId);
}
List<Account> accounts=[select
Total_Opportunities__c,Total_Opportunity_Amount__
c,
(select id,Amount from
Opportunities) from Account where Id in:accId];
for(Account a:accounts){
a.Total_Opportunities__c=a.opportunities.size();
Decimal sum=0;
for(Opportunity p:a.opportunities){
sum=sum+p.amount;
}
a.Total_Opportunity_Amount__c=sum;
}
update accounts;
}
}
========================================
========================================
==========================
Scenario 3:
========================================
========================================
===========================
Object : Contact
Custom Fields : Field Name DataType
----------------------------------------------
Source PickList: Sfdc
SAPExternal
Account :
Custom Fields : Field Name DataType
----------------------------------------------
Source PickList: Sfdc
SAPExternal
========================================
========================================
=====================
Scenario 4:
========================================
========================================
=====================
Object : Opportunity :
1. When ever a new Opportunity is created with
Amount more than 10 lakhs and source as 'Web'
Then add karthic as OpportunityTeamMember
Solution :
p.Stage=Opportunity_OldStage---
Opportunity_NewStage
p.Days =Today()-
opportunity_LastmOdifiedDate
p.Name=opportunityName
Solution :
p.Stage__c=oldOpt.StageName+'-'+newOpt.StageNa
me;
Date
modDate=oldOpt.LastModifiedDate.date();
Integer
days=System.Today().daysBetween(modDate);
p.Days__C=days;
p.ConvertedDate__c=System.today();
optlist.add(p);
}
}
insert optlist;
}
========================================
========================================
=============================
Scenario :6
========================================
========================================
============================
Object : Application__c
Salaried
Executive Lookup(user)
Email Email -
Object : BlackList
Object : Customer
Field Name DataType Options
-----------------------------------------------------
First Name Text
Applicatant PickList Business
Salaried
=========================
------------------------------------------------------------------
Scenario :1
1. When ever a we are inserting a new Account
record before the
record is inserted check the industry value
for(Contact c:Trigger.New){
if(c.leadsource=='Web' &&
(c.accountId==''||c.accountId==null)){
}
}
}
TestClass:
----------
@isTest
private class Example2 {
@isTest
static void testme(){
Contact c1=new
Contact(lastname='satish',leadSource='web');
Contact c2=new Contact(lastname='Myla');
List<Contact> cons=new List<Contact>{c1,c2};
Integer count=[select count() from Contact];
try{
Database.insert(cons,false);
}catch(Exception e){
System.debug(e);
}
Integer size=[select count() from contact ];
System.assertEquals(count+1,size);
}
}
-----------------------------------------------------------------------
------
Scenario 3:
1. When a new opportunity is created with following
details
1.leadsourc is Web
3. stagename!=closed won
2. Reassign the owner to user karthic
-----------------------------------------------------------------------
-----
trigger example3 on Opportunity (after insert) {
User u=[select id from user where alias='kart'];
List<Opportunity> ops=[select
leadsource,Amount,StageName from Opportunity
where id in:Trigger.new];
for(Opportunity p:ops){
if(p.stageName!='Closed won' && p.amount
>50000 && p.leadsource=='Web'){
p.ownerId=u.id;
}
}
update ops;
}
TestClass :
------------
@isTest
private class ExampleTest3 {
@isTest
static void testme(){
Opportunity op=new Opportunity();
op.name='Test';
op.amount=10000;
op.closeDate=System.today()+3;
op.stageName='Qualification';
op.leadSource='Web';
try{
insert op;
}catch(Exception e){
System.debug(e);
}
User u=[select id from user where alias='kart'];
Opportunity p=[select ownerid from Opportunity
where id=:op.id];
System.assertEquals(p.ownerId,u.id);
}
}
-----------------------------------------------------------------------
-------
Scenario 4:
1. FirstName : 10 points
3. Email : 10 points
-----------------------------------------------------------------------
----
1. Deletion of account record should fail it has any
contact records
}
-----------------------------------------------------------------------
------