• Elliot2642
  • NEWBIE
  • 0 Points
  • Member since 2013

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

Hi All,

 

My goal is to be able to have my Class grab a list of Task Records (which I know how to  do), perform a very simple calculation on them (which I do not know how to do) and then have the result be dropped into a field on the related Lead Record (which I know how to do). Would someone please help me understand how to perform simple calculations with the results of the query in my class or put me in the direction of documentation that might help? Thanks.

 

In case this helps, I am trying to create a 'Response Rate' field on the Lead Object. I want to look at all the Completed Tasks related to each Lead Record, then, grab all of the Task Records where the Type equals 'Email Sent' and 'Email Reply'. I woud then like to divide the number of Tasks where the Type is 'Email Sent' by the number of Tasks where the Type is 'Email Reply' and put that percentage into a field on the Lead called 'Response Rate'. 

Hi All,

 

I am not a developer, but due to a business request, I have been working on updating the below code to function with Leads:

 

http://www.radialweb.com/2010/08/summarizing_salesforce_fields_with_triggers/#comments

 

Basically, anywhere I saw 'Opportunity', I replaced it with 'Lead'. Anywhere I saw 'opp' or 'opps' I replaced them with 'lead' and 'leads' respectively. And anywhere I saw an 'o' in repsect to referencing a Record as an Opportunity Record, I changed the 'o' to an 'l'.

 

That being said, when I made the above edits and attempted to save the class, I got the below error:

 

Error: Compile Error: Test methods must be in test classes at line 40 column 35

 

Any help?

 

Thanks,

Elliot

 

 

Hi all,

 

Let me preface this by saying that I have very little coding experience.

 

That being said, our company has a particular business request and I'm not sure where to start. We would like to put a mechanism in place so that when a Lead Record has had no Activity Records in the past 3 months AND there are no Open Activities, that the ownership of the Lead will change to a queue. If anyone can please point me in the right direction as to where to start / learn how to go about such a project, it would be very much appreciated.

 

Thanks,

Elliot

Hi All,

 

I am an administrator and have not much apex experience. Having a bit of an issue with my trigger. The purpose of the trigger is to count the number of Activity Records associated to a Lead and then drop it into a field called Activity_Count__c on the Lead. The code all works as intended, but I wanted to add another parameter. I would like to be able to only have that field contain the number of Activity Records that are 'Created By' a certain User (i.e. User ID = '12345'). How would I add that contraint into the Trigger, Class and Test Class? See below.

 

Thanks,

Elliot

 

 

Class:

 

 

public with sharing class LeadActivityCount {

 

    public static Boolean didRun = false;

    public static String ledsPrefix =  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> ledsIds) {

 

        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> leds = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Lead WHERE ID IN :ledsIds];

            List<Lead> updateLeds = new List<Lead>();

 

            for (Lead l : leds) {

                Integer count = l.tasks.size() + l.events.size();

 

                if (l.activity_count__c != count) {

                    l.activity_count__c = count;

                    updateLeds.add(l); //we're only doing updates to leads that have changed...no need to modify the others

                }

            }

 

            //Update the appropriate leads

            try {

                update updateLeds;

            } catch (Exception e) {

                //This is controversial. Anything could happen when updating the opportunity..validation rule, security, etc. The key is we don't

                //want the event update to fail...so we put a try catch around the opp update to make sure we don't stop that from happening.

            }

        }

    }

}

 

 

Test Class:

 

 

@isTest
private class LeadsTestClassName{

public static Boolean didRun = false;
public static String ledsPrefix =  Lead.sObjectType.getDescribe().getKeyPrefix();
   
    /*
    * Test method for this class and TaskUpdateLead and EventUpdateLead
    */
    public static testMethod void testCountTask() {
        //Setup

        Lead leds = new Lead(lastname='Test', email='1@2.com', company='Test');
        insert leds;

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

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

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

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

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

        //Relink the task to the lead
        didRun = false; //Reset
        t.whoId = leds.id;
        update t;

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

        //Disconnect the event from the lead
        didRun = false; //Reset
        e.whoId = null;
        update e;

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

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

    }
}

 

 

Trigger:

 

 

trigger TaskUpdateLead on Task (after delete, after insert, after undelete, after update) {

    Set<ID> ledsIds = new Set<ID>();
    //We only care about tasks linked to leads.
    String prefix =  LeadActivityCount.ledsPrefix;

    //Add any lead ids coming from the new data
    if (Trigger.new != null) {
        for (Task t : Trigger.new) {
            if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
                ledsIds.add(t.whoId);
            }
        }
    }

    //Also add any lead ids coming from the old data (deletes, moving an activity from one lead to another)
    if (Trigger.old != null) {
        for (Task t : Trigger.old) {
            if (t.WhoId != null && String.valueOf(t.whoId).startsWith(prefix) ) {
                ledsIds.add(t.whoId);
            }
        }
    }

    if (ledsIds.size() > 0)
        LeadActivityCount.updateLeadCounts(ledsIds);

}

Hi All,

 

I am not a developer, but due to a business request, I have been working on updating the below code to function with Leads:

 

http://www.radialweb.com/2010/08/summarizing_salesforce_fields_with_triggers/#comments

 

Basically, anywhere I saw 'Opportunity', I replaced it with 'Lead'. Anywhere I saw 'opp' or 'opps' I replaced them with 'lead' and 'leads' respectively. And anywhere I saw an 'o' in repsect to referencing a Record as an Opportunity Record, I changed the 'o' to an 'l'.

 

That being said, when I made the above edits and attempted to save the class, I got the below error:

 

Error: Compile Error: Test methods must be in test classes at line 40 column 35

 

Any help?

 

Thanks,

Elliot

 

 

Hi all,

 

Let me preface this by saying that I have very little coding experience.

 

That being said, our company has a particular business request and I'm not sure where to start. We would like to put a mechanism in place so that when a Lead Record has had no Activity Records in the past 3 months AND there are no Open Activities, that the ownership of the Lead will change to a queue. If anyone can please point me in the right direction as to where to start / learn how to go about such a project, it would be very much appreciated.

 

Thanks,

Elliot