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
KeerthigeeKeerthigee 

How to write test method or code for trigger

Hi All,

    I want to write a testcode  for trigger to 75 or 100%  code coverage for uploading a package as follows:
 

  trigger BorrowIncrement on Borrow_Books__c (after insert) {
     set<id> mem =new set<id>();
for(Borrow_Books__c b:trigger.new){
    if(b.Member__c!=NULL)
        mem.add(b.Member__c);
}

List<Members__c> memlist=[Select id,name,Borrowed_Books__c,(select id,name from Borrow_Books__r) from Members__c where ID in :mem];
for(Members__c m:memlist){
    m.Borrowed_Books__c=m.Borrow_Books__r.size();
}
update memlist;

}

Can anyone help me.

Kindly support and suggest.
Grazitti TeamGrazitti Team
Hi Keerthigee,

To write a test code for this just insert a test record for the object Borrow_Books__c, in which you Member__c field is not equal to null.

Please mark it as best if this helps you.

Regards,
Grazitti Team
www.grazitti.com
Ankit AroraAnkit Arora
I think you are following the workbook? This link already have a test class very similar to your use case. Hope this will help.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm
KeerthigeeKeerthigee
Hi,

  Thank you for your response.

In Borrow_books__c custom object,name is default field. This object contains two master-detail relationship with books and members object.

And also contains validation rules on 3 three fields i.e Borrow_date__c,Return_date__c,Actual_returned_date__c.

So,these fields are mandatory to insert record for 75% code coverage of test class.

Here ,the code is

@isTest
private class Borrowbook {
    static testMethod void validateborrowbook() {
     
         Borrow_Books__c  BB = new Borrow_Books__c(name='OOPS',Webtechie__Boook__c='OOPS',Webtechie__Member__c='Test',Webtechie__Borrowed_Date__c='2014-9-15');
          insert BB;
   
    }
   
  }

I am getting error as expecting date for borrowed_date.

Kindly support and suggest.

Thanks.



    
KeerthigeeKeerthigee
Hi,

I modified above code as

 @isTest
private class Borrowbook {
    static testMethod void validateborrowbook() {
     
         Borrow_Books__c  BB = new Borrow_Books__c(name='OOPS',
                                                    Webtechie__Boook__c='OOPS',
                                                    Webtechie__Member__c='Test',
                                                    Webtechie__Borrowed_Date__c=date.newinstance(2014,9,15),
                                                    Webtechie__Return_Date__c=date.newinstance(2014,9,30),
                                                    Webtechie__Actual_Returned_Date__c=date.newinstance(2014,9,30));
         
                
          insert BB;
   
    }
   
  }

 I am getting error as

User-added image


Kindly support and suggest.

Awaiting for your response.

Thanks.