You need to sign in to do that
Don't have an account?
Dee Dee Aaron
Help to create a test class
Hi there,
I have an Apex class that works, but I need to create a test class for it. I'm not a developer and don't know how, so I sincerely appreciate your help. Thank you.
Apex Trigger:
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;
}
I have an Apex class that works, but I need to create a test class for it. I'm not a developer and don't know how, so I sincerely appreciate your help. Thank you.
Apex Trigger:
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;
}
All Answers
//here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record insert s;
I only have 2 required fields on this object
Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The options are "Layer 2" or "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.
Wherever I have wriiten "here fill all the required fields" , needs a modification there . For Ex : As you said that
Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The options are "Layer 2" or "Layer 3"
are the required fields of SALES_ENGINEERING_REQUEST__c , then before "insert s " line you need to populate these fields as follows:
s.Name = 'Test SER 1';
s.Layer__c = 'Layer 2';
Similiarly populate all the required fields for User and FeedComment Object here.
*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) - Yes the Validation Rule will apply in the test class as well . So , you need to insert them in such a way that it does not hit the Validation Rule.