• Sam Lau
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
Hi All,

I'm trying create a deletion trigger where I need to update two record attributes with attachment name and size.  I'm still getting use to APEX, and had come across a few examples to help me with the following trigger.   We're still utilizing the Attachment object as well.   Any assistance would be appreciated.   Currently I'm getting many errors on the SOQL query for TaskLst which I can't figure out at the moment.   Thanks!

Sam

trigger AttachmentHCLTask on Attachment (before delete) 

    String tempParentId;
    String tempAttName;
    Integer tempAttSize;
    Set<Id> setParentId = new Set<Id>();
    List<BMCServiceDesk__Task__c> Tasklst = new List<BMCServiceDesk__Task__c>();
    
 for (Attachment HCLTask : trigger.new ) {
            tempParentId = HCLTask.Id;
             tempAttName = HCLTask.Name;
             tempAttSize = HCLTask.BodyLength;
     
            if (tempParentId.left(10) =='a290H00000') {
                System.debug('Debug : found a290H00000');
                System.debug('Debug : Attachment id ' + HCLTask.Id );
                setParentId.add(HCLTask.Id);
            }
        }
    Tasklst = [select Id from BMCServiceDesk__Task__c where Id = setParentId AND HCL_Task_Creation_Sent__c = true];
     
     For(BMCServiceDesk__Task__c e : Tasklst)
     {
        e.HCL_Attachment_Remove_File_Name__c = tempAttName;
        e.HCL_Attachment_Remove_File_Size__c = tempAttSize;
     }

     update Tasklst;
}
Hi All,

I'm attempting to text parse between two tags within the body of an incoming email.   I'm receiving an undeliverable with my tests but can't figure out how to resolve "System.StringException: Ending position out of bounds: 35"

I have my code within the email class as such:   

            //Parse email   
            String EmailStartTag = '[Email]';
            String EmailEndTag = '[/Email]';        
            integer Rstart = Body.indexOf(EmailStartTag);
            integer Rend = Body.indexOf([EmailEndTag);
            integer Rlength = Rend - Rstart;
            NPR.Email__c = Body.substring(Rstart, Rlength); 

I'm not sure why I'd reach an out of bounds since I'm simply taking the ending position integer and subtracting the starting for the length of the substring.   Any pointers would be greatly appreciated.  

Sam
Hi,

I have a custom object which I'm nearing the limit for workflows.  I was wondering if it would be beneficial to consolidate process builder's using the same trigger, which is for the object upon new record, where I would be adding many criteria nodes to 1 workflow.  Essentially it's the same outcome if these were consoildated, but does this decrease the number of workflows used by the object?  Are there any benefits also in terms of processing efficiency to the platform by doing this?  

Any feedback would be helpful.  Thanks.

Sam
Hi All,

I'm trying create a deletion trigger where I need to update two record attributes with attachment name and size.  I'm still getting use to APEX, and had come across a few examples to help me with the following trigger.   We're still utilizing the Attachment object as well.   Any assistance would be appreciated.   Currently I'm getting many errors on the SOQL query for TaskLst which I can't figure out at the moment.   Thanks!

Sam

trigger AttachmentHCLTask on Attachment (before delete) 

    String tempParentId;
    String tempAttName;
    Integer tempAttSize;
    Set<Id> setParentId = new Set<Id>();
    List<BMCServiceDesk__Task__c> Tasklst = new List<BMCServiceDesk__Task__c>();
    
 for (Attachment HCLTask : trigger.new ) {
            tempParentId = HCLTask.Id;
             tempAttName = HCLTask.Name;
             tempAttSize = HCLTask.BodyLength;
     
            if (tempParentId.left(10) =='a290H00000') {
                System.debug('Debug : found a290H00000');
                System.debug('Debug : Attachment id ' + HCLTask.Id );
                setParentId.add(HCLTask.Id);
            }
        }
    Tasklst = [select Id from BMCServiceDesk__Task__c where Id = setParentId AND HCL_Task_Creation_Sent__c = true];
     
     For(BMCServiceDesk__Task__c e : Tasklst)
     {
        e.HCL_Attachment_Remove_File_Name__c = tempAttName;
        e.HCL_Attachment_Remove_File_Size__c = tempAttSize;
     }

     update Tasklst;
}
We would like to see which records have an attachment attached from the list view.  To do this, we created a checkbox field HasAttachment, default unchecked, and the following trigger.  If the Expense custom object record has an attachment, then the HasAttachment field should update to TRUE.  We tested this trigger through 'Upload File' under the Notes and Attachments related list, however, the HasAttachment field remains unchecked.
 
trigger AttachmentonExpense on Attachment (after insert) 
{
Set setParentId = new Set();
List Expenselst = new List();

for(Attachment att: Trigger.new)
{
setParentId.add(att.ParentId);
}

Expenselst = [select Id , HasAttachment__c from Expense__c where Id IN :setParentId];

For(Expense__c e : Expenselst)
{
e.HasAttachment__c = True;
}

update Expenselst;
}

 
Hi All,

I hope you could assist me in achieving my goal here. I am not a coder and just following the patterns on existing codes anywhere in the site. My goals are:
  • Create Opportunity record from incoming emails. (create something like e2c)
  • Create Contact record if there are no matching emails in the system and populate the custom Contact lookup in Opp.
  • When user send an email from that Opportunity record and customer replies back, the response should be attached to existing Opportunity.
Here's what I have:
  • Custom Contact Lookup in Opportunity
  • ThreadID field in Opportunity which I use in Email Subject and Description:

Here is the existing code:

global class OpportunityCreation implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

     Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    
       Contact vCon = [Select Id, Name, Email, AccountId
       From Contact 
       Where Email = :email.fromAddress
       Limit 1];

    Opportunity opportunity = new Opportunity();
    opportunity.Name = email.Subject;
    opportunity.StageName = 'Prospecting';
    opportunity.CloseDate = Date.today();
    opportunity.Email_Body__c = email.plainTextBody;
    opportunity.Contact_Name__c = vCon.Id;
    opportunity.AccountId = vCon.AccountId;
    insert opportunity;
    
       System.debug('====> Created opportunity '+opportunity.Id);
       
      
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created opportunity record
        attachment.ParentId = opportunity.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
      }
    }
  
    return result;

  }

}

Current behavior of the code:
  • It doesn't create an Opportunity if there are no matching email address in the system. (Expected: create Opportunity + Contact if there are no matching email in the system)
  • Email Threads like email to case doesn't work.
Any assistance is greatly appreciated.

  •