Apex Trigger Before Update Before Insert Example

trigger addSalesRepToLCR on LawyerConnectionRequest__c (before insert, before update) 
{       
    if (Trigger.isUpdate || Trigger.isInsert)
    {
        Set<Id> contactIds = new Set<Id>();
        for (LawyerConnectionRequest__c lcrToGetContactIds : Trigger.New)
        {
            contactIds.add(lcrToGetContactIds.Client__c);
        }

        Map<Id, Contact> mapOfUsers = new Map<Id, Contact>([select Id, Owner.Id, Owner.Profile.Name from Contact where Id In :contactIds]);
        for (LawyerConnectionRequest__c lcr : Trigger.New)
        {
            if (lcr.Client__c != null)
            {
                Contact newUser = mapOfUsers.get(lcr.Client__c);
                if (newUser.Owner.Profile.Name == 'Rocket Lawyer Sales')
                {
                    lcr.Sales_Rep__c = newUser.Owner.Id;
                }
            }
        }       
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: