Skip to main content
divesh khanduja ha fatto una domanda in #Apex
Hi 

I am using Standard controller (Case ) with extesion controller  I ma upsertingthe case record in amy controller ;

public with sharing Myclass{

//Constructor   

  

    public myController(ApexPages.StandardController stdController){

    

   

            try{

            this.caseId= ApexPages.currentPage().getParameters().get('id');

            if(caseId != null)

                oCase = [select Id, CaseNumber,AccountId, Owner.Name, Status, Priority, Origin, Contact.Name, Account.Name, Type, Reason, Subject, Description,(Select CommentBody from CaseComments order by createddate limit 1) from Case where Id = :caseId];

        } 

        catch(Exception e){

            ExceptionLogger.addException(e,'myController','Constructor'); 

            

        }

    }    

    

     public PageReference save(){

    upsert oCase;

   PageReference pg = new PageReference('/apex/Mypage');

   pg.getParameters().put('id',oCase.Id);        

    flag1=true;

    flag2= false;

    return pg;

}

When I try to cover /run the test class on save() in Test Class , after inserting Case it give me an error 

Attempted to upsert a null list

//Test Class Code :

Insert CaseList[with data ];

System.debug('showCas caseList= '+caseList); //Here i can see the case list 

ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(caseList[0]);

        myController controller = new myController(sc);

        controller.save() // Attempted to upsert a null list

Please help me ! Really needed ,

Thanks in Advance ! 
5 risposte
  1. 16 lug 2016, 14:12
    Where do you define oCase?

    Couple thoughts:

    - If oCase is a List<Case>, declare it as "List<Case> oCase = new List<Case>();".

    - Since your code only overrides the default value of oCase if caseId != null, it either will be blank list or one for the valid caseId.  In either scenario, you can change your upsert to "if (!oCase.isEmpty()) upsert oCase;".

    - If you are using caseId, you likely only expect one Case.  On that hand, you can define "Case oCase;" and use LIMIT 1 in your select to make sure you get a single Case object.  In your upsert, you would use "if (oCase != null) upsert oCase;".

    I hope that helps.
0/9000