Home » , » Creating trigger to assign lead owners based on web domain

Creating trigger to assign lead owners based on web domain

Written By Venu Gutta Salesforce LWC on Sunday, 26 February 2017 | 03:41

Object   : Lead
Evernt : before Insert
Requirement : When ever new Lead is created with lead source as Web  then assign Venkatesh as owner

Trigger :
trigger OwnerAssign on Lead (before insert) {
User u=[select id from user where username='Venkatesh@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='Ram';
        my.company='Salesforce';
        my.AnnualRevenue=8000;
        my.LeadSource='Web';
        insert my;
        User u=[select id from User where username='venkatesh@dev.com'];
        Lead l=[select ownerId from Lead where id=:my.Id];
        if(my.leadSource=='Web'){
        System.assertEquals(l.ownerId,u.Id);
        }
    }
}