+ Start a Discussion
Dee Dee AaronDee Dee Aaron 

Help to fill in 2 required fields on test class

Hi. With the help of another user on the forum, I was able to use a sample test class. I just don't know what format is applied when inserting the required fields.

My oringal Apex Trigger: (for reference if needed)
trigger FeedCommentTest on FeedComment (after insert) 
{
    Id profileId = UserInfo.getProfileId();
    String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
    Set<id> SalesEngineeringSet = new Set<Id>();
    List<SALES_ENGINEERING_REQUEST__c> serList = new List<SALES_ENGINEERING_REQUEST__c>();
    for(FeedComment f : Trigger.New)
    {
        if(profileName  == 'Net Planning Department')
        {
            SalesEngineeringSet.add(f.ParentId);
        }          
    }
    
    if(!SalesEngineeringSet.IsEmpty()){
        for(SALES_ENGINEERING_REQUEST__c ser : [SELECT ID,Status__c FROM SALES_ENGINEERING_REQUEST__c WHERE ID In: SalesEngineeringSet]){
            if(ser.Status__c != 'Approved' && ser.Status__c != 'Unable to Meet Request'){
                ser.Status__c = 'Approved';
                serList.add(ser);
            }
        }
    }
    
    if(!serList.IsEmpty())
        update serList;
}

The test class that was created by another member:
@isTest
public class FeedCommentTriggerTest {
    
    @isTest
    public static void testFeedCommentTrigger() {
        
        //Create a User with Profile 'Net Planning Department'
        Id pid = [Select Id FROM Profile WHERE Name = 'Net Planning Department'].Id;
        
        User u = new User();
        u.ProfileId = pid;
        //here fill all the required fields for the User Record
        insert u;
        
        SALES_ENGINEERING_REQUEST__c s = new SALES_ENGINEERING_REQUEST__c();
        s.Status__c = //any Value othe than "Approved" and "Unable to Meet Request"
        //here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record
        insert s;
        
        //Since test class runs in the current user mode and in this Trigger lines will be covered only when the    //User has a Profile - 'Net Planning Department' hence we need trigger this event as the new User created with this //Profile
        System.runAs(u) {
            FeedComment f = new FeedComment();
            f.ParentId = s.Id;
            //here fill all the required fields for FeedCommentRecord
            
            Test.startTest();
            insert f;
            Test.stopTest();
        }
        
        
        SALES_ENGINEERING_REQUEST__c updatedRecord = [SELECT Id,Status__c  FROM SALES_ENGINEERING_REQUEST__c WHERE Id =: s.Id];
        System.assertequals('Approved',updatedRecord.Status__c);
    }
    
}

The two required fields that need to be inserted:
Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The 2 options are "Layer 2" "Layer 3"

Can you please insert them? I'm not sure how they need to be formatted.

*Question: I have some validation rules on this object. Will that cause any issues? (E.g. IF this field is blank then this other field is required). Thank you for your help.

*I see another area in the test class that says "insert f". I'm not sure what I need to insert in that spot?

Thank you for your help!
Best Answer chosen by Dee Dee Aaron
ANUTEJANUTEJ (Salesforce Developers) 
Hi Dee,

You need to insert a record with the values of the fields name and layer__c to cover these lines in apex class.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Dee,

You need to insert a record with the values of the fields name and layer__c to cover these lines in apex class.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Dee Dee AaronDee Dee Aaron
I am asking you to insert the values in the correct format. As I said in my question. The two fields are:
Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The 2 options are "Layer 2" "Layer 3"

Can you please insert them? I'm not sure how they need to be formatted.



 
Dee Dee AaronDee Dee Aaron
Hi Anutej.
Here are the two required fields that need to be inserted:
Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The 2 options are "Layer 2" "Layer 3"

Can you please insert them for me? I'm not sure where or how to insert them in the test class below. I also don't know what needs to be inserted where it says: Insert f

TEST CLASS
@isTest
public class FeedCommentTriggerTest {
    
    @isTest
    public static void testFeedCommentTrigger() {
        
        //Create a User with Profile 'Net Planning Department'
        Id pid = [Select Id FROM Profile WHERE Name = 'Net Planning Department'].Id;
        
        User u = new User();
        u.ProfileId = pid;
        //here fill all the required fields for the User Record
        insert u;
        
        SALES_ENGINEERING_REQUEST__c s = new SALES_ENGINEERING_REQUEST__c();
        s.Status__c = //any Value othe than "Approved" and "Unable to Meet Request"
        //here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record
        insert s;
        
        //Since test class runs in the current user mode and in this Trigger lines will be covered only when the    //User has a Profile - 'Net Planning Department' hence we need trigger this event as the new User created with this //Profile
        System.runAs(u) {
            FeedComment f = new FeedComment();
            f.ParentId = s.Id;
            //here fill all the required fields for FeedCommentRecord
            
            Test.startTest();
            insert f;
            Test.stopTest();
        }