• Davy
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
I'm having a long-time issue sending emails to a collection of contacts. I'm using a flow to loop through records in a custom object. Based on the record status I need to send an email to the contact related to the record. I used to do this using the SendEmail class, but I soon ran into governor limits as there are more than 10 related contacts.

I solved this by adding a pauze in the loop, but eventually this also fails as the flow takes too long to execute. Instead of sending the email directly in the flow I removed all the logic from the flow and replaced it with a simple update of each record. This update triggers a Process Builder process, which sends the email. 

User-added image
User-added image
Since this are two seperate processes I assumed I would not run into any governor limits as the Process Builder only sends one email. But it still throws an error: CANNOT_EXECUTE_FLOW_TRIGGER: We can't save this record because the “Upsert View” process failed. Give your Salesforce admin these details. An Apex error occurred: System.LimitException: Too many Email Invocations: 11

I know there is a MassEmailMessage Apex Class that should be able to do what I want, but after spending days trying to get this to work I gave up on Apex classes.

Does anyone have any pointers on how I should fix this? 

 
  • June 09, 2022
  • Like
  • 0
Hello,

I created a VF Page to create new leads, the new lead must be related to an existing Campaign. On the VF Page I want to show the Campaign description. I created a VF Page:
 
<apex:page showheader="false" sidebar="false" standardStylesheets="false" controller="CreateLead" tabStyle="Lead">
  <apex:form>
				  <apex:outputText value="{!Campaign.Description}" escape="false" />
						<apex:pageMessages id="msgs"/>
						<apex:inputfield value="{!Lead.LastName}" html-placeholder="Achternaam" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
						...
						<apex:inputfield value="{!Lead.Email}" html-placeholder="Email adres" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:commandButton rerender="msgs" action="{! save }" value="Save"/>
  </apex:form>
</apex:page>
And an Apex page to get the Campaign description and to save the Lead and set a relation with the Campaign:
 
public class CreateLead {


    public Lead getLead() {
        return lead;
    }
    
				public Lead lead {get;set;}

				public CreateLead (){
								lead = new Lead();
				}
    
    public PageReference save() {
	    insert lead;

        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            CampaignId = '7011r00000'
        );
        insert member; 

        return null;
    }

    private Campaign campaign;

   	public Campaign getCampaign() {
        
        if (campaign == null){                     
	        campaign = [select Name, Description from Campaign where
            id = '7011r00000'];       
        }
        return campaign;
    }


}



This works fine, however I hardcoded the Campaign Id in the Apex class. I want to be able to reuse the Apex class so I want to make the Campaign Id variable. 

How can I pass a Campaign Id from the VF Page to the Apex class when the page is loaded. I found solutions with an additional form on the VF Page that can be used to set the Campaign Id. I would like to define it in the VF Page and make it available in the Apex class when a user loads the page.

Thank you for your help!
  • February 08, 2020
  • Like
  • 0

Hello All,

Apologies for the long question.

I am having a hard time figuring out a (probably) newbie, non-developer issue, since I'm both I have no idea how to fix this issue I am looking for some help here. Hopefully someone is able to point me in the right direction. I'm running into Salesforce Governor Limits, I understand why, but I have no clue how to solve them. 

This is my situation: 
I created 2 new objects: Topic and ContactTopicRelation.
The topic object has a one-to-many relation with the ContactTopicRelation object.
The ContactTopicRelation has the following fields: Topic Id, Contact Id, Status and Text

Through our website we allow Salesforce contacts to share their view on a Topic. Each view is stored in the ContactTopicRelation object (text) with a default status 'posted'.

I created a flow to send out an email confirmation to all Contacts that shared their view. This flow is triggered automatically every 24 hours. Once the confirmation is sent I update the ContactTopicRelation status to 'confirmed'. Each email I send contains a unique text and link, bases on the ContactTopicRelation fields. I'm using the SendEmail Apex class to send this email.

This all worked perfectly fine until I started using this in production and we received more than 10 views within a day, I started to receive a flow error (Too many API email invocations 11). When we receive more than 50 views, I received an additional flow error (Too many SOQL queries: 101).

