0% found this document useful (0 votes)
171 views16 pages

Untitled

1. The document describes a trigger that sets the phone number to '999' for any new Account records inserted with an Industry of 'Banking'. It includes a test class to validate this functionality. 2. It describes a trigger that calculates a LeadScore custom field on Lead objects before insertion based on the values of the Email, Phone, and Industry fields. A test class is included to validate this. 3. It describes the requirement to assign new Leads to different Queues based on their Industry field value, and assigns the record owner differently based on these criteria.

Uploaded by

R P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views16 pages

Untitled

1. The document describes a trigger that sets the phone number to '999' for any new Account records inserted with an Industry of 'Banking'. It includes a test class to validate this functionality. 2. It describes a trigger that calculates a LeadScore custom field on Lead objects before insertion based on the values of the Email, Phone, and Industry fields. A test class is included to validate this. 3. It describes the requirement to assign new Leads to different Queues based on their Industry field value, and assigns the record owner differently based on these criteria.

Uploaded by

R P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

===================================================================================

======
1. When ever new Account is inserted with Industry as 'Banking ' then set the
phone
as '888'
===================================================================================
======
Trigger:

trigger phoneUpdate on Account (before insert) {


for(Account a: Trigger.New){
if(a.industry=='Banking'){
a.phone='999';
}
}
}

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,'999');
}
}

===================================================================================
=====
2. When ever new lead is created , calculate the leadscore using before inserting

Field Name NOT NULL NULL

Email 10 0

Phone 10 0

Industry 20 0

Note : First Create a custom field LeadScore on Lead object

===================================================================================
=====

Trigger :

trigger leadscoreTrig on Lead (before insert) {


for(Lead ld: Trigger.New){
Integer count=0;
if(ld.email!=''&& ld.email!=null){
count=count+10;
}
if(ld.phone!='' && ld.phone!=null ){
count=count+10;
}
if(ld.industry!='' && ld.industry!=null){
count=count+20;
}
ld.leadScore__c=count;
}
}

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='[email protected]';
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) :

Create a Queue on Lead object as Energy Queue with Users

Scenario: When a new Lead is inserted based on the industry field value assign it

corresponding queue

Industry-Banking--assing owner of the record as Banking queue

Industry-Energy---Assign owner of the record as Energy Queue

Industry other than Banking and Energy user who ever has created the
record he should be the owner
===================================================================================
=====

===================================================================================
====
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

CloseDate as Today+30 days


===================================================================================
===
trigger AccountInsert on Account (after insert) {
List<Opportunity> optyList=new List<Opportunity>();
for(Account a: Trigger.New){
if(a.industry=='Energy' && a.AnnualRevenue >5000000){
Opportunity op=new Opportunity();
op.name=a.name;
op.stageName='Prospecting';
op.CloseDate=System.today()+20;
op.accountId=a.id;
optyList.add(op);
}
}
insert optyList;
}

TestClass :

@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 :

1.AccountId : Id of the record on which we are creating teammember

2.UserId : Id of the user whom we are adding as Teammember

3.TeamMemberRole : choose some value pickList field of TeamMemberRole


4.AccountAccessLevel : Type of Access you are providing on Account for the team
member

Posible values are 'Read' ,'Edit','ALL'

5. OpportunityAccessLevel:

6. CaseAccessLevel :

7. COntactAccessLevel :

Note : Before we implement this trigger enable the AccountTeams

setup
|----Build
|----Customize
|------Accounts
|------AccountTeams

1. Enable AccountTeam

2. Add the AccountTeams as related list on all the pageLayouts

===================================================================================
===

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.AccountAccessLevel,'Edit');
System.assertEquals(atm.TeamMemberRole,'Account Manager');
}else{
System.assertEquals(atm,null);
}
}
}

===================================================================================
===

6. When ever new opportuntiy is created with StageName as 'closed Won ' then

add karthic as OpportunityTeamMember

OpportunityTeamMember :

OpportunityId :

UserId :

OpportunityAccessLevel :

TeamMemberRole :

Note : Before we implement this enable opportunityTeams

setup
|----Build
|----Customize
|------Opportunitys
|------OpportunityTeams

1. Enable OpportuntiyTeam

2. Add the opportunitytTeams as related list on all the pageLayouts

===================================================================================
====

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.OpportunityAccessLevel,'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

create a new contact for this account with following data

Contact lastname as Account Name

COntact Phone as Account Phone


===================================================================================

Trigger:

