+ Start a Discussion
NikiG22NikiG22 

Code Coverage - Help

I need help with this trigger, i need to get some coverage on it so i can deploy it to production.. please help.

 

trigger RollUp on Resume_Subscriptions__c (after update, before delete) {

    // Set to hold the IDs of the Contacts of the Subscriptions
    Set<ID> ContactIDs= new Set<ID>();
    // Set to hold the IDs of the Subscriptions
    Set<ID> SubIDs = new Set<ID>();
    // Set to hold the IDs of the Contacts where the values need to be reset
    Set<ID> ContactIDsForReset = new Set<ID>();

    if(trigger.isupdate || trigger.isInsert)
    {
        for(Resume_Subscriptions__c rsSub : trigger.new )
        {
            // If the contact of a subscription has changed, then the old contact also needs to be updated 
            if( rsSub.User__c != trigger.oldmap.get(rsSub.ID).User__c)
                if( trigger.oldmap.get(rsSub.ID).User__c != null )
                    ContactIDs.add(trigger.oldmap.get(rsSub.ID).User__c);
            if(rsSub.User__c!=null)
            {
                ContactIDs.add(rsSub.User__c);
            }
        }
        ContactIDsForReset.addAll(ContactIDs);

        // The list of Contacts where the update has to take place
        List<contact> ContactsToUpdate = new List<contact>();    

        // Querying the database to get the Number of Subscriptions under an contact and the sum of the deal values of the Subscriptions under an contact
        AggregateResult[] Subscriptionsum = [select count(ID) NoOfSubscriptions, User__c ContactID from Resume_Subscriptions__c where Active__c=true AND User__c in: ContactIDs group by User__c ];
        
        for( AggregateResult ContactDetail : Subscriptionsum )
        {
            contact ContactToUpdate = new contact(ID = (ID)(ContactDetail.get('ContactID')));
            ContactToUpdate.Active_Resumes__c = integer.valueOf(ContactDetail.get('NoOfSubscriptions'));
            ContactsToUpdate.add(ContactToUpdate);
            // The contact being handled should not be reset. Hence removing it from the ResetSet
            ContactIDsForReset.remove((ID)(ContactDetail.get('ContactID')));
        }
        
        for( ID i : ContactIDsForReset )
        {
            // Resetting the summed up values to 0 if the contact no longer has a subscription.
            contact ContactToUpdate = new contact( ID = i );
            ContactToUpdate.Active_Resumes__c = 0;
            ContactsToUpdate.add(ContactToUpdate);
        }
        
        if(ContactsToUpdate.size() > 0)
            update ContactsToUpdate;
            
        ContactsToUpdate.clear();
    }

    if(trigger.isdelete)
    {
        for(Resume_Subscriptions__c rsSub : trigger.old )
        {
            if( rsSub.User__c != null)
                ContactIDs.add(rsSub.User__c);
            SubIDs.add(rsSub.ID);
        }
        ContactIDsForReset.addAll(ContactIDs);

        // The list of Contacts where the update has to take place
        List<contact> ContactsToUpdate = new List<contact>();    
        
        // Querying the database to get the Number of Subscriptions under an contact excluding the Subscriptions being deleted
        AggregateResult[] Subscriptionsum = null;
        if(ContactIDs.size() > 0)
        {
            Subscriptionsum = [select count(ID) NoOfSubscriptions, User__c ContactID from Resume_Subscriptions__c where User__c in: ContactIDs and ID not in: SubIDs group by User__c ];

            for( AggregateResult ContactDetail : Subscriptionsum )
            {
                contact ContactToUpdate = new contact(ID = (ID)(ContactDetail.get('ContactID')));
                ContactToUpdate.Active_Resumes__c = integer.valueOf(ContactDetail.get('NoOfSubscriptions'));
                ContactsToUpdate.add(ContactToUpdate);
                // The contact being handled should not be reset. Hence removing it from the ResetSet
                ContactIDsForReset.remove((ID)(ContactDetail.get('ContactID')));
            }

            for( ID i : ContactIDsForReset )
            {
                // Resetting the summed up values to 0 if the contact no longer has a subscription.
                contact ContactToUpdate = new contact( ID = i );
                ContactToUpdate.Active_Resumes__c = 0;
                ContactsToUpdate.add(ContactToUpdate);
            }
        }
        
        if(ContactsToUpdate.size() > 0)
            update ContactsToUpdate;

        ContactsToUpdate.clear();
    }

 Thank you,

Niki

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

Hi

 

@isTest

private class resume_Test{

static testmethod void resume_Test()

{

 

contact con = new contact()

con.Lastname='test';

insert con;

//insert the values for Account also

//here insert the values for resume_sbscriptions__c according to if conditons sothat all the condition will satisfy and update it

 

}}

 

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

@isTest

private class resume_Test{

static testmethod void resume_Test()

{

 

contact con = new contact()

con.Lastname='test';

insert con;

//insert the values for Account also

//here insert the values for resume_sbscriptions__c according to if conditons sothat all the condition will satisfy and update it

 

}}

 

This was selected as the best answer
NikiG22NikiG22

thank you i did get at least 35% for this trigger thank you for your help

 

code:

@isTest

private class RollUp_Test{

static testmethod void RollUp_Test()

{

contact con = new contact();

con.Lastname='test';
con.SystemOfOrigin__c ='test';

insert con;

Resume_Subscriptions__c res = new Resume_Subscriptions__c();

res.Product_Name__c='test';
res.Opportunity__c ='006R00000079BaC';



insert res;


 }
}