I tried to find a solution for this and found out I should not send emails within a loop, nor should I update a record within a loop. Batch sending emails and updating records is the answer, however I'm unable to find any documentation or examples how to do this is a flow. I'm sure there is a way to do this using Apex, but since do not have a developers background I prefer to do this in a flow.

In my current flow I do the following:
Get Record: Topic
Get Record: Get all ContactTopicRelations with status 'posted'
Loop: ContactTopicRelations
Get Record: ContactTopicRelations -> Contact (lookup email address)
Apex: Send email with a custom body text to Contact email address
Update Record: update ContactTopicRelations status to 'confirmed'

User-added image

I 'solved' my issues by adding a 'start' input variable to my flow and added a condition to the loop to stop it after 10 iterations. I now manually call the flow and manually set the start point to 1,11,21,31, etc. This works, but it is a manual process and I asume there must be a proper way to do this.

Is there anyone who can point me in the right direction, any example would be great.

Thank you so much!


 
  • June 26, 2019
  • Like
  • 0
I'm having a long-time issue sending emails to a collection of contacts. I'm using a flow to loop through records in a custom object. Based on the record status I need to send an email to the contact related to the record. I used to do this using the SendEmail class, but I soon ran into governor limits as there are more than 10 related contacts.

I solved this by adding a pauze in the loop, but eventually this also fails as the flow takes too long to execute. Instead of sending the email directly in the flow I removed all the logic from the flow and replaced it with a simple update of each record. This update triggers a Process Builder process, which sends the email. 

User-added image
User-added image
Since this are two seperate processes I assumed I would not run into any governor limits as the Process Builder only sends one email. But it still throws an error: CANNOT_EXECUTE_FLOW_TRIGGER: We can't save this record because the “Upsert View” process failed. Give your Salesforce admin these details. An Apex error occurred: System.LimitException: Too many Email Invocations: 11

I know there is a MassEmailMessage Apex Class that should be able to do what I want, but after spending days trying to get this to work I gave up on Apex classes.

Does anyone have any pointers on how I should fix this? 

 
  • June 09, 2022
  • Like
  • 0
Hello,

I created a VF Page to create new leads, the new lead must be related to an existing Campaign. On the VF Page I want to show the Campaign description. I created a VF Page:
 
<apex:page showheader="false" sidebar="false" standardStylesheets="false" controller="CreateLead" tabStyle="Lead">
  <apex:form>
				  <apex:outputText value="{!Campaign.Description}" escape="false" />
						<apex:pageMessages id="msgs"/>
						<apex:inputfield value="{!Lead.LastName}" html-placeholder="Achternaam" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
						...
						<apex:inputfield value="{!Lead.Email}" html-placeholder="Email adres" styleClass="store w-full border bg-gray-100 hover:bg-gray-200 focus:bg-gray-200 outline-none px-3 py-2"/>
						<apex:outputtext value=""></apex:outputtext>
      <apex:commandButton rerender="msgs" action="{! save }" value="Save"/>
  </apex:form>
</apex:page>
And an Apex page to get the Campaign description and to save the Lead and set a relation with the Campaign:
 
public class CreateLead {


    public Lead getLead() {
        return lead;
    }
    
				public Lead lead {get;set;}

				public CreateLead (){
								lead = new Lead();
				}
    
    public PageReference save() {
	    insert lead;

        CampaignMember member = new CampaignMember(
            LeadId = lead.Id,
            CampaignId = '7011r00000'
        );
        insert member; 

        return null;
    }

    private Campaign campaign;

   	public Campaign getCampaign() {
        
        if (campaign == null){                     
	        campaign = [select Name, Description from Campaign where
            id = '7011r00000'];       
        }
        return campaign;
    }


}



This works fine, however I hardcoded the Campaign Id in the Apex class. I want to be able to reuse the Apex class so I want to make the Campaign Id variable. 

How can I pass a Campaign Id from the VF Page to the Apex class when the page is loaded. I found solutions with an additional form on the VF Page that can be used to set the Campaign Id. I would like to define it in the VF Page and make it available in the Apex class when a user loads the page.

Thank you for your help!
  • February 08, 2020
  • Like
  • 0