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
sfdc development hintssfdc development hints 

Hi anyone please hel;p in writing test class for the trigger below,

trigger ContactSumTriggermodified on Contact (after  delete, after  insert, after update,after undelete)
 {
 
   
     Set<ID>conIds= new Set<ID>();
    
    
    Contact[] cons;
    if (Trigger.isDelete)
        cons = Trigger.old;
    else
        cons = Trigger.new;

    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons)
    {
            acctIds.add(con.AccountId);
    }
  
  

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([Select Id,Total_no_of_contacts__c ,(Select Id,AccountId from Contacts where AccountId in :acctIds)from Account where Id in :acctIds]);


 
  
  for(Contact con:[Select Id , AccountId from Contact where AccountId in:acctstoupdate.keyset()])
        conIds.add(con.id);
       
    for(Account acct:acctstoupdate.values())      
  {
  
   
 
  if (acct.Total_no_of_contacts__c != conIds.size())
            acct.Total_no_of_contacts__c = conIds.size();                 
  }         
  update acctsToUpdate.values();
                                           
 }

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Just to begin with :

 

@isTest
private class ContactSumTriggermodified_Test
{
    private static testmethod void myTest()
    {
        Contact con = new Contact(lastName = 'test') ;
        insert con ;
        delete con ;
    }
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

Just to begin with :

 

@isTest
private class ContactSumTriggermodified_Test
{
    private static testmethod void myTest()
    {
        Contact con = new Contact(lastName = 'test') ;
        insert con ;
        delete con ;
    }
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
sfdc development hintssfdc development hints

Hi , i need to write a trigger on a object called "enroll candidate" which is associated with candidate object and course detail object , where course detail object is a child object of course object, my req is there is a field called as booked slot and totalslot , when total slot is entered, booked slot is calculated automatically , i need to inactivate or stop the condition where bookslot is equal to totalslot and also booked slot should be incremented by ++ through a bulk trigger . can anyone please help me with this