• MontTwo MontTwo
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
I have 100% coverage, but I still haven't scenarios for check. How to write good and bad scenarios for test class?
This is my helper:
public with sharing class JobHelper {

public static List<Task> taskList = new List<Task>();

public static void afterInsert(List<Job__c> newList) {

    for(Job__c job : newList){ 
        if(job.Stage__c == 'Closed'){
            createTask(job.OwnerId, 'Closed');
        }
        if(job.Stage__c == 'Open'){
            createTask(job.OwnerId, 'Open');
        }
    }
    insertTask(newList);
}

public static void afterUpdate(List<Job__c> newList, Map<ID, Job__c> oldMap) {

    for(Job__c job : newList){ 
        if(job.Stage__c == 'Closed' && oldMap.get(job.Id).Stage__c != 'Closed'){
            createTask(job.OwnerId, 'Closed');
        }
        if(job.Stage__c == 'Open' && oldMap.get(job.Id).Stage__c != 'Open'){
            createTask(job.OwnerId, 'Open');
        }
    }
    insert taskList;
}

public static List<Task> createTask(Id ownId, String subj){
    taskList.add(new Task(OwnerId = ownId, Subject = subj));
    return taskList;
}
}
Trigger:
trigger JobTrigger on Job__c (after insert, after update) {

if(Trigger.isAfter && Trigger.isInsert) {
    JobHelper.afterInsert(Trigger.new);
}
else if (Trigger.isAfter && Trigger.isUpdate) {
    JobHelper.afterUpdate(Trigger.new, Trigger.oldMap);
}
}
My test-try:
@isTest
private class JobTest {


public static List<Job__c> jobList = new List<Jobn__c>();

static testMethod void createTaskTest(){

    Job__c job1 = new Job__c(
        Stage__c = 'Closed',
    );
    jobList.add(job1);

    Job__c job2 = new Job__c(
        Stage__c = 'Open',
    );
    jobList.add(job2);

    insert jobList;

    List<Task> taskListTest = new List<Task>();
    Task task1 = new Task(Subject = 'Closed');
    taskListTest.add(task1);

    Task task2 = new Task(Subject = 'Open');
    taskListTest.add(task2);

    insert taskListTest;

    JobHelper j = new JobHelper();
    //System.assert(taskListTest == j.taskList);
}
}

 

"Upsert" just inserts new records from JSON, but not updates existing.

It's my code:

public class myClass {
    
    @future (callout = true)
    public static void getCallout(){

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.myjson.com/bins/m56qw');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        if (response.getStatusCode() == 200){
        	List<Object> jsonList = (List<Object>) JSON.deserializeUntyped(response.getBody());
            List<Product2> productList = new List<Product2>();
            
            for(Object o : jsonList){
                Map<String, Object> results = (Map<String, Object>) o;
                Product2 prod = new Product2();
                prod.Cost__c = Integer.valueOf(results.get('cost'));
                prod.Warehouse_Key__c = String.valueOf(results.get('sku'));  //My key to check a record
                productList.add(prod);  
            }
            if(productList != null){
                upsert productList;  //It just inserted, but not update existing records if I change them
            }
        }
    }
}
What am I doing wrong?
 

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?
Hello. I have over 200 records, and I need to insert 2 lists. Work's records are created too fast and can't take a customField from still nott inserted Case. If insert 1 per time - Error: Too many DML statements: 151.

My code:
public class Helper {

    public static void upd(Map<Id, Case> cases){
        
        List<Case> newCases = new List<Case>();
        List<Work__c> wp = new List<Work__c>();
        
        for(Case c1 : cases.values()){
            Case c2 = new Case(Origin = 'Web', Status = 'New');
            newCases.add(c2);
            
            for(Work__c w1 : [SELECT Id, Quantity__c, Custom_Field__c FROM Work__c WHERE Custom_Field__c =: c1.Id]){
                Work__c w2 = new Work__c (Quantity__c = w1.Quantity__c, Custom_Field__c = c2.Id);
                wp.add(w2);
            }  
        }
        insert newCases;
        insert wp;
    }
}

 
Hello, Please, don't ask me "Why? You can use 200 and that's enough!". I have a task with the requirement to test inserting/importing 300 case's records from my Trigger class. Trigger creates new cases and replaces already closed. Is it possibly to do? If yes, what kind of tool can I use? I know about Batch Apex, but don't know if it can update 300 records per 1 time.
Hello. I have a problem with one code's part.

Controller (a part):
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;
                try{
                    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;                    
                    insert opp;                
                }catch(Exception e) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
                }     
            } 
         }
     }
Test (a part):
Account acc = new Account(Name ='Test acc');
		insert acc;
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;   
        PriceBook2 pb2 = new PriceBook2(Name = 'Test pb', IsActive = true);
        insert pb2;
        Opportunity opport = new Opportunity(Name = 'Test opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
	    PricebookEntry standardpbe = new PricebookEntry(Pricebook2Id = test.getstandardpricebookid(), Product2Id = prod.Id, UnitPrice = 0.0, IsActive = true);
        insert standardpbe;
       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        insert pbe;               

        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = quantity;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        insert item;
        
        prController.click();
And I have that:
User-added image
I do something wrong?

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?
Hello. I have over 200 records, and I need to insert 2 lists. Work's records are created too fast and can't take a customField from still nott inserted Case. If insert 1 per time - Error: Too many DML statements: 151.

My code:
public class Helper {

    public static void upd(Map<Id, Case> cases){
        
        List<Case> newCases = new List<Case>();
        List<Work__c> wp = new List<Work__c>();
        
        for(Case c1 : cases.values()){
            Case c2 = new Case(Origin = 'Web', Status = 'New');
            newCases.add(c2);
            
            for(Work__c w1 : [SELECT Id, Quantity__c, Custom_Field__c FROM Work__c WHERE Custom_Field__c =: c1.Id]){
                Work__c w2 = new Work__c (Quantity__c = w1.Quantity__c, Custom_Field__c = c2.Id);
                wp.add(w2);
            }  
        }
        insert newCases;
        insert wp;
    }
}

 
Hello. I have a problem with one code's part.

Controller (a part):
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;
                try{
                    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;                    
                    insert opp;                
                }catch(Exception e) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
                }     
            } 
         }
     }
Test (a part):
Account acc = new Account(Name ='Test acc');
		insert acc;
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;   
        PriceBook2 pb2 = new PriceBook2(Name = 'Test pb', IsActive = true);
        insert pb2;
        Opportunity opport = new Opportunity(Name = 'Test opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
	    PricebookEntry standardpbe = new PricebookEntry(Pricebook2Id = test.getstandardpricebookid(), Product2Id = prod.Id, UnitPrice = 0.0, IsActive = true);
        insert standardpbe;
       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        insert pbe;               

        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = quantity;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        insert item;
        
        prController.click();
And I have that:
User-added image
I do something wrong?