• Vivek singh 30
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 0
    Replies
public with sharing class RecurssiveCheck {
  public static boolean isUpdateLead=false;

}
global class asCaseAfterSalesEmailService implements Messaging.InboundEmailHandler
 
{
  Id CaseRecordTypeId_ASC = aiCommon.GetRecordTypeId('Case','Complaint');
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) 
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    
        as_After_Sales_Setting__c myCS = as_After_Sales_Setting__c.getValues('ASEscalation');
        
        System.debug('myCS:::::::::    ' + myCS.as_From_Address__c);

        list < String > emailaddress = new List < String > ();
        Set < String > emailaddressSet = new Set < String > ();

        if (myCS.as_From_Address__c.Contains(',')) 
        {
            emailaddress = myCS.as_From_Address__c.split(',');
        } 
        else 
        {
            emailaddress.add(myCS.as_From_Address__c);
        }

        for (String str: emailaddress) 
        {
            emailaddressSet.add(str);
        }

        if (emailaddressSet.contains(email.fromAddress)) 
        {
          if(email.subject.containsIgnoreCase('case#') 
            || email.subject.containsIgnoreCase('vin#')
            || email.subject.containsIgnoreCase('ro#'))
          {
              updateCase(email,true);
          }
          else
          {
            createCase(email, true);
          }
        } 
        else if(email.subject.containsIgnoreCase('case#') 
            || email.subject.containsIgnoreCase('vin#')
            || email.subject.containsIgnoreCase('ro#'))
    {
            updateCase(email,false);
        } 
        else 
        {
            createCase(email, false);

        }
        return result;
    }
  
  private void updateCase(Messaging.InboundEmail email,boolean isFromAI)
  {
    String subject = email.subject;
    list<Case> lstCaseToUpdate = new list<Case>();
    if(email.subject.containsIgnoreCase('case#'))
    {
          List < String > caseValues = email.subject.split('#');
          if (caseValues.get(1) != null) 
          {
              String caseNumber = caseValues.get(1);
              if(caseNumber.length() >=8)
                caseNumber = caseNumber.substring(0, 8);
                
              lstCaseToUpdate = [Select id, Status, CaseNumber,as_Is_Generated_From_AI__c,IsClosed FROM Case WHERE CaseNumber = : caseNumber and RecordTypeId =: CaseRecordTypeId_ASC limit 1];
          }
    }
    else if(email.subject.containsIgnoreCase('vin#'))
    {
      List < String > caseValues = email.subject.split('#');
          if (caseValues.get(1) != null) 
          {
              String strVIN = caseValues.get(1);
              if(strVIN.length() >=17)
                strVIN = strVIN.substring(0, 17);
                
              lstCaseToUpdate = [Select id, Status, CaseNumber,as_Is_Generated_From_AI__c,IsClosed FROM Case WHERE as_VIN__c = : strVIN and RecordTypeId =: CaseRecordTypeId_ASC limit 1];
          }
    }
    else if(email.subject.containsIgnoreCase('ro#'))
    {
      List < String > caseValues = email.subject.split('#');
          if (caseValues.get(1) != null) 
          {
              String strRO = caseValues.get(1);
              if(strRO.length() >=8)
                strRO = strRO.substring(0, 8);
                
              lstCaseToUpdate = [Select id, Status, CaseNumber,as_Is_Generated_From_AI__c,IsClosed FROM Case WHERE as_RO_Number__c = : strRO and RecordTypeId =: CaseRecordTypeId_ASC limit 1];
          }
    }
    
    if(lstCaseToUpdate != null && lstCaseToUpdate.size() > 0)
    {
      Case objCase = lstCaseToUpdate[0];
      if(objCase.IsClosed)
      {
        objCase.Status = 'Re-Open';
        objCase.as_Approval_Status__c = '';
              objCase.as_Is_Generated_From_AI__c = isFromAI;
              update objCase;
              
              CreateEmailMessageRecord(objCase.Id,email);
      }
      else
      {
        if(!objCase.as_Is_Generated_From_AI__c)
        {
          objCase.as_Is_Generated_From_AI__c = isFromAI;
          objCase.as_Approval_Status__c = '';
                update objCase;
                
                CreateEmailMessageRecord(objCase.Id,email);
        }
      }
    }
    else
    {
      createCase(email, isFromAI);
    }
  }
  
    private void createCase(Messaging.InboundEmail email, boolean isFromAI) 
    {
        Case objCase = new Case();
        objCase.RecordTypeId = CaseRecordTypeId_ASC;
        objCase.Status = 'New';
        objCase.Priority = 'High';
        objCase.Subject = email.subject;
        objCase.Description = email.plainTextBody;
        objCase.as_Type_of_Complaint__c = 'After Sales Concern';
        objCase.Origin = 'Email';
        objCase.as_Source__c  = 'Email';
        objCase.as_Complaint_Type__c = 'inbound';
        
        Id OwnerId;
        as_After_Sales_Setting__c myCS = as_After_Sales_Setting__c.getValues('ASEscalation');
        if(myCS.as_Random_Email_Case_Owner__c != null)
          OwnerId = myCS.as_Random_Email_Case_Owner__c;
          
        objCase.as_Is_Generated_From_AI__c = isFromAI ? true : false;
        objCase.as_Email__c = isFromAI ? '' : email.FromAddress;
        
        if(OwnerId != null)
          objCase.Ownerid = OwnerId;
          
        insert objCase;
        
        CreateEmailMessageRecord(objCase.Id,email);
        System.debug('objCase:::::::' + objCase.id);
    }
    
    /*
  This function is used to create new email record under related case
  --------------------------------------------------------------------------------------
  Version#     Date                           Author                    Description
  --------------------------------------------------------------------------------------
  1.0          14-Oct-2014                    Pradip Shukla             Initial Version
  --------------------------------------------------------------------------------------
  */
    private void CreateEmailMessageRecord(Id objCaseId,Messaging.InboundEmail email)
    {
      string strAddress = '';
      EmailMessage objEM = new EmailMessage();
      objEM.ParentId = objCaseId;
      if(email.toAddresses != null && email.toAddresses.size() > 0)
      {
        for(string str : email.toAddresses)
        {
          strAddress += str + ',';
        }
        strAddress = strAddress.removeEnd(',');
      }
      
      objEM.ToAddress = strAddress;
      objEM.TextBody = email.plainTextBody;
      objEM.Subject = email.Subject;
      objEM.Incoming = true;
      objEM.HtmlBody = email.plainTextBody;
      objEM.FromName = email.FromName;
      objEM.FromAddress = email.FromAddress;
      
      strAddress = '';
      if(email.ccAddresses != null && email.ccAddresses.size() > 0)
      {
        for(string str : email.ccAddresses)
        {
          strAddress += str + ',';
        }
        strAddress = strAddress.removeEnd(',');
      }
    objEM.CcAddress = strAddress;
      
      insert objEM;
    }
}