• Joe Rodden 7
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
Hi, 

The class below is supposed to fire an error message if the Total of Sales Invoices goes over the opportunity Amount of the opp they're associated to. For some reason it's just doing nothing and I've been wracking my brain to figure out why. I'm fairly new to development so I'm sure it's something basic. 

Trigger:
trigger MasterSalesInvoiceTrigger on Sales_Invoice__c (
  before insert, after insert, 
  before update, after update, 
  before delete, after delete,
  after undelete) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

        SalesInvoice_InvoiceTotalValidation.siTotalValidation(Trigger.new, Trigger.oldMap);
    } 
    if (Trigger.isUpdate) {
		SalesInvoice_InvoiceTotalValidation.siTotalValidation(Trigger.new, Trigger.oldMap);

            }
    if (Trigger.isDelete) {

    }
  }

  if (Trigger.IsAfter) {
    if (Trigger.isInsert) {

    } 
    if (Trigger.isUpdate) {

    }
    if (Trigger.isDelete) {

    }
    if (Trigger.isUndelete){

      }
  }
}



Class:
public class SalesInvoice_InvoiceTotalValidation {

    public static void siTotalValidation(List<Sales_Invoice__c> trigSi, Map<Id, Sales_Invoice__c> oldSIMap) {
        Map<Id, Decimal> siTotal   							= new Map<Id, Decimal>(); //Holds the sum of all inserted and updated sales invoices
        Map<Id, Decimal> oppAmount 							= new Map<Id, Decimal>(); //Hold the current amount for the opportunity
        Map<Id, List<Sales_Invoice__c>> oppIdToSalesInvoice = new Map<Id, List<Sales_Invoice__c>>(); //Maps the opp Id to the Sales Invoices for the error message
        Set<Id> oppIdSet		  						    = new Set<Id>(); //Used to create the key for siTotal and oppAmount when calling error
        
        //Filling up maps
        for (Sales_Invoice__c si : trigSi) {
            if (siTotal.containsKey(si.Opportunity__c)) {
                siTotal.put(si.Opportunity__c, si.Total__c + siTotal.get(si.Opportunity__c));
            } else {
                siTotal.put(si.Opportunity__c, si.Total__c);
            } 
            
            if(oppIdToSalesInvoice.containsKey(si.Opportunity__c)) {
                oppIdToSalesInvoice.get(si.Opportunity__c).add(si);
            } else {
                oppIdToSalesInvoice.put(si.Opportunity__c, new List<Sales_Invoice__c>{si});
            }
            
            oppAmount.put(si.Opportunity__c, si.Opportunity__r.Amount);
		}

	      
        oppIdSet = siTotal.keySet();
        for(Id oppId  : oppIdSet) {
            if(siTotal.get(oppId) > oppAmount.get(oppId)){
                for(Sales_Invoice__c si2 : oppIdToSalesInvoice.get(oppId)) {
                    si2.addError('Total of all Sales Invoices cant be greater than opportunity amount. Adjust opp amount first then adjust the Sales Invoices.');
                }
            }
        }
        
        
        
    }
}

 
Fairly new so bear with me. I've written a class that throws an error message for any duplicate emails on Contacts. While writing the test I thought I'd covered everything the original class does however I'm only ending up with 85% code coverage. I'd like to figure out how to get to 100. 

Class (the lines that aren't covered are bolded and underlined):
 
public class Contact_CheckEmail {

    List<Contact> contacts = new List<Contact>();
    
    public Contact_CheckEmail(List<Contact> trigCon){
       contacts = trigCon; 
    }
    

    public void checkEmail(){
    Set<String> emailSet = new Set<String>();
    
    for(Contact c : contacts){
        
        emailSet.add(c.Email);
    
    }
    
    List<Contact> duplicateEmail = [SELECT Email FROM Contact WHERE Email IN: emailSet];
    
    Set<String> duplicateEmailId = new Set<String>();
    
    for(Contact c : duplicateEmail) {
    
        duplicateEmailId.add(c.Email);
    
    }

    for(Contact c: contacts){
        if(duplicateEmailId.contains(c.Email)){
               c.addError('Email already exists.');
        
        }
    
    }
    
}
}


Test Class:
@isTest
public class Test_CheckEmail {
    
    @isTest static void EmailCheckTest(){
        
        Account acc = new Account();
        acc.Name = 'TestAccount';
        insert acc;
        
        List<Contact> contacts = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = 'fakeman@fake.com';
            contacts.add(myCon);
        }
        
        try{
        insert contacts;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, true);
        }
        
        List<Contact> contacts2 = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = iString + '@fake.com';
            contacts2.add(myCon);
        }
        
        try{
        insert contacts2;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, false);
        }
        
        
        
    }

}