• Savitha
  • NEWBIE
  • 0 Points
  • Member since 2014

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

An Email to Case is Created. The email sender address is having the Address like '0234155555_fax@cws-boco.com', the system should find the contacts having the Phone or Fax number as '+49 234155555', that contact and its account should be associated with the Created case.

Right now contact is assigning properly. but at one point when 2 contacts are having same phone number and fax number, then I want the case to be associated with only the Fax matching Contact.

Here is my code:
trigger caseContactAccount on Case (before insert, before update) {

 
    list<string> sendaddr1=new list<string>();
    list<Contact> contactList=new list<Contact>();
    list<EmailMessage> em=new list<EmailMessage>();
 
    for(case c:Trigger.new){         
        //if(c.Origin=='Fax'){
           em=[Select Id, FromAddress, ParentId from EmailMessage where ParentId = : c.id];      
       // }
    if(!em.isempty()){
        for(EmailMessage i: em){
        Boolean isMatched = Pattern.matches('[0][0-9]+_fax@cws-boco.com', em[0].FromAddress);          
        system.debug('formattttt checccck'  +isMatched);
      
        if(isMatched){
             
                string code=em[0].FromAddress;
                system.debug('code entering' + code);
                Integer startIndex = code.indexOf('0')+1;
                Integer endIndex = code.indexOf('_');
                sendaddr1.add(code.substring(startIndex,endIndex));             
                code='+49'+sendaddr1[0];
                system.debug('++++++++++++ code ++++++++++---' +code);
              
                try{
                List<List<SObject >>searchList = [FIND :code RETURNING Contact(Id,Name,MobilePhone,Fax,Email)];
                contactList = ((List<Contact>)searchList[0]);              
                Id Conid=contactList[0].Id;
                system.debug('+++++Contact Id++++++' + Conid);
                if(conid!= null){                    
                c.Contactid=Conid;
                update c;
                }else {}
                }
              
                catch(Exception e) {
                        system.debug('Exception Occurred : ');
                }
        }else {}
       }
    }else {}
  
  }
}


Please help me this piece of code.

Kindly help

  
Can someone help me in writing the test class for this Apex class..

public class ClsChatterfeed{

