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
s5s5 

Regarding writing test method

Hi,

 

I wrote one sheduled batch apex programming and i am new to this apex code.Please tel me how to write the test method for the below code.Please help me in this.



  
     List<Registration__c> allRegList =new List<Registration__c>();
  RecordType recordtype=[select name,id from RecordType where Name='Surface Product Registration Record'];
  allRegList=[select id,Name,where_did_you_purchase__c,sum_of_enduse_points__c  from Registration__c where recordtypeid=:recordtype.id and Createddate=TODAY];

   for(sObject s:scope){
       Account a=(Account)s;
       accSet.add(a);
   }   
   
   for(Account a:accSet){
       Double sum=0.0;
            
       for(Registration__c r:allRegList){
                      if(r.Where_did_you_Purchase__c==a.id){
                         if(r.sum_of_enduse_points__c!=null){           
                sum=sum+r.sum_of_enduse_points__c;
                }      
                System.debug('THE SUM INSIDE FOR LOOP IS ******'+sum);
            }
       }
           a.current_year_cp_points__c= a.current_year_cp_points__c + sum;
          updateAcc.add(a);    
   }
      
}

Please help  me in this.Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

in your cide you have to create those two object records also....

contact objcon = new contact();

objcon.lastname = 'test';

insert objcon;

 

like obj.contact__c = objcon.id;

 

you have to create like this for all lookups....

All Answers

kiranmutturukiranmutturu

try this....

 

@isTest

private class testbatch{

 

static testmethod void testmy(){

  RecordType recordtype=[select name,id from RecordType where Name='Surface Product Registration Record'];

            Registration__c obj = new Registration__c();

             obj.name = 'test';

             obj.recordtype = recordtype.id;//create this object with all possible values.. here i created a dummy record

         insert  obj;

 

Test.StartTest(); 

 batchUpdateCPpoints reassign = new batchUpdateCPpoints();

   ID batchprocessid = Database.executeBatch(reassign); 

test.stoptest();

}

}

s5s5

Hi,

 

Here in Registration object 2 lookup fields are there and they are mandatory.So it is givng string exception and 0 percent coverage.

 

@isTest
private class testbatch{
 
static testmethod void testmy(){
  RecordType recordtype=[select name,id from RecordType where Name='Surface Product Registration Record'];
            Registration__c obj = new Registration__c();

             obj.sum_of_enduse_points__c = 9;
             obj.where_did_you_purchase__c='Dealer';
            obj.Contact__c = 'Dummy Registration Contact';
            obj.Registration_Address__c = 'Banglore';
            obj.Registration_Zip__c = '573201';
            obj.Last__c = 'Keerthi';
                 insert  obj;
         
Test.StartTest(); 
 batchUpdateCPpoints reassign = new batchUpdateCPpoints();
   ID batchprocessid = Database.executeBatch(reassign);
test.stoptest();
}
}

 

In the above code Contact is the lookup of 'Contact' object and Where did you purchase is the lookup of Account object so it is giving string exception.how can the above code be altered.Please help me in this.

kiranmutturukiranmutturu

in your cide you have to create those two object records also....

contact objcon = new contact();

objcon.lastname = 'test';

insert objcon;

 

like obj.contact__c = objcon.id;

 

you have to create like this for all lookups....

This was selected as the best answer
s5s5

Here in Registration object Contact is the mandatary field and in lookup i am selecting Name in contact.If i assign something to Name in lookup it was showing error saying 'Name cannot be writteble' as name is standard field in Conatct Object.

 

So it was showing Dml exception.How can this be done.Please tel me

s5s5

Thank you so much now it is working.