• Patrick Marks 2
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 13
    Replies
Hi everyone-

I scheduled an apex job to run daily at midnight but noticed that the job failed due to the Apex CPU time limit exceeded error. The code works in my developer sandbox but not in production. I'm wondering if it is because of something in my SOQL query, but unclear on where the culprit would be. The query only returns about 500 results. Any help is appreciated- here is my class:
 
global without sharing class UnassignAccountsDueToInactivity implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<Account> acctList = [SELECT Id, Name, OwnerId 
                                  FROM Account 
                                  WHERE (OwnerProfileName__c LIKE '%AE%' OR OwnerProfileName__c LIKE '%SDR%') AND 
                                  (Current_Customer__c != 'Current Customer A' AND Current_Customer__c != 'Current Customer B' AND Current_Customer__c != 'Current Customer C') AND
                                  Number_of_Open_Opportunities__c <= 0 AND
                                  DaysSinceLastActivity__c >= 30 AND
                                  DaysSinceLastOwnerChangeDate__c >= 7];
        
        if(!acctList.isEmpty()){
            for(Account acc : acctList){
                acc.DisqualifiedReason__c = 'No response';
                acc.OwnerId = '000000000000000';
            }
            update acctList;
        }
    }
}
Hey everyone-

I have a couple of custom Campaign lookup fields on the Contact object called First Campaign and Most Recent Campaign. These lookup fields need to get populated with the ID of the appropriate Campaign (should be pretty self explanatory-First Campaign would lookup to the first Campaign that the Contact was added to, Most Recent Campaign would lookup to the most recent Campaign that the Contact was added to). This action is supposed to take place when either of a couple of other custom Date/Time fields (Most Recent PQC Date/Most Recent Upsell PQC Date) on the Contact are updated. I would really appreciate help figuring out why the code isn't working as intended and what the fix is. Here is the trigger:
trigger ContactTrigger on Contact ( after update) {

    if(Trigger.isUpdate && Trigger.isafter && RecursionClass.fireContactUpdate)
    {
        ContactTriggerHandler hndl = new ContactTriggerHandler ();  
        hndl.UpdateContact(trigger.OldMap ,trigger.NewMap);
    }
}

Here is the handler class:
public class ContactTriggerHandler {
    ContactTriggerAction act = new ContactTriggerAction();
    public void UpdateContact(Map<id ,Contact> OldMap , Map<id , Contact> NewMap)
    {
        act.UpdateContactCampaignField(OldMap,NewMap);
    }
}

Here is the class that performs the action:
public class ContactTriggerAction {
    
    //METHOD TO SET CAMPAIGN NAME ON CONTACT IF PQC DATE FIELDS ARE UPDATED
    public void UpdateContactCampaignField(Map<id ,Contact> oldMap , Map<id , Contact> newMap)
    {
        List<Id> filteredContactIds = new List<Id>();
        
        //check for fields if updated
        for(Contact newCon :newMap.values())
        {
           Contact oldCon = oldMap.get(newCon.id);
           
           system.debug('___newCon__ContactStatus__c___'+newCon.ContactStatus__c);
           
           //FILTER CONTACTS WHOSE MostRecentPQCDate OR MostRecentUpsellPQCDate IS UPDATED 

           if( (newCon.MostRecentMQCDate__c!=null && oldCon.MostRecentMQCDate__c != newCon.MostRecentMQCDate__c) || (newCon.MostRecentUpsellMQCDate__c !=null && oldCon.MostRecentUpsellMQCDate__c!= newCon.MostRecentUpsellMQCDate__c) )
               filteredContactIds.add(newCon.Id);
        }
        
        system.debug('___filteredContactIds____'+filteredContactIds+'__current__userId___'+UserInfo.getUserId());
        
        //Functionality doesn't run if trigger is fired by integration user to avoid assignment issues
        if(filteredContactIds!=null && filteredContactIds.size()>0 && UserInfo.getUserId() != System.Label.Integration_User_Id)
            CheckRecentCampaignmembers(filteredContactIds);        
    }
    
