• dschild
  • NEWBIE
  • 0 Points
  • Member since 2005

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

Found this code to count activities on Opportunities. I've tried to re-purpose it for Leads. It's not passing the tests. The error says System.AssertException: Assertion Failed: Expected: 1, Actual: nullClass.LeadActivityCount.testCountTask: line 52, column 1

 

Here's the code:

 

public with sharing class LeadActivityCount {

    public static Boolean didRun = false;
    public static String ldPrefix =  Lead.sObjectType.getDescribe().getKeyPrefix();

    /*
    * Takes a set of Lead IDs, queries those leads, and updates the activity count if appropriate
    */
    
    public static void updateLeadCounts(Set<ID> ldIds) {

        if (didRun == false) { //We only want this operation to run once per transaction.
            didRun = true;

            //Query all the leads, including the tasks child relationships
            List<Lead> ld = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Lead WHERE ID IN :ldIds];
            List<Lead> updateld = new List<Lead>();

            for (Lead l : ld) {
                Integer count = l.tasks.size() + l.events.size();

                if (l.activity_count__c != count) {
                    l.activity_count__c = count;
                    updateld.add(l); //we're only doing updates to ld that have changed...no need to modify the others
                }
            }

            //Update the appropriate leads
            try {
                update updateld;
            } catch (Exception e) {
                //This is controversial. Anything could happen when updating the Lead..validation rule, security, etc. The key is we don't
                //want the event update to fail...so we put a try catch around the lead update to make sure we don't stop that from happening.
            }
        }
    }

    /*
    * Test method for this class and TaskUpdateLead and EventUpdateLead
    */
    public static testMethod void testCountTask() {
        //Setup
        Lead l = new Lead(lastname='Test', company='Test Company');
        insert l;

        //Insert our first task
        Task t = new Task(subject='Test Activity', whoId = l.id);
        insert t;

        //Verify count
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Disconnect task from the Lead
        didRun = false; //Reset
        t.whatId = null;
        update t;
        //Verify count = 0
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(0,l.activity_count__c);

        didRun = false; //Reset
        //Add an event
        Event e = new Event(subject='Test Event', whoId = l.id, startDateTime = System.Now(), endDateTime = System.now());
        insert e;

        //Verify count = 1
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Relink the task to the Lead
        didRun = false; //Reset
        t.whatId = l.id;
        update t;

        //Verify count = 2
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(2,l.activity_count__c);

        //Disconnect the event from the Lead
        didRun = false; //Reset
        e.whatId = null;
        update e;

        //Verify count is back down to 1
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Delete the task
        didRun = false; //reset
        delete t;
        //Verify count is back down to 0
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(0,l.activity_count__c);

    }

}

Where can I find this information?
"What are the limitations of Developer's Edition"

i.e. number of contacts I can store
number of users
any other limitations, etc....
I am receiving the following error in the Creating Global Quick Actions Trailhead module challenge - "The global quick action was not created or it was not named 'New Detailed Account' "

Have read the previous posts and suggestions to fix, but my Global Actions are identical to the correct responses and the Publisher Layouts are displayed.  When I go into Chatter and Home on one of the Apps, I still do not see the New Detailed Account action listed in the feed though.  Is there some other setting I need to fix that will enable this to display BESIDES the Global Page Layout??
User-added imageUser-added image
User-added image

Found this code to count activities on Opportunities. I've tried to re-purpose it for Leads. It's not passing the tests. The error says System.AssertException: Assertion Failed: Expected: 1, Actual: nullClass.LeadActivityCount.testCountTask: line 52, column 1

 

Here's the code:

 

public with sharing class LeadActivityCount {

    public static Boolean didRun = false;
    public static String ldPrefix =  Lead.sObjectType.getDescribe().getKeyPrefix();

    /*
    * Takes a set of Lead IDs, queries those leads, and updates the activity count if appropriate
    */
    
    public static void updateLeadCounts(Set<ID> ldIds) {

        if (didRun == false) { //We only want this operation to run once per transaction.
            didRun = true;

            //Query all the leads, including the tasks child relationships
            List<Lead> ld = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Lead WHERE ID IN :ldIds];
            List<Lead> updateld = new List<Lead>();

            for (Lead l : ld) {
                Integer count = l.tasks.size() + l.events.size();

                if (l.activity_count__c != count) {
                    l.activity_count__c = count;
                    updateld.add(l); //we're only doing updates to ld that have changed...no need to modify the others
                }
            }

            //Update the appropriate leads
            try {
                update updateld;
            } catch (Exception e) {
                //This is controversial. Anything could happen when updating the Lead..validation rule, security, etc. The key is we don't
                //want the event update to fail...so we put a try catch around the lead update to make sure we don't stop that from happening.
            }
        }
    }

    /*
    * Test method for this class and TaskUpdateLead and EventUpdateLead
    */
    public static testMethod void testCountTask() {
        //Setup
        Lead l = new Lead(lastname='Test', company='Test Company');
        insert l;

        //Insert our first task
        Task t = new Task(subject='Test Activity', whoId = l.id);
        insert t;

        //Verify count
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Disconnect task from the Lead
        didRun = false; //Reset
        t.whatId = null;
        update t;
        //Verify count = 0
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(0,l.activity_count__c);

        didRun = false; //Reset
        //Add an event
        Event e = new Event(subject='Test Event', whoId = l.id, startDateTime = System.Now(), endDateTime = System.now());
        insert e;

        //Verify count = 1
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Relink the task to the Lead
        didRun = false; //Reset
        t.whatId = l.id;
        update t;

        //Verify count = 2
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(2,l.activity_count__c);

        //Disconnect the event from the Lead
        didRun = false; //Reset
        e.whatId = null;
        update e;

        //Verify count is back down to 1
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(1,l.activity_count__c);

        //Delete the task
        didRun = false; //reset
        delete t;
        //Verify count is back down to 0
        l = [SELECT ID, activity_count__c FROM Lead WHERE ID = :l.id];
        System.assertEquals(0,l.activity_count__c);

    }

}