• Mont Mont
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
I don't know, how to fix it: my messages are in List.

Helper:
public with sharing class Helper {
    
    public static void afterInsert(List<P__c> newList, Map<ID, P__c> newMap){
    
    	List<Id> userIds = new List<Id>();
        EmailTemplate emailTemplate = [SELECT Id FROM emailTemplate WHERE Name = 'Email'];
        Messaging.Email[] messages = new Messaging.Email[0];
        
        for(User u : [SELECT Id FROM User WHERE UserRole.Name = 'Manager']){
                userIds.add(u.Id);
            }
    
    	for(P__c p : newList){
            
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
  	    mail.templateId = emailTemplate.Id;
            mail.targetObjectIds = userIds;
            mail.saveAsActivity = false;
  	    messages.add(mail);
    	}
        Messaging.sendEmail(messages);
    }
Trigger:
trigger EmailTrigger on P__c (after insert) {
   
    if (Trigger.isAfter && Trigger.isInsert) {
        Helper.afterInsert(Trigger.new, Trigger.newMap);
    } 
}
Bulk-test:
@isTest
    static void sendEmailBulkTest(){
        insert createUser();

        for(Integer i=0; i<11; i++){
            insert p();
        }
        Integer emailInvocations = Limits.getEmailInvocations();
        system.assertEquals(emailInvocations, 11);
        
    }
And I get msg "System.LimitException: Too many Email Invocations: 11". How can I fix it?

 

Hello. I have problems with this fragment in my Test method:

Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        insert pb2;
        
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
         
       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        
        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here
Crush in 'insert item'. Where is a problem? I don't see it.

I can't understand, why % of my tests is the same.

My code:
 

public class Controller {
    
    public List <Wrapper> wrapperList{get;set;}
    public Boolean bool {get;set;}

 	public Controller(){
        bool = false;
 	}

    public void table(){    
        wrapperList= new list<WrapperClass>();
        
        if(bool == true){
            for(Product2 prod:[SELECT Name FROM Product2 WHERE Name LIKE 'A']){
           		wrapperList.add(new Wrapper(prod, false, 1));
        	} 
        }
}
My test for it is:
 
@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Product2 prod = new Product2(Name = 'someProduct');

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        Boolean a = true;
        cont.bool = a;

       if(a == true){
            ourList.add(new Controller.Wrapper(prod, false, 1));
            cont.wrapperList = ourList;
            cont.table();
        }
        test.stopTest();  
   }
}


But I have the same 34%, like if I make it without if. Like this:

@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        cont.wrapperList = ourList;
        cont.table();

        test.stopTest();  
   }
}
What is wrong?

I really don't understand, how to test that. Become an error.
Help me, please.

My code:

public void click(){
        
        for(WrapperClass wrp : wrapperRecordList){
            
  			if(wrp.isSelected){
                OpportunityLineItem opp = new OpportunityLineItem();
                opp.Product2Id = wrp.productRecord.Id;
                opp.Quantity = wrp.quantity;
                opp.OpportunityId = prodId;
                
                if(opp.PricebookEntryId != null){
                    opp.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].Id;
                	opp.TotalPrice = wrp.quantity * [SELECT UnitPrice FROM PricebookEntry WHERE Pricebook2Id IN (SELECT Pricebook2Id FROM Opportunity WHERE Id =: prodId) AND Product2Id =: wrp.productRecord.Id LIMIT 1].UnitPrice;
                }
                
                try {
                    insert opp; 
                } catch(Exception e) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
                }    
            } 
 		}
 	}

My incorrect test:

@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();
        
        Pricebook2 pricebook = new Pricebook2(Name = 'new prbook');
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), StageName = 'Prospecting', Pricebook2Id = pricebook.Id);
        Product2 product = new Product2(Name = 'prod');
        
        OpportunityLineItem oppLine = new OpportunityLineItem();
        oppLine.Product2Id = product.Id;
        oppLine.Quantity = 2;
        oppLine.OpportunityId = opport.Id;
        
        insert oppLine;
        
        ProductController ProductControllerObj = new ProductController();
        ProductControllerObj.click();
        test.stopTest();        
    }
}
I have a before insert, before update Trigger:
trigger OpportunityStageUpdate on Opportunity (before insert, before update) {
        
    for(Opportunity opp : trigger.new){
        if(opp.StageName == 'A'){
            opp.StageName = 'B';
        }
    }
}

But I also have a Validation Rule for StageName 'B'. Trigger can't execute. Why? Before trigger must be before Validation Rule.
Hello. I need to use checkbox like a filter for rows from my table. How to do that? For example, show only "А" names, when checkbox is true, and return all names, when it's false.

Hello. I have problems with this fragment in my Test method:

Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        insert pb2;
        
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
         
       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        
        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here
Crush in 'insert item'. Where is a problem? I don't see it.

I can't understand, why % of my tests is the same.

My code:
 

public class Controller {
    
    public List <Wrapper> wrapperList{get;set;}
    public Boolean bool {get;set;}

 	public Controller(){
        bool = false;
 	}

    public void table(){    
        wrapperList= new list<WrapperClass>();
        
        if(bool == true){
            for(Product2 prod:[SELECT Name FROM Product2 WHERE Name LIKE 'A']){
           		wrapperList.add(new Wrapper(prod, false, 1));
        	} 
        }
}
My test for it is:
 
@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Product2 prod = new Product2(Name = 'someProduct');

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        Boolean a = true;
        cont.bool = a;

       if(a == true){
            ourList.add(new Controller.Wrapper(prod, false, 1));
            cont.wrapperList = ourList;
            cont.table();
        }
        test.stopTest();  
   }
}


But I have the same 34%, like if I make it without if. Like this:

@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        cont.wrapperList = ourList;
        cont.table();

        test.stopTest();  
   }
}
What is wrong?