trigger example on Account( after insert ){

List<Contact> contacts=new List<Contact>();

for(Account a: Trigger.New){

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

private class AccountTest{

@isTest
static void testme(){

Account a=new Account();


a.name='Test';
a.Type='Prospect';

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);

===================================================================================
=====

Sharing rules on the sobject are stored in thier corresponding share objects

Ex:

Account ----AccountShare

Contact-----ContactShare

Opportunity---OpportuntiyShare

Customer__c-----Customer_Share

Transaction _ _ c------Transaction_share
Q::When do you share a record ?

When OWD on a object is Private /Public Read and user doesnt have read and write
access
on a record

Q:: When we want to share a record using apex what field are needed

1. Which record : OpportunityId

2. With whom : UserOrGroupId

3. What type of access :OpportunityAccessLevel

4. Reason for sharing :RowCause

8. When ever a new Opportunity is created with stageName 'closed won' share the
record
with user karthic
===================================================================================
=====
trigger opptyShareTrig on Opportunity (after insert) {
List<OpportunityShare> share=new List<OpportunityShare>();
User u=[select id from User where alias='knair'];
for(Opportunity op:Trigger.New){
if(op.stageName=='Closed Won'){
OpportunityShare os=new OpportunityShare();
os.OpportunityId=op.id;
os.UserOrGroupId=u.id;
os.OpportunityAccessLevel='Edit';
os.RowCause='Manual';
share.add(os);
}
}
insert 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,UserOrGroupId FROM OpportunityShare
where OpportunityId=:op.id and UserOrGroupId=:u.id];
if(op.stageName=='Closed Won'){
System.assertEquals(share.OpportunityAccessLevel,'Edit');
System.assertEquals(share.RowCause,'Manual');
}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:

trigger CaseShareTrig on Case (after insert) {


List<CaseShare> share=new List<CaseShare>();
User u=[select id from User where alias='knair'];
for(Case c:Trigger.new){
if(c.origin=='Email' && c.priority=='High'){
CaseShare cs=new CaseShare();
cs.caseId=c.id;
cs.UserOrGroupId=u.id;
cs.caseAccesslevel='Edit';
cs.RowCause='Manual';
share.add(cs);
}
}
insert share;
}

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 ,RowCause from
CaseShare where CaseId=:c.id and userOrGroupId=:u.id];
if(c.origin=='Email' && c.Priority=='High'){
System.assertEquals(cs.CaseAccessLevel,'Edit');
System.assertEquals(cs.RowCause,'Manual');
}else{
System.assertEquals(cs,null);
}
}
}

===================================================================================
====
10. Customer__c : Customer_share

Loan__c : Loan_Share

Candidate__c : Candidate_share

Fields :

ParentId : id of the record which we want to share

UserOrGroupId : Id of the user / group with whom the record need to


shared

AccessLevel : Type of Access we want to provide for the user

RowCause : Reason for sharing

Note :RowCause=Schema.CustomObject_Share.RowCause.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

10 .When ever new customer is created with status Approved then share the record
with
user kavya with read Access
===================================================================================
======
trigger CustomerShare on Customer__c ( after insert ){

List<Customer_share> share=new List<Customer_Share>();


User u=[select id from user where username='[email protected]'];

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

otherPhone as Old Phone no of Account

===================================================================================
=====
class: AccountTriggerHandler :

public class AccountTriggerHandler {


public static void beforeUpdate(Map<Id,Account> oldMap,Map<Id,Account> newMap){
List<Id> accIds=new List<Id>();

for(Id key:oldMap.keySet()){
Account old=oldMap.get(key);
Account newAcc=newMap.get(key);
if(old.phone!=newAcc.Phone){
accIds.add(key);
}
}
List<Contact> cons=[select id,accountId,homephone,otherphone from Contact
where accountId in:accIds];
for(Contact c:cons){
Account a=newMap.get(c.accountId);
Account b=oldMap.get(c.accountId);
c.otherPhone=b.phone;
c.homephone=a.phone;
}
update cons;
}
}

Trigger :

TestClass :

trigger AccountPhoneupdate on Account (before update) {


AccountTriggerHandler.beforeUpdate(Trigger.oldMap,Trigger.newMap);
}
@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,newPhone);
System.assertEquals(c2.otherphone,oldphone);
}else{
System.assertEquals(c2.homephone,c1.homephone);
System.assertEquals(c2.otherphone,c1.otherPhone);
}

}
}

===================================================================================
=====

12. Create a Following Custom Fields on Account Object :

Field Name DataType


--------------------------------
Total_Amount Currency

PipeLine_Amount Currency

Won_Amount Currency

Lost_Amount Currency

Scenario 1: When a new Opportunity record is created for an account rollup the
opportunity amount to Corresponding Account

1. If new Opportunity Stage is Close Won then add the amount to Account 's

Total_Amount=Total_Amount+Opportunity Amount

Won_Amount=Won_Amoutn+Opportuntiy amount

2. If new Opportunity stage is closed Lost then add the amount to Accounts
Total_Amount=Total_Amount+Opportunity Amount

Lost_Amount=Lost_Amoutn+Opportuntiy amount

3. If new Opportunity stage is not closed Lost or closed won then add
the amount to Accounts

Total_Amount=Total_Amount+Opportunity Amount

PipeLine_Amount=PipeLine_Amount+Opportuntiy amount

Scenario 2: When a existing opportunity stage is modified to closed won or closed


lost

then

1. If stageName is modified to Closed won

Remove the amount from PipleLine_Amount and add it to WOn_Amount

2. If StageName is modified to Closed Lost

Remove the amount from PipleLine_Amount and add it Lost_Amount


===================================================================================
=====

Class :

public class OpptyTriggerHandler {

public static void afterInsert(List<Opportunity> optyList){

Map<Id,List<Opportunity>> accMap=new Map<Id,List<Opportunity>>(); // key


accountId value
for(Opportunity op:OptyList){

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);

for(Opportunity p:oppList){

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{

if(p.stageName=='Closed Lost'){

a.Lost_Amount__c=a.Lost_Amount__c+p.amount;

}else{

a.pipeLine_Amount__c=a.pipeLine_Amount__c+p.amount;

}
}
}
}

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.accountId)){
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,newList);
}
}
}
}
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_Amount__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_Amount__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>();

for(Id key: Trigger.oldMap.keyset()){

Contact old=Trigger.oldMap.get(key);
Contact newCon=Trigger.newMap.get(key);

if(old.phone!=newCon.phone){
contacts.add(newCon);
}

List<Account> accs=new List<Account>();


for(Contact c:contacts){
Account a=new Account();
a.id=c.accountId;
a.phone=c.phone;
accs.add(a);
}

update accs;
}

You might also like