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
Christopher PickingChristopher Picking 

Need help converting the code in a trigger to a class.

My Trigger is a follows. I know this should properly be done by calling a method in a class via the trigger and in order to move this to production I need to have this in a class and then create the test class. I'm ultra new at all of this and really appreciate any help that you can provide. 

trigger UpdateNCMOwner on Non_Conforming_Material__c (before insert, before update) {
            for(Non_Conforming_Material__c n : Trigger.new){
                if(n.NCM_Stage__c == 'In Process'){
                   n.OwnerID = n.NCM_Supplier_Buyer_ID__c;
                }
                else if (n.NCM_Stage__c == 'Resolution'){
                       n.OwnerID = n.NCM_Created_By_ID__c;
                }
             }
}
Best Answer chosen by Christopher Picking
SarfarajSarfaraj
Hi Chris

This is the code,
trigger UpdateNCMOwner on Non_Conforming_Material__c (before insert, before update) {
	UpdateNCMOwnerHandler handler = new UpdateNCMOwnerHandler();
	handler.handle(Trigger.new);
}

public with sharing class UpdateNCMOwnerHandler
{
	public void handle(List<Non_Conforming_Material__c> nonConformingMaterials)
	{
		for(Non_Conforming_Material__c n : nonConformingMaterials){
			if(n.NCM_Stage__c == 'In Process'){
				n.OwnerID = n.NCM_Supplier_Buyer_ID__c;
			}
			else if (n.NCM_Stage__c == 'Resolution'){
				n.OwnerID = n.NCM_Created_By_ID__c;
			}
		}
	}
}

@isTest
public with sharing class UpdateNCMOwnerHandlerTest
{
	public static testMethod void testUpdateNCMOwnerHandler()
	{
		List<Non_Conforming_Material__c> nonConformingMaterials = new List<Non_Conforming_Material__c>();
	Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process'); //#1
		nonConformingMaterials.add(nonConformingMaterial);
		nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'Resolution'); //#2
		nonConformingMaterials.add(nonConformingMaterial);
		insert nonConformingMaterials;
	}
}
You may need to make minor modification to line #1 and #2. In case you have some mandatory fields in object Non_Conforming_Material__c, you have to put values for them as well. Example: if you have mandatory fields Material_Size__c and Material_UOM__c the code will be like this,

Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process', Material_Size__c = 10, Material_UOM__c = 'PTS');


All Answers

RamuRamu (Salesforce Developers) 
As far as the trigger is concerned, there is no need to write a class or method as the code is not too complex. Regarding the test class for the same. I suggest that you review the article at the link below

http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
SarfarajSarfaraj
Hi Chris

This is the code,
trigger UpdateNCMOwner on Non_Conforming_Material__c (before insert, before update) {
	UpdateNCMOwnerHandler handler = new UpdateNCMOwnerHandler();
	handler.handle(Trigger.new);
}

public with sharing class UpdateNCMOwnerHandler
{
	public void handle(List<Non_Conforming_Material__c> nonConformingMaterials)
	{
		for(Non_Conforming_Material__c n : nonConformingMaterials){
			if(n.NCM_Stage__c == 'In Process'){
				n.OwnerID = n.NCM_Supplier_Buyer_ID__c;
			}
			else if (n.NCM_Stage__c == 'Resolution'){
				n.OwnerID = n.NCM_Created_By_ID__c;
			}
		}
	}
}

@isTest
public with sharing class UpdateNCMOwnerHandlerTest
{
	public static testMethod void testUpdateNCMOwnerHandler()
	{
		List<Non_Conforming_Material__c> nonConformingMaterials = new List<Non_Conforming_Material__c>();
	Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process'); //#1
		nonConformingMaterials.add(nonConformingMaterial);
		nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'Resolution'); //#2
		nonConformingMaterials.add(nonConformingMaterial);
		insert nonConformingMaterials;
	}
}
You may need to make minor modification to line #1 and #2. In case you have some mandatory fields in object Non_Conforming_Material__c, you have to put values for them as well. Example: if you have mandatory fields Material_Size__c and Material_UOM__c the code will be like this,

Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process', Material_Size__c = 10, Material_UOM__c = 'PTS');


This was selected as the best answer