• Steve Sumner
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi,
 
Can someone explain to me how to write a test class for an apex trigger like the following one ? This trigger assigns new Leads in Salesforce to the existing Account owner if the domain email address matches the Account's domain.

I'm new to writing Apex but it is working in the Sandbox. I just need a test class to be able to deploy it to Production.


Thanks 

 
trigger addAccount on Lead (before insert){

    //Use a set instead of a list to gather all of the unique domains
    Set<String> leadDomains = new Set<String>();

    //Loop through leads to populate the set
    for(Lead l : trigger.new){
        leadDomains.add(l.domain__c);
    }

    //Query for accounts
    List<Account> leadAccounts = [Select Id, OwnerId, domain__c From Account Where domain__c = :leadDomains];

    //Create Map and loop through list to populate the map
    Map<String,Account> accMap = new Map<String,Account>();
    for(Account a : leadAccounts){
        accMap.put(a.domain__c,a);
    }

    //Loop through leads and assign correct owner from Map
    for(Lead l2 : trigger.new){
        if(accMap.containsKey(l2.domain__c)){
            l2.OwnerId = accMap.get(l2.domain__c).OwnerId;
            l2.Existing_Account__c = true;
            
         
        }
        else{
           
        }
    }


}

 
Hi,
 
Can someone explain to me how to write a test class for an apex trigger like the following one ? This trigger assigns new Leads in Salesforce to the existing Account owner if the domain email address matches the Account's domain.

I'm new to writing Apex but it is working in the Sandbox. I just need a test class to be able to deploy it to Production.


Thanks 

 
trigger addAccount on Lead (before insert){

    //Use a set instead of a list to gather all of the unique domains
    Set<String> leadDomains = new Set<String>();

    //Loop through leads to populate the set
    for(Lead l : trigger.new){
        leadDomains.add(l.domain__c);
    }

    //Query for accounts
    List<Account> leadAccounts = [Select Id, OwnerId, domain__c From Account Where domain__c = :leadDomains];

    //Create Map and loop through list to populate the map
    Map<String,Account> accMap = new Map<String,Account>();
    for(Account a : leadAccounts){
        accMap.put(a.domain__c,a);
    }

    //Loop through leads and assign correct owner from Map
    for(Lead l2 : trigger.new){
        if(accMap.containsKey(l2.domain__c)){
            l2.OwnerId = accMap.get(l2.domain__c).OwnerId;
            l2.Existing_Account__c = true;
            
         
        }
        else{
           
        }
    }


}