I have 2 triggers and i would like to have the following:

When I create a Task from email to salesforce, i want the attachments in the email not saved to the task but to the customer document which is a custom object with attachments, therefore i tried to first create a customer document when i create a new case and then i would like to have the id from the customer document object in the task, but this doesn´t seemed to work actually, any idea?

First trigger to create Customer Document

trigger NewCD on Task (after insert) {

for (Task a : trigger.new){
    if(a.CreatedById == '00520000000mT0D' ){
      Customer_Document__c newCD = New Customer_Document__c (
        Documentname__c = 'Email Attachment from ' + date.today(),
        no_expire_date__c = true,
        IssueDate__c = date.today(),
        Task_ID__c =a.Id,
          Lead__c = a.WhoId,
        Account__c = a.WhatId);
        insert newCD;

Second trigger to pull ID from Customer Document to Task

trigger SetCDIDToTask on Customer_Document__c (after update) {

for (Customer_Document__c a : trigger.new){
           task lookup = [SELECT Id  FROM Task WHERE Issue_Date__c = :a.IssueDate__c and WhoId = :a.Lead__c];
        lookup.Customer_Doc__c = a.Id;
  • What exactly do you want to happen differently than what's happening now? i.e. what errors are being thrown or what undesired behavior are you observing? – Brian Mansfield May 11 '15 at 14:31
  • Hi Bri, it´s not happening, that the id from the customer Document will be thrown to Task. But i would like to have the id from customer document object in the task to get it used for the attachments to be linked to the customer document. But maybe, the whole thing can be placed in a better way, my goal is it to have the attachments from email to salesforce linked in a new customer document object – Ralf Wittenberger May 11 '15 at 14:32
  • @Ralf Wittenberger This is a great resource to help with SOQL governor limits. salesforce.com/us/developer/docs/apex_workbook/Content/… – Berg May 11 '15 at 14:46

You're never updating the record in your second trigger, try:

lookup.Customer_Doc__c = a.Id;
update lookup;

Potentially, it could also be WhoId = :a.Lead__c in your query, depending on how you've defined the field Lead__c. WhoId is a reference field and you need to be using an explicit ID.

As an aside, your trigger needs to be "bulkified," meaning it will quickly hit governor SOQL limits (and fail) in its current state, because you have a SOQL query inside a loop, so you will be performing one query for every record that the trigger is firing for.

Consider the case where someone updates n (hundreds) of records; this one trigger alone will be attempting to do n queries in one execution context (plus any additional others that arise from other code executing in that context - remember, it is common to have multiple triggers on one object type) and the batch will fail.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.