function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MontTwo MontTwoMontTwo MontTwo 

Test Roll-Up Summary field with MIN Decimal for Data calculating

Hello. I tried everything in this test. I really don't know, how to do that right.

I have 3 Objects:

- A has a Number (Decimal) field.
- B has a Lookup field to A, Master-Detail to C and Number field for Number from A (I used Process Builder to get the Number).
- C has Roll-Up Summary: it calculates MIN value from all Number fields from B with C in Master-Detail.

My code:

public class Helper {

    public static void upd(Map<Id, Case> cases){  //Case is C-object
        
        List<Case> newCases = new List<Case>();
        
        for(Case caseOld : cases.values()){
            Date date = Date.today().addMonths(Integer.valueOf(caseOld.Min__c));  //Min__c is Roll-Up Summary field
            Case caseNew = new Case(Origin = 'Web', Status = 'New', Date_Due__c = date);
            newCases.add(caseNew);
        }
        insert newCases;
    }
}
My Test fragment:
List<Case> newCList = new List<Case>();

Case cs = [SELECT Min__c FROM Case];
        
        for(Integer i=1; i<20; i++){
            Date date = Date.today().addMonths(Integer.valueOf(cs.Min__c));
            Case c = new Case(Origin = 'Web', Status = 'New', Date_Due__c = date);
            newCList .add(c);
        } 
        insert newCList;
        System.assertEquals(20, newCList.size());
I used everything: adding in test objects A and B, inserting and this:
Case cs1 = new Case(Origin = 'Web', Status = 'New');
insert cs1;
Case cs = [SELECT Min__c FROM Case WHERE Status = 'New'];
        
        for(Integer i=1; i<20; i++){
            Date date = Date.today().addMonths(Integer.valueOf(cs.Min__c));
            Case c = new Case(Origin = 'Web', Status = 'New', Date_Due__c = date);
            newCList .add(c);
        } 
        insert newCList;
        System.assertEquals(20, newCList.size());
Roll-Up Summary isn't writable.

I become Errors like "Argument can not be null", "List has no rows" or "Update failed"... What can I do with that?
Best Answer chosen by MontTwo MontTwo
MontTwo MontTwoMontTwo MontTwo

I solved a problem :)

Product2 pr = new Product2(Name = 'product', isActive = true, Number__c = 2);
insert pr;

Case c1 = new Case(Origin = 'Web', Status = '12345');
insert testCase;

CustomObj_c obj = new CustomObj_c(C_c = c1.Id, A_c = pr.Id);
insert obj;

Case c2 = [SELECT Min__c FROM Case WHERE Status = '12345'];

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Have you tried something like this?

<pre>
    Case testCase = new Case();
    insert testCase;
    ObjectB__c testB = new ObjectB__c( Number__c = 1, Case__c = testCase.Id );
    insert testB;
    Case cs = [SELECT Min__c FROM Case WHERE Id = :testCase.Id];
    //  cs.Min__c should now equal 1
</pre>
MontTwo MontTwoMontTwo MontTwo
Object A is Product2, B - CustomObj, C - Case. So Case cs = [SELECT Min__c FROM Case WHERE Id = :testCase.Id]; doesn't work. Also it's relationship many o many through Obj B.
MontTwo MontTwoMontTwo MontTwo

I solved a problem :)

Product2 pr = new Product2(Name = 'product', isActive = true, Number__c = 2);
insert pr;

Case c1 = new Case(Origin = 'Web', Status = '12345');
insert testCase;

CustomObj_c obj = new CustomObj_c(C_c = c1.Id, A_c = pr.Id);
insert obj;

Case c2 = [SELECT Min__c FROM Case WHERE Status = '12345'];
This was selected as the best answer