function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DannyK89DannyK89 

Trigger Question

Ok so I was doing some research on triggers and found  something called Trigger Context Variables. In there I found something called isExecuting. I was wondering if I put that in a if statement around all my trigger code with it only run if a user in my org makes a change that causes the trigger to fire. For example if I pull Leads from Data.com with the trigger code fire?

 

Sample Code:

 

Trigger TestTrigger on Lead (after insert){
   if(Trigger.isExecuting){
      // Trigger code
   }
{

 


Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

Your code looks sound - I'm curious as to why you're hitting a SOQL Governor Limit.

 

Can you throw in some System.Debug messages, pull up the trusty old System Log - and reproduce the issue?  Even if Data.com is batching this stuff in - each batch would have its own set of Limits...

 

-Andy

All Answers

MattLacey.ax1065MattLacey.ax1065

From looking at the documentation I believe the point of this method is that you can use it in apex code that's called by your trigger, but not contained within the trigger itself — allowing you to differentiate between the code being run by a trigger or by some other method.

 

Using it inside the trigger itself would appear to make no sense, as it would always return true, you couldn't have code in a trigger being run that hasn't been the result of that trigger being fired.

DannyK89DannyK89

Alright thanks for the information. I guess I just misunderstood what I read. My Issue really is that when I pull a large amount of Leads from Data.com I hit a governor limit for SOQL queries. I have a trigger that fires everytime a lead is added into our org. Is there anyway to get specific user information without using a SOQL query? Or is there a better way to right my trigger code?

 

Apex Trigger:

 

/*
	This trigger tracks new Leads that are created
	it only trackes them if the Leads TAP Result is not null
*/

trigger NewLeadTrackingTrigger on Lead  (after insert) {
    DateTime dt = system.now();
    String textarea;
    String timestamp = dt.format('MM/dd/yyyy hh:mm a');
    // This gets the users information
    User Person = [SELECT FirstName, LastName, Title, Alias FROM User WHERE Id = :UserInfo.getUserId()];
    map<String, Schema.Recordtypeinfo> rMap = new map<String, Schema.Recordtypeinfo>();
    List<Tracking__c>TrackingList = new List<Tracking__c>();
    // This section makes a list of Record Types for the Tracking Object
    Schema.DescribeSObjectResult R = Tracking__c.SObjectType.getDescribe();
	rMap = R.getRecordTypeInfosByName(); 
    // This section creates the new Tracking record
    // only creates a Tracking record if the TAP Result field is not null
    for(Lead L : Trigger.new){
        if(L.TAP__c == true){
            string Id = L.Id;
            Tracking__c newTrack = new Tracking__c();
            newTrack.RecordTypeId = rMap.get('Lead Tracking').getRecordTypeId();
            newTrack.User_Name__c = Person.FirstName + ' ' + 
                Person.LastName;
            newTrack.User_Title__c = Person.Title;
            newTrack.Time_Stamp__c = dt;
            newTrack.Object__c = 'Lead';
            newTrack.System__c = 'TAP Candidate (Cold)';
            newTrack.Name = Person.Alias + ' - ' 
            + timestamp + ' - ' + newTrack.Object__c + ' - ' + L.FirstName + ' ' + L.LastName; 
            newTrack.Who__c = L.FirstName + ' ' + L.LastName;
            newTrack.Tracking_Account__c = L.Company;
            newTrack.Tracking_Title__c = L.Title;
            newTrack.Lead_SIT_Assigned__c = L.SIT_Assigned__c;
            newTrack.Lead_Activity_Date__c = L.Activity_Date__c;
            newTrack.Lead_Description__c = L.Description;
            newTrack.Lead_Status__c = L.Status;
            newTrack.Tracking_Result__c = L.TAP_Result__c;
            if(L.TAP_Activity__c != '' && L.TAP_Activity__c != null){
                textarea = L.TAP_Activity__c;
                // This section checks if the character count the the
                // TAP Activity is over 500 characters
                // if so then it is reduced to 500 characters
                if(textarea.length() >= 500){
                    newTrack.Tracking_Details__c = textarea.substring(0, 500);
                }
                else{
                    newTrack.Tracking_Details__c = textarea;
                }
            }
            newTrack.Lead_Id__c = Id;
            TrackingList.add(newTrack);
        }
    }
	insert TrackingList;
}

 

Andy BoettcherAndy Boettcher

Your code looks sound - I'm curious as to why you're hitting a SOQL Governor Limit.

 

Can you throw in some System.Debug messages, pull up the trusty old System Log - and reproduce the issue?  Even if Data.com is batching this stuff in - each batch would have its own set of Limits...

 

-Andy

This was selected as the best answer
DannyK89DannyK89

The only way that I can reproduce the issue is to make a list of objects and insert them one at a time in a for loop. If I insert then as a list it works fine. I'm not sure when the issue is. 

Andy BoettcherAndy Boettcher

Aaahhh - that would explain it.  You have that SOQL query at the head of your trigger that would fire for each record if you inserted them one at a time through another method.

 

Could you use the UserInfo object in place of your SOQL query to get the user's ID - place that in a new field in the Tracking__c object - then based on that, you could use Formula fields to pull in the other information from the UserId.

 

-Andy

 

 

DannyK89DannyK89

I was able to figure something out. I used work flow rules and field updates to put the correct data into the fields that needed user information. I am in the process of testing it right know and it seems to be working fine for the moment. 

Andy BoettcherAndy Boettcher

Excellent!  Declarative is absolutely the way to go if you can do it.  Nice work.

 

(shameless plug) If my comment above helped you get to that conclusion, mark it as a solution so others can benefit (/shameless plug)