Untitled
Untitled
======
1. When ever new Account is inserted with Industry as 'Banking ' then set the
phone
as '888'
===================================================================================
======
Trigger:
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
Email 10 0
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='[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) :
Scenario: When a new Lead is inserted based on the industry field value assign it
corresponding 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
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 :
5. OpportunityAccessLevel:
6. CaseAccessLevel :
7. COntactAccessLevel :
setup
|----Build
|----Customize
|------Accounts
|------AccountTeams
1. Enable AccountTeam
===================================================================================
===
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
OpportunityTeamMember :
OpportunityId :
UserId :
OpportunityAccessLevel :
TeamMemberRole :
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.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
Trigger:
for(Account a: Trigger.New){
if(a.type=='Prospect'){
Test Class :
@isTest
@isTest
static void testme(){
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
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:
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 :
Note :RowCause=Schema.CustomObject_Share.RowCause.Reason
Ex: Schema.Customer_Share.RowCause.Manual
Ex: Schema.Candidate_Share.RowCause.Owner
Example :
Object :Customer__c
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 ){
for(Customer__c c:Trigger.New){
if(c.status__c='Accepted'){
}
}
insert share;
===================================================================================
=====
11.When ever the phone no of the account is modified then set corresponding
contacts
===================================================================================
=====
class: AccountTriggerHandler :
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 :
}
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);
}
}
}
===================================================================================
=====
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
then
Class :
if(accMap.containsKey(op.accountId)){
List<Opportunity> optList=accMap.get(op.accountId);
optList.add(op);
accMap.put(op.accountId,optList);
}else{
}
}
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>();
Contact old=Trigger.oldMap.get(key);
Contact newCon=Trigger.newMap.get(key);
if(old.phone!=newCon.phone){
contacts.add(newCon);
}
update accs;
}