    //METHOD TO SET RECENT CAMPAIGN AND FRIST CAMPAIGN ON CONTACT 
    @future
    public static void CheckRecentCampaignmembers(List<Id> contactIdLst)
    {
    
        system.debug('___entered____CheckRecentCampaignmembers____');
             
        List<Contact> cLstToUpdate = new List<Contact>();
        
        //SETTING MostRecentCampaign AND FirstCampaign FIELDS OF CONTACT WITH LastModified CAMPAIGNID
        //FETCH CAMPAIGN MEMBER RECORDS TO WHICH CONTACT IS LINKED 
        for(Contact cont : [Select Id, Name, FirstCampaign__c, MostRecentCampaign__c ,(Select Id, CampaignId , HasResponded from CampaignMembers where HasResponded=TRUE ORDER BY LastModifiedDate desc limit 1) from Contact where Id In :contactIdLst])
        {    
        
            Contact con_update = new Contact(Id = cont.Id);
            
           // system.debug('______cont_____'+cont+'____size____'+cont.CampaignMembers.size()+'___campaignId____'+cont.CampaignMembers[0].CampaignId);
            
            //UPDATING MostRecentCampaign AND FirstCampaign FIELDS ON CONTACT WITH CAMPAIGN NAME
            if(cont.CampaignMembers.size()>0){
                
                con_update.MostRecentCampaign__c  = cont.CampaignMembers[0].CampaignId;
                
                //CHECKS IF FIRST CAMPAIGN FIELD IS BLANK
                if(cont.FirstCampaign__c == null)
                {
                    con_update.FirstCampaign__c = cont.CampaignMembers[0].CampaignId;  
                }
            }
            
            system.debug('____con_update_____'+con_update);
            
            cLstToUpdate.add(con_update);

        }
        
        if(cLstToUpdate!=null && cLstToUpdate.size()>0){
            RecursionClass.fireContactUpdate = false;
            update cLstToUpdate ;

        }
    }
      
}

And here is the recursion class:
Public class RecursionClass{
    
    public static Boolean fireContactUpdate = true;
}
Thanks again for your help!
​​​​​​​​​​​​​​
Hey everyone-I'm trying to figure out the best way to transfer Account ownership of certain accounts once a day. I would use declarative tools but can't because the data that would fire the automation is coming from a couple of custom formula fields. I'm confused whether I need to write a scheduled class, a batch class or a scheduled batch class. I've gone through trailhead and sfdc help docs and am still unclear on how to accomplish this. Any pointers on how to get started are much appreciated.
Hey everyone-trying to write a simple trigger for populating a couple of custom lookup fields on Contacts. We have 2 custom fields called "First Campaign" and "Most Recent Campaign". I'm trying to get "First Campaign" populated with the first campaign the Contact was added to and "Most Recent Campaign" with the latest campaign they've been added to. Here's what I have so far, would love any help with this. Thanks in advance!
trigger FirstAndMostRecentCampaignOnContact on Contact (after insert, after update) {

    Set<Id> CampaignIds = new Set<Id>();
    for(Contact CON: Trigger.new) CampaignIds.add (CON.MostRecentCampaign__c);
    List<Campaign> c= new List<Campaign>([
        select 
        Id
        from Campaign 
        where Id = :CampaignIds ORDER BY CreatedDate DESC NULLS LAST LIMIT 1
    ]);
    Map<Id, List<Campaign>> cmMap = new Map<Id, List<Campaign>>();
    for (Campaign cm: c) {
        if (cmMap.containsKey(cm.Id)) {
            List<Campaign> x;
            x = cmMap.get(cm.Id);
            x.add(cm);
            cmMap.put(cm.Id, x);
        } else {
            List<Campaign> tmp = new List<Campaign>();
            tmp.add(cm);
            cmMap.put(cm.Id, tmp);
        }
    }
    List<Contact> CON1 = new List<Contact>(); 
    for(Contact newCon: Trigger.new){
        if (cmMap.containsKey(newCon.MostRecentCampaign__c)) {
            for (Campaign cm: cmMap.get(newCon.MostRecentCampaign__c)) {
                CON1.add(
                    New Contact(
                        id=newCon.id)                    
                );
            }
        }
    }
}

 
Hi everyone-we're going to be using a Community page to host our support articles and I'm trying to get rid of all this excess white space on the Article Detail page of our Community:

User-added image

I've already reached out to SFDC support and they've told me this isn't possible without a custom component or CSS. Does anyone have any pointers on how to accomplish this? Would either like to center the article content or enlarge it. Thanks!
Hi all-

We have a custom field on Opportunities called "Referring Account", which is a lookup field related to Accounts. Is it possible to run a query on Opportunities that pulls in every Contact associated to the Account in this custom lookup field (Referring Account) and if so, how would you go about writing it? Thanks!
Hi all-

I'm trying to come up with a way to require every new Opportunity to have a Contact Role associated with it upon creation. The issue I'm running into is the stupid "Recently Viewed" Opportunity List View. It's the only place left that would allow a user to create an Opp without associating a Contact Role. I have a VF page that redirects the user to an error message when trying to create an Opp using this method. 
<apex:page standardController="Opportunity">
    Please create this Opportunity from the associated Contact.
</apex:page>

This works great, except when I now create an Opportunity frome the Contact, there is for some reason no associated Contact Role on the Opp.
User-added image

Would really appreciate any direction here. Thanks!
Hi everyone-I have an interesting business case, so will try to keep this as concise as possible:

