0

I am very new to Apex and found this site- hoping someone can help me out. We have a LOT of code-free automation on our cases and related objects, so we had to resort to Apex classes to do a few things, which we outsourced to a developer we no longer work with.

I would like to add a field update to the bottom of the apex code. Field is called "Quality Age" and should calculate number of days between Now and Created Date when a case status is changed to Completed (we had to use Apex to automate this status change). This is what I have so far, but keep getting errors. Any help would be appreciated!

enter image description here

1
  • 6
    Generally speaking, you should post code as text instead of an image, and if you're getting error messages you should include the full text of the error, varbatim. Also, if you're new to a part/feature in Salesforce, then Trailhead is going to be a better starting point. If you're entirely new to programming, then Apex Basics for Admins is a good place to start. After that, the Apex Basics & Database module is the next step.
    – Derek F
    Dec 14, 2021 at 15:18

1 Answer 1

2

The method you're looking for is System.now() but you'll also need to loop over your collection (List<Quality>) to update each of the records:

public class populateQualityAge {
    public Quality c;

    public static void populateField(List<Quality> ages) {
        for(Quality age : ages) {
            age.Quality_Age__c = System.now().date().daysBetween(age.CreatedDate.date());
        }

        update ages;
    }
}

EDIT: Updated to calculate number of days

2
  • 1
    Don't you need to do math on the getTime() values? I don't think Datetime math is supported as you have it here.
    – Adrian Larson
    Dec 14, 2021 at 15:36
  • You are correct, I notice OP asked for "number of days" so I will update this answer with a more appropriate method.
    – nbrown
    Dec 16, 2021 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .