function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
eric_wceric_wc 

sample trigger and test class example for admins/rookie developers

Jake Gmerek helped me here on the boards with this trigger so I wanted to post it for others to use.  As an admin that ends up doing some dev projects I offten come to the boards looking for code samples to use in my projects.  

The trigger has 2 sections, the top is commented out and needs to be rewritten to handle bulk inserts that can happen with a trigger to avoid governer limits but it does work.  My project requirements changed so I did not rewrite it I just left it there for reference.

 

The bottom is the actual trigger that updates a new invoice (custom object) with an opportuity id.

 

I have also included the Test class which has been the hardest thing to wrap my head around how to do but I am now getting the hang of it for simple unit test.

 

I hope these code samples can help other admin/rookie developers.

Thanks again to Jake for his help and answering all my questions with code examples.

Eric

 

trigger InvoiceCreated on Invoice__c (before insert) {

 //***********************************************************************************************

/*
//update opp with new invoice custom obj id
//***this code needs to be changed to move the query out of the for loop!***
Opportunity[] updateOpportunity = new Opportunity[]{};
   
    //Loop over every invoice passed in. 
    for(Invoice__c inv : Trigger.new)
    {
       string PC = inv.EPICOR_Project_Code__c;  
       System.debug('>>>>>Project code: '+PC);
       ID opp = [select ID from Opportunity where EPICOR_Project_Code__c = :PC].id;
        System.debug('>>>>>Update Opp: '+opp);
        
             //Add them to list to update. 
            Opportunity thisOpportunity = new Opportunity(ID = opp);
                thisOpportunity.InvoiceID__c = inv.ID;
                updateOpportunity.add(thisOpportunity);
                update updateOpportunity; 
       
    }
*/

//***************************************************************************************************

//insert corresponding opp id into invoice custom object
//invoice list 
Invoice__c[] updateInvoices = new Invoice__c[]{};
	//list to store all epicor project codes from invoices
	List <string> pcode = new List<string>();
	//loop throgh invoices adding the projects codes to the list above
	For (Invoice__c inv : Trigger.new){
		pcode.add(inv.EPICOR_Project_Code__c);
	}
	//list to store all the opportunitys with matching project codes
	Opportunity[] opps = [select ID, EPICOR_Project_Code__c from Opportunity where EPICOR_Project_Code__c IN :pcode];
	
	For (Opportunity o: opps){
	//Loop through invoices updating them with the opp id with matching project code
		For (Invoice__c invoice : Trigger.new){
			if (invoice.EPICOR_Project_Code__c != null)
				if (o.EPICOR_Project_Code__c == invoice.EPICOR_Project_Code__c ){
					invoice.Opportunity__c = o.Id;
					updateInvoices.add(invoice);
				}
			}
	}
//only needed if using after insert
//update updateInvoices;
}

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class TestInvoice {

    //get recordTypes
	public RecordType getRecordTypes(){		
		RecordType[] arRT = [select Id, Name from RecordType where SobjectType = :sObjectType.Opportunity.getName() limit 1];
		if(arRt.size() != 1)
			System.assertEquals(1,2, 'CommonTestUtil: CANNOT FIND RECORD TYPE!');
		
		return arRT[0];
	}
    
    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Account account = new Account (Name = 'testa');
        insert account;
        
        
        Opportunity[] opportunitys = new Opportunity[]{
        		new Opportunity (name = 'testo', 
        						account = account, 
        						EPICOR_Project_Code__c = 'TEST', 
        						stagename = 'Renew', 
        						closedate = system.today()+91),
        						
        		new Opportunity (name = 'testo2', 
        						account = account, 
        						EPICOR_Project_Code__c = 'TEST2', 
        						stagename = 'Renew', 
        						closedate = system.today()+91)
        };
        insert opportunitys;
        opportunitys[0].EPICOR_Project_Code__c = 'TEST';
        opportunitys[1].EPICOR_Project_Code__c = 'TEST2';
        update opportunitys;
        
       Opportunity opportunityTest = [Select EPICOR_Project_Code__c, stagename, closedate From Opportunity where id =: Opportunitys[0].id];
        system.debug (' Opportunity Object : '+ opportunityTest.EPICOR_Project_Code__c + opportunityTest.StageName + opportunityTest.CloseDate );  
       Opportunity opportunityTest2 = [Select EPICOR_Project_Code__c, stagename, closedate From Opportunity where id =: Opportunitys[1].id];
        system.debug (' Opportunity Object : '+ opportunityTest2.EPICOR_Project_Code__c + opportunityTest2.StageName + opportunityTest2.CloseDate );
         
        Invoice__c[] invoices = new Invoice__c[]{
        	new Invoice__c(name = 'testi', 
        			EPICOR_Project_Code__c = 'TEST', 
        			account__c = account.id
        			),
        			
        	new Invoice__c(name = 'testi2', 
        			EPICOR_Project_Code__c = 'TEST2', 
        			account__c = account.id
        			),
        			
        	new Invoice__c(name = 'testi3', 
        			EPICOR_Project_Code__c = 'TEST2', 
        			account__c = account.id
        			),
        			
        	new Invoice__c(name = 'testi4', 
        			EPICOR_Project_Code__c = 'TEST', 
        			account__c = account.id
        			),
        	new Invoice__c(name = 'testi5', 
        			//EPICOR_Project_Code__c = 'TEST', 
        			account__c = account.id
        			)
        };
        //test InvoiceCreated trigger
        insert invoices;
        
        Invoice__c invoiceTest = [Select EPICOR_Project_Code__c, name, opportunity__c From Invoice__c where id =: Invoices[0].id];
        system.debug (' Invoice Object : '+ invoiceTest.EPICOR_Project_Code__c + invoiceTest.name + invoiceTest.opportunity__c );  
       Invoice__c invoiceTest2 = [Select EPICOR_Project_Code__c, name, opportunity__c From Invoice__c where id =: Invoices[1].id];
        system.debug (' Invoice Object : '+ invoiceTest2.EPICOR_Project_Code__c + invoiceTest2.name + invoiceTest2.opportunity__c );
        Invoice__c invoiceTest3 = [Select EPICOR_Project_Code__c, name, opportunity__c From Invoice__c where id =: Invoices[2].id];
        system.debug (' Invoice Object : '+ invoiceTest3.EPICOR_Project_Code__c + invoiceTest3.name + invoiceTest3.opportunity__c );
        Invoice__c invoiceTest4 = [Select EPICOR_Project_Code__c, name, opportunity__c From Invoice__c where id =: Invoices[3].id];
        system.debug (' Invoice Object : '+ invoiceTest4.EPICOR_Project_Code__c + invoiceTest4.name + invoiceTest4.opportunity__c );
        Invoice__c invoiceTest5 = [Select EPICOR_Project_Code__c, name, opportunity__c From Invoice__c where id =: Invoices[4].id];
        system.debug (' Invoice Object : '+ invoiceTest5.EPICOR_Project_Code__c + invoiceTest5.name + invoiceTest5.opportunity__c );
        
        System.assert(invoiceTest.opportunity__c == opportunitytest.id);
        System.assert(invoiceTest2.opportunity__c == opportunitytest2.id);
        System.assert(invoiceTest3.opportunity__c == opportunitytest2.id);
        System.assert(invoiceTest4.opportunity__c == opportunitytest.id);
        System.assert(invoiceTest5.opportunity__c == null);
        
    }
}