+ Start a Discussion
Dee Dee AaronDee Dee Aaron 

Help with test class - where to insert required fields?

Hi. With the help of another user on the forum, I was able to use a sample test class. I just don't know how or where to add the required fields?

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 see another area in the test class that says "insert f". I'm not sure what I need to insert in that spot?

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();
        }
        
        
        SALES_ENGINEERING_REQUEST__c updatedRecord = [SELECT Id,Status__c  FROM SALES_ENGINEERING_REQUEST__c WHERE Id =: s.Id];
        System.assertequals('Approved',updatedRecord.Status__c);
    }
    
}

Thank you for your help!
Best Answer chosen by Dee Dee Aaron
veer soniveer soni
Hi Dee,
try this following Test Class Dummy code.
@isTest
public class insertUserRecTest {
@isTest
    
    public static void testFeedCommentTrigger() {
        
        //Create a User with Profile 'Net Planning Department'
        Id pid = [Select Id,Name FROM Profile WHERE Name like 'System Administrator'].Id;
        
       User u = new User(Alias = 'Dsystem', Email= pid + '1@myorg.com', 
          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
          LocaleSidKey='en_US', ProfileId =pid, 
          TimeZoneSidKey='America/New_York', UserName= 'Dummycommunity@gmail.com' + '1@myorg.com');
        insert u;
        system.debug('u==>' + u);
       
        SALES_ENGINEERING_REQUEST__c s = new SALES_ENGINEERING_REQUEST__c();
        s.Status__c = 'Approved';//any Value othe than "Approved" and "Unable to Meet Request"
        s.Name = 'testRec';//if in this object Layer__c  filed is required then fill up like this {s.Layer__c = 'Layer 2'}
       //set here any required filed of this SALES_ENGINEERING_REQUEST__c object 
        //for Example (in this Object if Name filed is required then set that like above fields {s.Name = 'test';} )
        
        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
       //Parent Record
Account acc = new Account(Name = 'Test Account');
insert acc;
 
//Create Related Feed Item Record
FeedItem fi = new FeedItem(ParentId = acc.Id, Body = 'Test Body');
insert fi;
 
//Create Feed Comment Record
FeedComment fc = new FeedComment(FeedItemId = fi.Id, CommentBody = 'Test Comment');
insert fc;
           
        }
        
        
        SALES_ENGINEERING_REQUEST__c updatedRecord = [SELECT Id,Status__c  FROM SALES_ENGINEERING_REQUEST__c WHERE Id =: s.Id];
        System.assertequals('Approved',updatedRecord.Status__c);
    }
    

I don't know that Layer__c this filed is of which record so you check and set this filed.
let me know, if it's helps you and close your query as best mark of this.
Thank You