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
Joe MaurasJoe Mauras 

Test Class Help For Concatenate Trigger

Hello all. I'm as new as it comes to Apex. I'm on Trailhead learning about it but I could use help with the Test code to get the ball rolling. My trigger (so far) simply concatenates a bunch of fields based on another field. I would appreciate any help. Thank you.
trigger Item_Desc_Trigger on Item_Creation__c (before insert, before update){


for (Item_Creation__c obj: trigger.new){

    obj.Item_Description__c = '';
    obj.Extended_Description__c = '';

    if ( obj.Item_Description_Code__c == 'ORAS' )
        {
            obj.Item_Description__c = obj.Default_Product_Group_Value__c + '-' + obj.Subcategory_Group_Code__c + ' ' + obj.O_ring_Size_Value__c + ' ' + obj.Material_Family_Value__c + ' ' + obj.Compound_Name_Value__c;
            
            obj.Extended_Description__c = obj.Product_Group_Value__c + ' ' + obj.Subcategory_Group_Value__c + '\n' + 
                obj.Material_Family_Value__c + ' ' + obj.Durometer__c + ' ' + obj.Color__c + ', RoHS / Reach, AMS 83485' + '\n' +  // FIX THIS***
                obj.Nominal_ID_Value__c + '"ID x ' + obj.Nominal_CS_Value__c + '"CS Nom. (' + obj.ID__c + '"ID x ' + obj.CS__c + '"CS)' + '\n' +
                'Tolerances: ' + obj.Subcategory_Group_Value__c;
        }

    else if ( obj.Item_Description_Code__c == 'ORMM' )
        {
            obj.Item_Description__c = obj.Default_Product_Group_Value__c + '-' + obj.Subcategory_Group_Code__c;
            
            obj.Extended_Description__c = obj.Product_Group_Value__c + ' - ' + 'Metric' + '\n' +
                obj.Material_Family_Value__c + ' ' + obj.Durometer__c + ' ' + obj.Color__c + ', RoHS / Reach' + '\n' +  // FIX THIS***
                'Tolerances: ';        
        }

 
Best Answer chosen by Joe Mauras
Rishabh Goyal 18Rishabh Goyal 18
Hi Joe, 
 
Your test class would look somewhat like this - 

@isTest
public class ItemDesctriggerTest {
    
    static testMethod void testMethod() {
    Item_Creation__c itc = new Item_Creation__c();
    itc.Item_Description_Code__c = 'ORAS';
    itc.Default_Product_Group_Value__c = 'test2';
    itc.Subcategory_Group_Code__c = 'test3';.....
   ..
  ..
  ..
   // like wise do add for all the fields used in string concatenation
  // then insert thid record as below the trigger will be called automatically
insert itc;
// with the above code if first if condition will be covered 
// to cover the else part you would require to insert this record once again to satisfy the else condition
}
}

----------------------------------------
hope this code helps you , do let me know if you require more help in it!
 

All Answers

Rishabh Goyal 18Rishabh Goyal 18
Hi Joe, 
 
Your test class would look somewhat like this - 

@isTest
public class ItemDesctriggerTest {
    
    static testMethod void testMethod() {
    Item_Creation__c itc = new Item_Creation__c();
    itc.Item_Description_Code__c = 'ORAS';
    itc.Default_Product_Group_Value__c = 'test2';
    itc.Subcategory_Group_Code__c = 'test3';.....
   ..
  ..
  ..
   // like wise do add for all the fields used in string concatenation
  // then insert thid record as below the trigger will be called automatically
insert itc;
// with the above code if first if condition will be covered 
// to cover the else part you would require to insert this record once again to satisfy the else condition
}
}

----------------------------------------
hope this code helps you , do let me know if you require more help in it!
 
This was selected as the best answer
Joe MaurasJoe Mauras
Hi Rishabh,
Thank you for that. That is very helpful and makes sense. However I'm getting an error that the "Field is not writable". I'm assuming it's because the fields we're writing to are actually Formulas. The formulas are pulling information from a child object if that helps. For example the "Subcategory_Group_Code__c" is generated after the user selects a "Subcategory Group". The "Subcategory_Group_Code__c" is then pulled from that child object. 

Do you know of a way around that?

Thanks again for your help. 
Joe MaurasJoe Mauras
Hi Rishabh,
I think I figured out the problem. In my test class, using your example, I generated the fields that generate the formulas. That gave me the coverage I needed. I really appreciate your help!