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
Rakesh ImsaniRakesh Imsani 

How to write test class for this class Please help me..

Public class CustomCaseAlertsController
{

  public CustomCaseAlertsController(ApexPages.StandardController stdController)
    {
//how to get id here
//how to populate account look up in case obj... please help me
        String caseId = stdController.getId();
        caseObj = [Select id,AccountId,ContactId,CaseNumber from Case where id=:caseId];
        
        
    ......................................
        ................................

     }    

    public void hideAlerts()
    {
        insert ListObj;
    }

}
Vishnu VaishnavVishnu Vaishnav
Hi Rakesh,
Suppose that if u have standard controller for Account :
      Case caseObj = new Case();
      //Fill data of case
      insetr caseObj;

      ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
      CustomCaseAlertsController CCAC = new CustomCaseAlertsController(sc);      

:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....
Vishnu VaishnavVishnu Vaishnav
Hi Rakesh,

Here is more about for u ...
http://salesforce.stackexchange.com/questions/39978/need-help-with-test-class-with-apexpages-currentpage-getparameters-id
 
Rakesh ImsaniRakesh Imsani
Thankyou vishnu
This is extension for Case object
while i am populating account name look up  in case object i am getting 
System.NullPointerException: Attempt to de-reference a null object in test class exception

@isTest(SeeAllData=true)
Public Class CustomCaseAlertsControllerTest
{
        
       static testmethod void myTest()
       {
        Case cObj=new Case();
      cObj.Account.Name='Rakesh'; // here i am getting System.NullPointerException: Attempt to de-reference a null object in test class
    cObj.Contact.Lastname='Yadav';
--------------------------------------
-------------
}
}
Vishnu VaishnavVishnu Vaishnav
Hi Rakesh ,

u r on wrong way ...
Account acc = new Account(name = 'Rakesh');
insert acc;
Case cObj=new Case();
cObj.AccountId = acc.id ; // here i am getting System.NullPointerException: Attempt to de-reference a null object in test class

Now u can try this one..
Error is on your code is that you are putting directly name in  "cObj.Account.Name" ..

Qusetion Solved ?


 
Rakesh ImsaniRakesh Imsani
Thankyou vishnu once again
i am getting one more error, can you plese help one more time

System.DmlException: Insert failed. First exception on row 0 with id a29R00000005OICIA2; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

@isTest(SeeAllData=true)
Public Class CustomCaseAlertsControllerTest
{
        
       static testmethod void myTest()
       {
          
          Account a =new Account();
          a.name='Rakesh';
          a.Type='Other';
          a.Phone='89898989';
          a.Bill_to_State__c='TG';
          insert a;
           
      
            //Pagereference ref = new Pagereference('apex/CustomAlertPopup?id=500R0000004pbU8');
            //ref.getParameters().put('case',std.getId());
           Case cObj=new Case();
          cObj.AccountId =a.id ;
          cObj.Origin='Phone';
          cObj.Priority='Low';
          cObj.status='Solving';
          cObj.Subject='Test class';
           
           
            insert cObj;
          // cObj=[Select id,AccountId,ContactId,Site__c,Site__r.Name,CaseNumber,Account.name,contact.name,Origin,Priority,status,Subject  from case where Id='500R0000004pbU8' ];
                    
           Hattrix_Account__c hattObj=new Hattrix_Account__c();
           hattObj=[select id,Hatrix_Parent_Account__c from Hattrix_Account__c limit 1];
           
           soalert__Hidden_Alert__c hideAlertObj=new soalert__Hidden_Alert__c();
           
           List<soalert__Hidden_Alert__c> listObj =new List<soalert__Hidden_Alert__c>();
           listObj=[Select soalert__Alert__c,soalert__Alert_Message__c,soalert__Object_ID__c,soalert__Record_Name__c,soalert__User__c from soalert__Hidden_Alert__c limit2 ];
         
                  
           test.starttest();
         
ApexPages.StandardController std =new ApexPages.StandardController(cObj);
   CustomCaseAlertsController testObj=new CustomCaseAlertsController(std); 
     PageReference pageRef = Page.CustomAlertPopup;
     pageRef.getParameters().put('id', String.valueOf(cObj.Id));
            Test.setCurrentPage(pageRef);
            
        testObj.hideAlerts();
        insert listObj; // System.DmlException: Insert failed. First exception on row 0 with id a29R00000005OICIA2; first error:                    INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]  here
        test.stoptest();
       
       }

}
Rakesh ImsaniRakesh Imsani
iwant to write test class for this class
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AwM4
Vishnu VaishnavVishnu Vaishnav
Hi Rakes,

Its big class bud. But error is that u r querfying and put list in "listObj" and u r inserting this.
U know that we can't insert record with id, we can only upsert or update .
Rakesh ImsaniRakesh Imsani
ha ha got it vishnu, i verified already, i made mistake...
Tq very much