We have a business unit with 2 Opportunity record types, called Firm (parent opportunity) and Fund (child opportunity). I created a lookup relationship on the Opportunity object that also looks up to Opportunities. Opportunities of the Fund record type (child) are required to have a value in the lookup field that points to the parent opportunity (Firm).

Here is what I'm trying to solve for: we have a custom Currency field on the Firm record type called Total Contract Value. What I am trying to do is write a trigger that will calculate the sum of all Closed Won Fund opportunity amounts (children), plus the amount of the Firm opportunity (parent), assuming it is Closed Won as well. Would love some direction on what this trigger would look like. Thanks in advance!
Hi everyone-I have an interesting business case, so will try to keep this as concise as possible:

We have a business unit (Investor Services) with 2 Opportunity record types, called Firm (parent opportunity) and Fund (child opportunity). I created a lookup relationship on the Opportunity object that also looks up to Opportunities. Opportunities of the Fund record type (child) are required to have a value in the lookup field that points to the parent opportunity (Firm).

Here is what I'm trying to solve for: we have a custom Currency field on the Firm record type called Total Contract Value. What I am trying to do is write a trigger that will calculate the sum of all Closed Won Fund opportunities (children), plus the amount of the Firm opportunity (parent), assuming it is Closed Won as well. Would love some direction on what this trigger would look like. Thanks in advance!
Hi everyone-

I scheduled an apex job to run daily at midnight but noticed that the job failed due to the Apex CPU time limit exceeded error. The code works in my developer sandbox but not in production. I'm wondering if it is because of something in my SOQL query, but unclear on where the culprit would be. The query only returns about 500 results. Any help is appreciated- here is my class:
 
global without sharing class UnassignAccountsDueToInactivity implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<Account> acctList = [SELECT Id, Name, OwnerId 
                                  FROM Account 
                                  WHERE (OwnerProfileName__c LIKE '%AE%' OR OwnerProfileName__c LIKE '%SDR%') AND 
                                  (Current_Customer__c != 'Current Customer A' AND Current_Customer__c != 'Current Customer B' AND Current_Customer__c != 'Current Customer C') AND
                                  Number_of_Open_Opportunities__c <= 0 AND
                                  DaysSinceLastActivity__c >= 30 AND
                                  DaysSinceLastOwnerChangeDate__c >= 7];
        
        if(!acctList.isEmpty()){
            for(Account acc : acctList){
                acc.DisqualifiedReason__c = 'No response';
                acc.OwnerId = '000000000000000';
            }
            update acctList;
        }
    }
}
Hey everyone-

I have a couple of custom Campaign lookup fields on the Contact object called First Campaign and Most Recent Campaign. These lookup fields need to get populated with the ID of the appropriate Campaign (should be pretty self explanatory-First Campaign would lookup to the first Campaign that the Contact was added to, Most Recent Campaign would lookup to the most recent Campaign that the Contact was added to). This action is supposed to take place when either of a couple of other custom Date/Time fields (Most Recent PQC Date/Most Recent Upsell PQC Date) on the Contact are updated. I would really appreciate help figuring out why the code isn't working as intended and what the fix is. Here is the trigger:
trigger ContactTrigger on Contact ( after update) {

    if(Trigger.isUpdate && Trigger.isafter && RecursionClass.fireContactUpdate)
    {
        ContactTriggerHandler hndl = new ContactTriggerHandler ();  
        hndl.UpdateContact(trigger.OldMap ,trigger.NewMap);
    }
}

Here is the handler class:
public class ContactTriggerHandler {
    ContactTriggerAction act = new ContactTriggerAction();
    public void UpdateContact(Map<id ,Contact> OldMap , Map<id , Contact> NewMap)
    {
        act.UpdateContactCampaignField(OldMap,NewMap);
    }
}

Here is the class that performs the action:
public class ContactTriggerAction {
    
    //METHOD TO SET CAMPAIGN NAME ON CONTACT IF PQC DATE FIELDS ARE UPDATED
    public void UpdateContactCampaignField(Map<id ,Contact> oldMap , Map<id , Contact> newMap)
    {
        List<Id> filteredContactIds = new List<Id>();
        
        //check for fields if updated
        for(Contact newCon :newMap.values())
        {
           Contact oldCon = oldMap.get(newCon.id);
           
           system.debug('___newCon__ContactStatus__c___'+newCon.ContactStatus__c);
           
           //FILTER CONTACTS WHOSE MostRecentPQCDate OR MostRecentUpsellPQCDate IS UPDATED 

           if( (newCon.MostRecentMQCDate__c!=null && oldCon.MostRecentMQCDate__c != newCon.MostRecentMQCDate__c) || (newCon.MostRecentUpsellMQCDate__c !=null && oldCon.MostRecentUpsellMQCDate__c!= newCon.MostRecentUpsellMQCDate__c) )
               filteredContactIds.add(newCon.Id);
        }
        
        system.debug('___filteredContactIds____'+filteredContactIds+'__current__userId___'+UserInfo.getUserId());
        
        //Functionality doesn't run if trigger is fired by integration user to avoid assignment issues
        if(filteredContactIds!=null && filteredContactIds.size()>0 && UserInfo.getUserId() != System.Label.Integration_User_Id)
            CheckRecentCampaignmembers(filteredContactIds);        
    }
    
