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
bomboobomboo 

class/trigger

Hello everyone,

 

I'm completely new to apex, I didn't do any Java development before, and I have almost zero experience with object-oriented programming. So, during an excercise within salesforce I encountered a problem: I want to use a function of a class that I have written here: 

 

public class InvitationSender 
{
    public void sendInvitation(Invitations__c invitation) 
    {   
        [lots of code]
    }
}
 

 Now, what I want to do is to use that sendInvitation function when a record within my invitations__c object is inserted. So I use this code: 

 

trigger SendEmailInvitation on Invitations__c (after insert) {

	InvitationSender.sendInvitation[] invitation = new InvitationSender.sendInvitation();
	
	InvitationSender.sendInvitation(invitation);
	

}

 ...And the Force.com IDE just says "Save error: invalid type: InvitationSender.sendInvitation.

 

Can anyone help me out on this? I know this might be a stupid question, but I am really inexperienced, so please be kind ;-). 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard
You'd need to rewrite the method to process a number of invites at once. Otherwise, if you have a number of records passed into a trigger, e.g. a update from the data loader, then your SOQL queries embedded into the method will be executed for each record and you would breach the limit of 100 queries per transaction.

All Answers

bob_buzzardbob_buzzard

You are trying to instantiate the method rather than the class.  You should just instantiate the class and call the method. Also, your method looks like it only takes a single instance of invitation __c  - you should probably make this accept an array, otherwise you will have to iterate the trigger records.  The code below assumes it can take a list of records:

 

trigger SendEmailInvitation on Invitations__c (after insert) {

	InvitationSender iSender = new InvitationSender();
	
	iSender.sendInvitation(trigger.new);
	

}

 If it can't, you'll need something like the following, but this probably won't scale:

 

trigger SendEmailInvitation on Invitations__c (after insert) {


for (Invitations__c ivite : trigger.new)
{ InvitationSender iSender = new InvitationSender(); iSender.sendInvitation(ivite); } }
bomboobomboo

For a better understanding, I'll just post the whole sendInvitation-Code here. It's commentated, and since it's beginner's code, it should not be hard to understand: 

 

/** 
 * ApexClass designed to send an invitation email to CRM-registered contacts to events of the Awesomo AG
 * It is triggered, when a new record within the Invitations__c custom object is created. 
 * 
 */
public class InvitationSender 
{
    /**
     * This function shall send a single invitation to a single record from the invitation object. 
     * It queries the email template that shall be used.
     * It queries the record entry that shall receive the invitation.
     * After the queries, it sends the email to the record.
     */
    public void sendInvitation(Invitations__c invitation) 
    {   
        EmailTemplate invitationEmailTemplate;
        Invitations__c invitee;
        
        // building of a email that is sent to administrator if query fails
        Messaging.SingleEmailMessage exception_notification = new Messaging.SingleEmailMessage();
        
        String[] toAdmin = new String[] {'email@of_the_admin.com'};
		exception_notification.setToAddresses(toAdmin);
		exception_notification.setSubject('Invitation could not be sent!');
		exception_notification.setPlainTextBody('Something went wrong while sending an invitation to a customer. Please have      
a look at it.'); // queries try { invitationEmailTemplate = [SELECT id FROM EmailTemplate WHERE name = 'AwesomeEventInvitation' LIMIT 1]; } catch(System.QueryException e) { Messaging.sendEmail(new Messaging.SingleEmailMessage[] {exception_notification} ); } try { invitee = [SELECT id, Contact__c, Contact__r.Email FROM Invitations__c ORDER BY createdDate DESC LIMIT 1]; } catch(System.QueryException e) { Messaging.sendEmail(new Messaging.SingleEmailMessage[] {exception_notification} ); } // build email with parameters queried above, send email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toInvitee = new String[] { invitee.Contact__r.Email }; mail.setToAddresses(toInvitee); mail.setTemplateId(invitationEmailTemplate.id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }

 

At what point can I make this method accept an array? 

bob_buzzardbob_buzzard
You'd need to rewrite the method to process a number of invites at once. Otherwise, if you have a number of records passed into a trigger, e.g. a update from the data loader, then your SOQL queries embedded into the method will be executed for each record and you would breach the limit of 100 queries per transaction.
This was selected as the best answer