• Anbu
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies

Hi everyone,

I am new with Apex and and still have a lot to learn. I have built a schedulable class that should check some records and perform an update in the same object (quite simple). Problem is the test class that doesn't get enough code coverage. I have used the anonymous window and records are created. Problem is the UPDATE which is not performed.

here the class:



Apex Class

global class UpdateRollingAvgMonthlyRevenueJob implements Schedulable {
    global void execute(SchedulableContext ctx){
        //Query Acct Revenue Month that needs to be updated
    List<Acct_Revenue_Month__c> acctRevenueMonthList = [SELECT Id, Rolling_AvgMonRev__c, Rolling_AvgMonRevforFormula__c 
                                                            FROM Acct_Revenue_Month__c
                                                            WHERE IsAvgMonthRollCompare__c = TRUE ];
       
        List<Acct_Revenue_Month__c> acctRevenueMonthListToBeUpdated = new List<Acct_Revenue_Month__c>();
        
        if(!acctRevenueMonthList.isEmpty()){
            for(Acct_Revenue_Month__c arm : acctRevenueMonthList){
                Acct_Revenue_Month__c AcctRevMonth = new Acct_Revenue_Month__c ();
                AcctRevMonth.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
acctRevenueMonthListToBeUpdated.add(arm);                
            }
            update acctRevenueMonthListToBeUpdated;
        }
    }
}



Test Class


@isTest
private class UpdateRollingAvgMonthlyRevenueTest {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testmethod void testScheduledJob() {
        
         Date firstDayOfMonth = System.today().toStartOfMonth();
    
        //Create acct Revenue Month
        Acct_Revenue_Month__c arm = new Acct_Revenue_Month__c();
        arm.Name = 'TestAcct';
        arm.Rolling_AvgMonRevforFormula__c = 1; 
        
        insert arm;
        
        arm.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
        update arm;
        
        Test.startTest();
        // Schedule the test job
        String jobId = System.schedule('ScheduledApexTest',
           CRON_EXP,
            new UpdateRollingAvgMonthlyRevenueJob());
        // Stopping the test will run the job synchronously
        Test.stopTest();   
        
        arm = [SELECT Name,Tier__c,Company_Group_Monthly__c,Rolling_AvgMonRevforFormula__c,Rolling_AvgMonRev__c,LastModifiedDate
                                FROM Acct_Revenue_Month__c WHERE id = :arm.Id];
        
        system.assertNotEquals(null, arm.Rolling_AvgMonRevforFormula__c, 'This record has been modified');
        arm.Rolling_AvgMonRevforFormula__c = arm.Rolling_AvgMonRev__c;
        update arm;
    }
}

  • November 05, 2021
  • Like
  • 0
Hello guys,

In case object I have a lookup to account. I would like to write a trigger that fill a custom field e.g Account_Name__c, with the same value of the lookup but as text field. Could you help me with this? thanks!!
  • February 26, 2019
  • Like
  • 0
Hello guys,

I have just started developing and I am following the current situation:

we're using community and we want to implement email-to-case. The only problem is that with partner licenses we cannot reply emails directly from Case object and this is mean that if we receive a case from email to case, we can only reply by email and this is also mean that we will get a new case for each reply and it will be a real mess in our salesforce.

I read that you can do some tricks by code. Any idea how to write a trigger that would do the following:

If the person will reply back to the current case, then the trigger will add the conversation to the first case and will delete the second case and so on.

I hope this is clear and someone can help with this!

Thanks a lot!

 
  • February 14, 2019
  • Like
  • 0