    //METHOD TO SET RECENT CAMPAIGN AND FRIST CAMPAIGN ON CONTACT 
    @future
    public static void CheckRecentCampaignmembers(List<Id> contactIdLst)
    {
    
        system.debug('___entered____CheckRecentCampaignmembers____');
             
        List<Contact> cLstToUpdate = new List<Contact>();
        
        //SETTING MostRecentCampaign AND FirstCampaign FIELDS OF CONTACT WITH LastModified CAMPAIGNID
        //FETCH CAMPAIGN MEMBER RECORDS TO WHICH CONTACT IS LINKED 
        for(Contact cont : [Select Id, Name, FirstCampaign__c, MostRecentCampaign__c ,(Select Id, CampaignId , HasResponded from CampaignMembers where HasResponded=TRUE ORDER BY LastModifiedDate desc limit 1) from Contact where Id In :contactIdLst])
        {    
        
            Contact con_update = new Contact(Id = cont.Id);
            
           // system.debug('______cont_____'+cont+'____size____'+cont.CampaignMembers.size()+'___campaignId____'+cont.CampaignMembers[0].CampaignId);
            
            //UPDATING MostRecentCampaign AND FirstCampaign FIELDS ON CONTACT WITH CAMPAIGN NAME
            if(cont.CampaignMembers.size()>0){
                
                con_update.MostRecentCampaign__c  = cont.CampaignMembers[0].CampaignId;
                
                //CHECKS IF FIRST CAMPAIGN FIELD IS BLANK
                if(cont.FirstCampaign__c == null)
                {
                    con_update.FirstCampaign__c = cont.CampaignMembers[0].CampaignId;  
                }
            }
            
            system.debug('____con_update_____'+con_update);
            
            cLstToUpdate.add(con_update);

        }
        
        if(cLstToUpdate!=null && cLstToUpdate.size()>0){
            RecursionClass.fireContactUpdate = false;
            update cLstToUpdate ;

        }
    }
      
}

And here is the recursion class:
Public class RecursionClass{
    
    public static Boolean fireContactUpdate = true;
}
Thanks again for your help!
​​​​​​​​​​​​​​
Hey everyone-I'm trying to figure out the best way to transfer Account ownership of certain accounts once a day. I would use declarative tools but can't because the data that would fire the automation is coming from a couple of custom formula fields. I'm confused whether I need to write a scheduled class, a batch class or a scheduled batch class. I've gone through trailhead and sfdc help docs and am still unclear on how to accomplish this. Any pointers on how to get started are much appreciated.
Hey everyone-trying to write a simple trigger for populating a couple of custom lookup fields on Contacts. We have 2 custom fields called "First Campaign" and "Most Recent Campaign". I'm trying to get "First Campaign" populated with the first campaign the Contact was added to and "Most Recent Campaign" with the latest campaign they've been added to. Here's what I have so far, would love any help with this. Thanks in advance!
trigger FirstAndMostRecentCampaignOnContact on Contact (after insert, after update) {

    Set<Id> CampaignIds = new Set<Id>();
    for(Contact CON: Trigger.new) CampaignIds.add (CON.MostRecentCampaign__c);
    List<Campaign> c= new List<Campaign>([
        select 
        Id
        from Campaign 
        where Id = :CampaignIds ORDER BY CreatedDate DESC NULLS LAST LIMIT 1
    ]);
    Map<Id, List<Campaign>> cmMap = new Map<Id, List<Campaign>>();
    for (Campaign cm: c) {
        if (cmMap.containsKey(cm.Id)) {
            List<Campaign> x;
            x = cmMap.get(cm.Id);
            x.add(cm);
            cmMap.put(cm.Id, x);
        } else {
            List<Campaign> tmp = new List<Campaign>();
            tmp.add(cm);
            cmMap.put(cm.Id, tmp);
        }
    }
    List<Contact> CON1 = new List<Contact>(); 
    for(Contact newCon: Trigger.new){
        if (cmMap.containsKey(newCon.MostRecentCampaign__c)) {
            for (Campaign cm: cmMap.get(newCon.MostRecentCampaign__c)) {
                CON1.add(
                    New Contact(
                        id=newCon.id)                    
                );
            }
        }
    }
}

 
Hi all-

We have a custom field on Opportunities called "Referring Account", which is a lookup field related to Accounts. Is it possible to run a query on Opportunities that pulls in every Contact associated to the Account in this custom lookup field (Referring Account) and if so, how would you go about writing it? Thanks!