• SF_Admin96
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 6
    Replies
Hi All,

I currently implemented a code (which I copied somewhere) in my Production which copies the email body and insert it to Case Comment. However, I am getting multiple entries when I edit and Save the record. 

Ex. When the email comes in, it copy the email body and the code insert it to case comment. When I click the Edit button and Save WITHOUT changing any information, it creates a duplicate on the case comment. 

How can I correct this behavior that it should only create case comment when a new email is sent or received?

trigger commentMove on Case (after update) {
  Case myCase = trigger.new[0];
  if (myCase.New_Case_Comment__c!= null) {
    String caseId= myCase.ID;
    CaseComment cc = new CaseComment(CommentBody=myCase.New_Case_Comment__c,parentID=caseId);
    insert cc;
  }
}

Any assistance is greatly appreciated.
Hi Experts,

I know that this is not a standard functionality. However, can anyone share a sample code for sending email alert when a new attachment has been added to the custom object.

Here is the specific scenario:

When a new file or multiple files has been added for a custom object, I would like a specific set of users to be notified.

Any assistance is greatly appreciated. Thanks.
Hi All,

I would like to know how can I pre-populate fields based on the lookup value being entered by a user?

For example: I have three fields and those values will be coming from the Parent Object. Whatever value is being entered in the lookup field, it should pre-populate the values on the three fields BEFORE saving the record. How do I do that? I believe it can only be done using APEX.

Is there anyone here tried that? Would you be able share some samples codes please?

Thank you in advance.
Hi All,

Hope you could assist me modifying a code or adding a new one. I currently have email services that creates an Opportunity.  I am not really a coder, I am just looking around how to get this done.

My goal is that when user send an email from that Opportunity record (generated from Email services) and customer replies back (with threadId generated initially), the response of the customer should be attached to existing Opportunity. How can I achieve that? Should I attach it to existing code that I have?

Here is my existing Code that generates Opportunity record:
global class OpportunityCreation implements Messaging.InboundEmailHandler {

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

     Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
     
     String fName = email.fromname.substring(0,email.fromname.indexOf(' '));
     String lName = email.fromname.substring(email.fromname.indexOf(' '));
     
     Contact vCon;
     for(Contact c: [Select Id, Name, Email, AccountId From Contact Where Email = :email.fromAddress Limit 1])
         vCon = c;
     if(vCon == null)
     {
        vCon = new Contact(
        FirstName = fName,
        LastName =  lName,
        Email = email.fromAddress);
        insert vCon;
     }   
     
     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;

  }

}


Thank you in advance.


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.

  •  
Hi Experts,

I know that this is not a standard functionality. However, can anyone share a sample code for sending email alert when a new attachment has been added to the custom object.

Here is the specific scenario:

When a new file or multiple files has been added for a custom object, I would like a specific set of users to be notified.

Any assistance is greatly appreciated. Thanks.
Hi All,

I currently implemented a code (which I copied somewhere) in my Production which copies the email body and insert it to Case Comment. However, I am getting multiple entries when I edit and Save the record. 

Ex. When the email comes in, it copy the email body and the code insert it to case comment. When I click the Edit button and Save WITHOUT changing any information, it creates a duplicate on the case comment. 

How can I correct this behavior that it should only create case comment when a new email is sent or received?

trigger commentMove on Case (after update) {
  Case myCase = trigger.new[0];
  if (myCase.New_Case_Comment__c!= null) {
    String caseId= myCase.ID;
    CaseComment cc = new CaseComment(CommentBody=myCase.New_Case_Comment__c,parentID=caseId);
    insert cc;
  }
}

Any assistance is greatly appreciated.
Hi Experts,

I know that this is not a standard functionality. However, can anyone share a sample code for sending email alert when a new attachment has been added to the custom object.

Here is the specific scenario:

When a new file or multiple files has been added for a custom object, I would like a specific set of users to be notified.

Any assistance is greatly appreciated. Thanks.
Hi All,

I would like to know how can I pre-populate fields based on the lookup value being entered by a user?

For example: I have three fields and those values will be coming from the Parent Object. Whatever value is being entered in the lookup field, it should pre-populate the values on the three fields BEFORE saving the record. How do I do that? I believe it can only be done using APEX.

Is there anyone here tried that? Would you be able share some samples codes please?

Thank you in advance.
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.

  •