Trigger Scenario-Based Questions in Salesforce

Jagdish
0
Trigger Scenario-Based Questions in Salesforce



Scenario 1.  Create a trigger on the contact object to make the email and phone fields mandatory upon creating or updating a record.

Ans-
        ContactTrigger.apxt
    
        trigger ContactTrigger on Contact(before insert,before update)
        {
        if(Trigger.isInsert || Trigger.isUpdate && Trigger.isBefore)
        {
        ContactTriggerHandler.createdRecord(Trigger.New);
        }
        }

        ContactTriggerHandler.apxc

        public class ContactTriggerHandler {
public static void createdRecord(List<Contact> conList)
{
for(contact con : conList)
{
if(con.Email==null || con.Email==' ')
{
con.Email.AddError('Please Enter Your Email Id');
}
else if(con.Phone==null || con.Phone=='')
{
con.Phone.AddError('Please Enter Phone Number');
}
}
}
}

open an anonymous window and write -

    Contact con=new Contact(FirstName='Test', LastName='Test1');
    insert con;




Scenario 2. Create a trigger on the account object to assign the annual revenue value as below:

                            Industry name                 Annual revenue
                                Banking                500000
                                Finance                        400000
                                Insurance                300000
                                Healthcare                200000
                                   else                100000

Ans - 
            AnnualRevenue.apxt

