You need to sign in to do that
Don't have an account?
AntonPavlov
write a trigger test
This is code trriger
trigger changeFieldProduct on Product2 (after insert,after update) {
List<Task> taskList = new List<Task>();
for(Product2 prod: Trigger.New){
if(prod.IsActive__c == false){
List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id=:prod.id];
for(OpportunityLineItem opp :obj){
taskList.add(new Task(
Subject='new',
whatId = opp.OpportunityId,
Status='New',
Auto_Created__c=true,
ActivityDate = date.today,
Phone__c = opp.Opportunity.Account.Phone,
Priority = 'High'));
}
}
}
if(taskList.size()>0){
insert taskList;
}
}
Test class write only to 40% but I need get 100% help to solve
@isTest
public class changeFieldProductTest {
@isTest
public static void testing(){
List<Task> taskList = new List<Task>();
List<Product2> prod = new List<Product2>();
for(Integer i=0;i<10;i++){
Product2 p = new Product2(Name='test ' +i, IsActive__c = false);
prod.add(p);
}
Test.startTest();
insert prod;
Test.stopTest();
System.assertEquals(10, prod.size());
List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id IN:prod];
System.assertEquals(10, prod.size());
}
}
trigger changeFieldProduct on Product2 (after insert,after update) {
List<Task> taskList = new List<Task>();
for(Product2 prod: Trigger.New){
if(prod.IsActive__c == false){
List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id=:prod.id];
for(OpportunityLineItem opp :obj){
taskList.add(new Task(
Subject='new',
whatId = opp.OpportunityId,
Status='New',
Auto_Created__c=true,
ActivityDate = date.today,
Phone__c = opp.Opportunity.Account.Phone,
Priority = 'High'));
}
}
}
if(taskList.size()>0){
insert taskList;
}
}
Test class write only to 40% but I need get 100% help to solve
@isTest
public class changeFieldProductTest {
@isTest
public static void testing(){
List<Task> taskList = new List<Task>();
List<Product2> prod = new List<Product2>();
for(Integer i=0;i<10;i++){
Product2 p = new Product2(Name='test ' +i, IsActive__c = false);
prod.add(p);
}
Test.startTest();
insert prod;
Test.stopTest();
System.assertEquals(10, prod.size());
List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id IN:prod];
System.assertEquals(10, prod.size());
}
}
Please use try the below updated trigger and Test Class.
In test class if any required fields are missing then add it to create the records. Add the system asserts in test class.
Apex Trigger :
Test Class:
Thanks,
Maharajan.C
All Answers
I would suggest you to remove the soql from the for loop as it might lead to reaching of governer limit and also, I see that the for loop is not being covered because there are no opportunities that are inserted so no list of opportunity records are retrieved and the for loop is not being covered, so the changes you need to make are as follows:
>> remove the soql from inside for loop you can run the for loop by creating a map of produced and list of opportunity records and then fetching these records using get.
>> As the product records are new you need to insert respective opportunity records so as to cover the nested for loop i.e., to cover the below part:
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.
Please use try the below updated trigger and Test Class.
In test class if any required fields are missing then add it to create the records. Add the system asserts in test class.
Apex Trigger :
Test Class:
Thanks,
Maharajan.C