• NavRam
  • NEWBIE
  • 0 Points
  • Member since 2018

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

Can some one help me with the test case for this trigger?
When an email to case is created it will search for contact based on email id and stamp the contact if found if not it will create a contact and stamp the value on case.


trigger TriggertoCreateContactformCase on Case (before insert) {
    List<String> UseremailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedEmail!=''|| c.SuppliedEmail==null)
        {
            UseremailAddresses.add(c.SuppliedEmail);
        }
    }

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    List<Contact> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
    Set<String> ExstingEmails = new Set<String>();
    for (Contact c:listofallContacts) {
        ExstingEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedName!=null &&
            c.SuppliedEmail!=null &&
            c.SuppliedName!='' &&
           !c.SuppliedName.contains('@') &&
            c.SuppliedEmail!='' &&
           !ExstingEmails.contains(c.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] Emailheader = c.SuppliedName.split(' ',2);
            if (Emailheader.size() == 2)
            {
                Contact conts = new Contact(FirstName=Emailheader[0],
                                            LastName=Emailheader[1],
                                            Email=c.SuppliedEmail
                                            );
                emailToContactMap.put(c.SuppliedEmail,conts);
                casesToUpdate.add(c);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case c:casesToUpdate) {
        Contact newContact = emailToContactMap.get(c.SuppliedEmail);
        
        c.ContactId = newContact.Id;
    }
}


 
I have configured Email-to-Case functionality to my sandbox and for routing address customers will send emails with their queries, while sending emails they may include some screenshots .

Sample Email Content: 


Hi this is google image and below image screen shot

 User-added image

What Salesforce functionality happen here is by defualt email text will be populated to description field and screen shot will get stored into attachments. and under case object we do have Emails related list where we can see original email content which customer has sent.

But the problem is my customer wants to see original content which customers has sent with in the description field .

So I have created one custom description field and datatype is Rich text area. 

Customization i have done : 

Created workflow rule on EmailMessage Object and tried to populate EmailMessage object HtmlBody field into Case object description (Custome field ) .
text is populated but images are not able to see and some empty textboxes i can see like below.
User-added image


So above solution is not working and again i have done some tirgger on EmailMessage object to update htmlbody into desc field

like below : 

first i put event after insert only but still its doesnt work for me so again i put all the events but no luck.

trigger UpdateDescTrigger on EmailMessage (after insert,after update,before insert ,before update) {
    //set<id> CaseIds = new set<Id>();
    map<Id,EmailMessage> MapEmailMessage = new map<Id,EmailMessage>();
    for(EmailMessage e:trigger.new){
        MapEmailMessage.put(e.ParentId,e);
    }
   
    list<Case> lstC = [select Id,Description__c from Case where id in:MapEmailMessage.keyset()];
    for(Case c:lstC){
        if(MapEmailMessage != null && MapEmailMessage.get(c.Id) != null )
            c.Description__c = MapEmailMessage.get(c.Id).HtmlBody;
    }
    update lstC;
   
}


My question is : i want to automatically update HTMLBody of EmailMessage into Case Description__c (Custom field)

But if i edit case record manually  and saved its working for me when i have done below trigger.


trigger updateCasse on Case (before update) {
    set<Id> parentIds = new set<Id>();
    map<Id,EmailMessage> mapEmailMessage = new map<Id,EmailMessage>();
    for(Case c:trigger.new){   
        parentIds.add(c.Id);
    }   
    list<EmailMessage> lste = [select id,HtmlBody,parentId from EmailMessage where parentid in:parentIds and Incoming = true];
   
    if(lste.size() > 0 ){
     for(EmailMessage e:lste){
         mapEmailMessage.put(e.parentId,e);
     }
     list<Case> lstC = new list<Case>();
     for(Case c:trigger.new){ 
      if(mapEmailMessage != null && mapEmailMessage.get(c.Id) != null)   
          c.description__c = mapEmailMessage.get(c.Id).HtmlBody;       
     }
    }
}

Plz help me.