3

i want to insert an event record...but like i have more than 1 lead..i want to insert more than 1 leads as an invitee...

Like using whoid we can insert 1 record ... Using the standard interface we can click on add to invitees button and add multiple invitees..How to add multiple using code so that it shows below on salesforce events...any help will be appreciated...

Event e= new Event();
e.Subject='Call';
e.ownerid=userinfo.getuserid();
e.StartDateTime=datetime.now().adddays(5);
e.EndDateTime=datetime.now().adddays(5);
e.WhatId=recid;
**//e.WhoId=**
insert e;

2 Answers 2

2

The WhoId is not intended to be used for invitees. It's typically more related to use for pointing to fields that can relate the Event to a customer or else an internal business account/program (think in terms something that can help point to a Record Type). For example, the WhoId might be the Contact.Id (of the lead) which would have an Account associated with it. If you want to invite multiple invitees, you probably want to generate an email to all of the invitees. If you need help with how to do that, I can provide you with a snippet of code (see below).

Depending on the org, the WhatId is going to be a Lookup to something like one of the following: a Contract, Campaign, Account, Opportunity, Product, Asset, Case, Solution, Merchandise, Restaurant, Position, Candidate, Job Application, Review, Job Posting, etc. You'll need to look in App Setup->Customize->Activities->Event Fields to see what lookups are defined for the WhatId on Events in your org. Ideally, this would be the place to reference a Campaign or other program that you're using to fund the event that allows you to invite multiple leads.

You don't need both a Who and a What in order to create an Event unless your org requires both of them. Look to see if they're both required while you're checking the fields. If they are, you could do a InsertResults = Database.Insert(ToInsert,false); type of operation which would allow you to create the event without having all of the required fields.

Here's a class you can call from your trigger to send the emails or just grab portions of the code and incorporate it directly:

/* ============================================================
* Contributor: Caleb Sidel
* 
* This code is part of the "apex-lang" open source project avaiable at:
* 
*      http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0.  You may obtain a 
* copy of the License at:
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
global class EmailUtils {

global static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Id> attachmentIDs) {
    List<Attachment> stdAttachments = [SELECT id, name, body FROM Attachment WHERE Id IN:attachmentIDs];
    sendEmailWithStandardAttachments(recipients, emailSubject, body, useHTML, stdAttachments);
}

global static void sendEmailWithStandardAttachments(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Attachment> stdAttachments) {
    List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();

    for(Attachment attachment : stdAttachments) {
        Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
        fileAttachment.setFileName(attachment.Name);
        fileAttachment.setBody(attachment.Body);
        fileAttachments.add(fileAttachment);
    }
    sendEmail(recipients, emailSubject, body, useHTML, fileAttachments);
}

global static void sendTextEmail(List<String> recipients,String emailSubject,String textBody) { 
    sendEmail(recipients, emailSubject, textBody, false, null);
}

global static void sendHTMLEmail(List<String> recipients,String emailSubject,String htmlBody) { 
    sendEmail(recipients, emailSubject, htmlBody, true, null);
}

global static void sendEmail(List<String> recipients,String emailSubject,String body,Boolean useHTML,List<Messaging.EmailFileAttachment> fileAttachments) { 
    if(recipients == null) return;
    if(recipients.size() == 0) return;
    // Create a new single email message object
    // that will send out a single email to the addresses in the To, CC & BCC list.
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();        
    //the email is not saved as an activity.
    mail.setSaveAsActivity(false);
    // Assign the addresses for the To lists to the mail object.
    mail.setToAddresses(recipients);          
    // Specify the subject line for your email address.
    mail.setSubject(emailSubject);
    // Set to True if you want to BCC yourself on the email.
    mail.setBccSender(false);
    // The email address of the user executing the Apex Code will be used.
    mail.setUseSignature(false);
    if (useHTML) {
        // Specify the html content of the email.
        mail.setHtmlBody(body);
    } else {
        // Specify the text content of the email.
        mail.setPlainTextBody(body);
    }
    // Specify FileAttachments
    if(fileAttachments != null && fileAttachments.size() > 0) {
        mail.setFileAttachments(fileAttachments);
    }
    // Send the email you have created.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

/**
 * null     => false
 * ''       => false
 * ' '      => false
 * 'x'      => false
 * 'x@'     => false
 * 'x@x'    => false
 * '[email protected]'  => true
 */
global static Boolean isValidEmailAddress(String str){
    if(str != null && str.trim() != null && str.trim().length() > 0){
        String[] split = str.split('@');
        if(split != null && split.size() == 2){
            split = split[1].split('\\.');
            if(split != null && split.size() >= 2){
                return true;
            }
        }
    }
    return false;
}

global static Boolean isNotValidEmailAddress(String str){
    return !isValidEmailAddress(str);
}

}
1

Only one Lead can be added to a Task/Event - even when using the "ManyWho" feature of multiple contacts on one Lead/Event, also known as "Shared Activities." http://www.salesforce.com/us/developer/docs/object_reference/Content/sforce_api_objects_taskrelation.htm or http://www.salesforce.com/us/developer/docs/object_reference/Content/sforce_api_objects_taskrelation.htm.

"Events can have up to two relationships when relating to Leads: one Lead and one other relationship to Account, Asset, Campaign, Case, Contract, Opportunity, Product, Solution, or custom object. The EventRelation object represents this relationship."

I've not seen anything about event invites via the API, and can't find any docs on the object reference documentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .