You need to sign in to do that
Don't have an account?
Dee Dee Aaron
Help to Create Test Class Based on my Apex Trigger
Hi there,
I have an Apex trigger, but I will need help to create the test class. Thank you for your help in Advance:
Apex Trigger
trigger FeedCommentUpdatetoNetPlanning 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 == 'GeoLinks Net Planning')
{
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 = 'Awaiting Reply from Rep';
serList.add(ser);
}
}
}
if(!serList.IsEmpty())
update serList;
}
I have an Apex trigger, but I will need help to create the test class. Thank you for your help in Advance:
Apex Trigger
trigger FeedCommentUpdatetoNetPlanning 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 == 'GeoLinks Net Planning')
{
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 = 'Awaiting Reply from Rep';
serList.add(ser);
}
}
}
if(!serList.IsEmpty())
update serList;
}
To get started, use the following code
Let me know if it gives you any coverage
All Answers
I see that you are using profile id so I think you can make use of run as in test class to run the test as specific user, as mentioned in the documentation:
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm
" The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced."
An example is present in documentation which you can try checking.
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.
To get started, use the following code
Let me know if it gives you any coverage