    public static void accountCreditCheck(Map<Id, Account> newAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        for (Account acc : newAccountMap.values()) {
            if(acc.Please_do_Credit_Check__c == true) {
                AccountCreditCheckPendingIds.add(acc.Id);
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void accountCreditCheck(Map<Id, Account> newAccountMap, Map<Id, Account> oldAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        if(Trigger.isUpdate && Trigger.IsAfter) {
            for (Account acc : newAccountMap.values()) {
                if(oldAccountMap.get(acc.Id).Please_do_Credit_Check__c != acc.Please_do_Credit_Check__c && acc.Please_do_Credit_Check__c == true) {
                    AccountCreditCheckPendingIds.add(acc.Id);
                }
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void creditCheckUpdate(Set<Id> AccountCreditCheckPendingIds) {
        if(!AccountCreditCheckPendingIds.IsEmpty()) {
            List<Account> accList = new List<Account>();
            for(Account acc : [Select Id, Name, Last_Credit__c, Credit_Check_Requestor1__c from Account where Id =: AccountCreditCheckPendingIds]) {
                acc.Credit_Check_Requestor1__c = UserInfo.getUserId();
                accList.add(acc);
            }
            if (!(accList.isEmpty())) {
                try {
                    update accList;
                } catch(Exception e) {
                }
            }
        }
    }
   
    public static void feedItemCreate(Set<Id> AccountCreditCheckPendingIds) {
        if(runOnce()) {
            if(!AccountCreditCheckPendingIds.IsEmpty()) {
                List<FeedItem> feedItemList = new List<FeedItem>();
                for(Account acc : [Select Id, Name, Last_Credit__c, SystemModstamp, Credit_Check_Requestor1__c, Please_do_Credit_Check__c from Account where Id =: AccountCreditCheckPendingIds]) {
                    if(acc.Last_Credit__c != null && acc.Credit_Check_Requestor1__c != null && acc.Please_do_Credit_Check__c == true) {
                        FeedItem fitem = new FeedItem();
                        fitem.type = 'LinkPost';
                        fitem.ParentId = acc.Credit_Check_Requestor1__c;
                        fitem.LinkUrl = '/' + acc.id;
                        fitem.Title = acc.Name + ' Account';  //This is the title that displays for the LinkUrl
                        fitem.Body = ('Last Credit has been entered at '+acc.SystemModstamp);
                        feedItemList.add(fItem);
                    }
                }
                if(feedItemList.size() > 0 && !feedItemList.isEmpty()) {
                    try {
                        insert feedItemList;
                    } catch(Exception e) {
                        system.debug('Exception Occurred : '+feedItemList);
                    }
                }
            }
        }
    }
   
    private static boolean recursiveCheck = true;
    public static boolean runOnce() {
        if(recursiveCheck){
            recursiveCheck = false;
            return true;
        }else{
            return recursiveCheck;
        }
    }
}
(REGEX(Phone, "\\D*?(\\d\\D*?){10}")

please let me know what the above expression does
Hi All,

An Email to Case is Created. The email sender address is having the Address like '0234155555_fax@cws-boco.com', the system should find the contacts having the Phone or Fax number as '+49 234155555', that contact and its account should be associated with the Created case.

Right now contact is assigning properly. but at one point when 2 contacts are having same phone number and fax number, then I want the case to be associated with only the Fax matching Contact.

Here is my code:
trigger caseContactAccount on Case (before insert, before update) {

 
    list<string> sendaddr1=new list<string>();
    list<Contact> contactList=new list<Contact>();
    list<EmailMessage> em=new list<EmailMessage>();
 
    for(case c:Trigger.new){         
        //if(c.Origin=='Fax'){
           em=[Select Id, FromAddress, ParentId from EmailMessage where ParentId = : c.id];      
       // }
    if(!em.isempty()){
        for(EmailMessage i: em){
        Boolean isMatched = Pattern.matches('[0][0-9]+_fax@cws-boco.com', em[0].FromAddress);          
        system.debug('formattttt checccck'  +isMatched);
      
        if(isMatched){
             
                string code=em[0].FromAddress;
                system.debug('code entering' + code);
                Integer startIndex = code.indexOf('0')+1;
                Integer endIndex = code.indexOf('_');
                sendaddr1.add(code.substring(startIndex,endIndex));             
                code='+49'+sendaddr1[0];
                system.debug('++++++++++++ code ++++++++++---' +code);
              
                try{
                List<List<SObject >>searchList = [FIND :code RETURNING Contact(Id,Name,MobilePhone,Fax,Email)];
                contactList = ((List<Contact>)searchList[0]);              
                Id Conid=contactList[0].Id;
                system.debug('+++++Contact Id++++++' + Conid);
                if(conid!= null){                    
                c.Contactid=Conid;
                update c;
                }else {}
                }
              
                catch(Exception e) {
                        system.debug('Exception Occurred : ');
                }
        }else {}
       }
    }else {}
  
  }
}


Please help me this piece of code.

Kindly help

  
Can someone help me in writing the test class for this Apex class..

public class ClsChatterfeed{

    public static void accountCreditCheck(Map<Id, Account> newAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        for (Account acc : newAccountMap.values()) {
            if(acc.Please_do_Credit_Check__c == true) {
                AccountCreditCheckPendingIds.add(acc.Id);
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void accountCreditCheck(Map<Id, Account> newAccountMap, Map<Id, Account> oldAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        if(Trigger.isUpdate && Trigger.IsAfter) {
            for (Account acc : newAccountMap.values()) {
                if(oldAccountMap.get(acc.Id).Please_do_Credit_Check__c != acc.Please_do_Credit_Check__c && acc.Please_do_Credit_Check__c == true) {
                    AccountCreditCheckPendingIds.add(acc.Id);
                }
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void creditCheckUpdate(Set<Id> AccountCreditCheckPendingIds) {
        if(!AccountCreditCheckPendingIds.IsEmpty()) {
            List<Account> accList = new List<Account>();
            for(Account acc : [Select Id, Name, Last_Credit__c, Credit_Check_Requestor1__c from Account where Id =: AccountCreditCheckPendingIds]) {
                acc.Credit_Check_Requestor1__c = UserInfo.getUserId();
                accList.add(acc);
            }
            if (!(accList.isEmpty())) {
                try {
                    update accList;
                } catch(Exception e) {
                }
            }
        }
    }
   
    public static void feedItemCreate(Set<Id> AccountCreditCheckPendingIds) {
        if(runOnce()) {
            if(!AccountCreditCheckPendingIds.IsEmpty()) {
                List<FeedItem> feedItemList = new List<FeedItem>();
                for(Account acc : [Select Id, Name, Last_Credit__c, SystemModstamp, Credit_Check_Requestor1__c, Please_do_Credit_Check__c from Account where Id =: AccountCreditCheckPendingIds]) {
                    if(acc.Last_Credit__c != null && acc.Credit_Check_Requestor1__c != null && acc.Please_do_Credit_Check__c == true) {
                        FeedItem fitem = new FeedItem();
                        fitem.type = 'LinkPost';
                        fitem.ParentId = acc.Credit_Check_Requestor1__c;
                        fitem.LinkUrl = '/' + acc.id;
                        fitem.Title = acc.Name + ' Account';  //This is the title that displays for the LinkUrl
                        fitem.Body = ('Last Credit has been entered at '+acc.SystemModstamp);
                        feedItemList.add(fItem);
                    }
                }
                if(feedItemList.size() > 0 && !feedItemList.isEmpty()) {
                    try {
                        insert feedItemList;
                    } catch(Exception e) {
                        system.debug('Exception Occurred : '+feedItemList);
                    }
                }
            }
        }
    }
   
    private static boolean recursiveCheck = true;
    public static boolean runOnce() {
        if(recursiveCheck){
            recursiveCheck = false;
            return true;
        }else{
            return recursiveCheck;
        }
    }
}