Trigger AnnualRevenue on Account( before insert, before update)
{
    if(( Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore )
    {
        AnnualRevenueHandler.checkAnnualRevenue(Trigger.New);
    }
}

            AnnualRevenueHandler.apxc

public class  AnnualRevenueHandler {
    public static void checkAnnualRevenue(List<Account> accList)
    {
        for(Account acc : accList)
        {
            if( acc.Industry=='Banking')
                acc.AnnualRevenue=500000;
            else if(acc.Industry=='Finance')
                acc.AnnualRevenue=400000;
            else if(acc.Industry=='Insurance')
                acc.AnnualRevenue=300000;
            else if(acc.Industry=='Healthcare')
                acc.AnnualRevenue=200000;
            else
                acc.AnnualRevenue=100000;
          }
       }
}

open an anonymous window and write -

Account acc=new Account(Name='Test', Industry='Finance');
insert acc;

Scenario 3. When an account is inserted, write a trigger on the Account object; the billing address should automatically populate into the account shipping address.

Ans - 
            ShippingAddress.apxt

trigger ShippingAddress on Account( before insert, before update )
{
        if(( Trigger.isInsert || Trigger.isUpdate ) && Trigger.isBefore )
        {
            ShippingAddressHandler.autoPopulateAddress(Trigger.New);
        }
}

            ShippingAddressHandler.apxc

public class ShippingAddressHandler {
    public static void autoPopulateAddress(List<Account> accList)
    {
        for(Account acc : accList)
        {
            if(acc.BillingStreet ! = null)
            {
                acc.ShippingStreet=acc.BillingStreet;
            }
            if(acc.BillingCity ! = null)
            {
                acc.ShippingCity=acc.BillingCity;
            }
            if(acc.BillingState ! = null)
            {
                acc.ShippingState=acc.BillingState;
            }
            if(acc.BillingPostalCode ! = null)
            {
                acc.ShippingPostalCode=acc.BillingPostalCode;
             }
             if(acc.BillingCountry ! = null)
            {
                acc.ShippingCountry=acc.BillingCountry;
            }
        }
    }  
}

open an anonymous window and write -

  Account acc=new Account(Name='Guest User', Phone='1234567890', BillingStreet='102,Sai Complex', BillingCity='Pune', BillingState='Maharashtra', BillingPostalCode='411052', BillingCountry='India');
     insert acc;





Scenario 4. When the case is created with origin as email, set status as new and priority as medium.

Ans -
        CaseOrigin.apxt

    trigger CaseOrigin on Case(before insert)
    {
        if( Trigger.isInsert && Trigger.isBefore )
        {
            CaseOriginHandler.caseCreatedOrigin(Trigger.New);
        }
    }

        CaseOriginHandler.apxc
     
    public class CaseOriginHandler {
        public static void caseCreatedOrigin( List<Case> caseList )
        {
            for( case cs : caseList )
            {
                if( cs.origin == 'Email' )
                {
                    cs.Status = 'New';
                    cs.Priority = 'Medium';
                }
             }
         }
}

open an anonymous window and write -

Case cs = new Case( Origin='Email' );
insert cs;

Scenario 5. When the Lead is created with Lead Source as Web then give rating as Cold otherwise Hot.

Ans- 
        LeadCreation.apxt

trigger LeadCreation on Lead( before insert )
{
    if( Trigger.isInsert && Trigger.isBefore )
    {
        LeadCreationHandler.leadCreation( Trigger.New );
    }
}

         LeadCreationHandler.apxc

public class  LeadCreationHandler {
    public static void leadCreation(List<Lead> leadList)
    {
        for( Lead le : leadList )
        {
            if( le.leadSource == 'Web' )
            {
                le.Rating = 'Cold';
            }
            else {
                le.Rating= 'Hot';
            }
        }
    }
}

open an anonymous window and write -

Lead le = new Lead( FirstName='Guest', LastName='User', Company='ABC Pvt. Ltd', LeadSource='Web' );
insert le;

Scenario 6. Whenever a new account record is created, it must automatically be associated with a contact record.

Ans- 
        AccountAfterCreate.apxt

    trigger AccountAfterCreate on Account( before insert ) {
        if( Trigger.isInsert && Trigger.isAfter )
        {
            AccountAfterCreateHandler.accountToContactRecord( Trigger.New );
        }
   }

        AccountAfterCreateHandler.apxc

    public static void AccountAfterCreateHandler {
        public static void accountToContactRecord( List<Account> accList )
        {
            List<Contact> cons = new List<Contact>();
            for( Account acc : accList )
            {
                Contact c =new Contact();
                c.accountid=acc.id;
                c.lastname=acc.name;
                c.phone=acc.phone;
                cons.add( c );
             }
                insert cons;
         }
    }

open an anonymous window and write -

Account acc = new Account( Name = ' Test', Phone='1234567890');
insert acc;

Scenario 7. When the account is created with industry as banking, create a contact for the account, using contact lastname as account name and contact phone as account phone.

Ans- 
        CreateAccountContact.apxt

    trigger CreateAccountContact on Account( before insert )
    {
        if( Trigger.isInsert && Trigger.isBefore )
        {
            CreateAccountContactHandler.accountContactRecord( Trigger.New );
        }
    }

        CreateAccountContactHandler.apxc

    public class CreateAccountContactHandler {
        public static void accountContactRecord(List<Account> accList )
        {
            List<Contact> conList = new List<Contact>();
            for( Account acc : accList )
            {
                if( acc.Industry == 'Banking' )
               {
                    Contact con = new Contact();
                    con.LastName = acc.name;
                    con.Phone = acc.Phone;
                    con.AccountId = acc.Id;
                    conList.add(con);
                }
            }
                insert conList;
        }
    }


Scenario 8. Write a trigger on the Account object, and when the Account is updated, check all the opportunities related to the Account. Update all opportunities stages to "closed lost" if an opportunity's creation date is greater than 30 days from today and staged is not equal to closed won.

Ans-
        StageUpdate.apxt

trigger StageUpdate on Account(after update)
{
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        StageUpdateHandler.createOppRecord(Trigger.New);
    }
}

        StageUpdateHandler.apxc

public class StageUpdateHandler {
    public static void createOppRecord(List<Account> accList)
    {
        Set<Id> accountIds=new Set<Id>();
        for(Account acc:accList)
        {
            accountIds.add(acc.Id);
        }
    DateTime day30=System.now()-30;
    List<Opportunity> oppListToUpdate=new List<Opportunity>();
    List<Opportunity> oppList=[Select Id, AccountId, StageName, CreatedDate, CloseDate from Opportunity Where AccountId in:accountIds];
        if( oppList.size()>0 )
        {
            for (Opportunity opp:oppList )
            {
                if( opp. CreatedDate < day30 && opp.StageName !='Closed Won' )
                {
                    opp.StageName='Closed Lost';
                    opp.CloseDate=System.today();
                    oppListToUpdate.add(opp);
                 }
            }
        }
            if( oppListToUpdate.size() > 0 )     
            {
                update oppListToUpdate;
            }
    }
}






Post a Comment

0Comments
Post a Comment (0)
To Top