You need to sign in to do that
Don't have an account?
Savitha
Help in FIND functionality in Apex Trigger
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
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
I highly suggest we re-write the trigger as I see that this trigger is not bulkified and you'll easily hit the governor limit if multiple cases gets created at one time.
I've tried my best to re-factor it for you...(it might not be prefect but let me know if it works or doesn't)
But I'm getting an Error Compile Error: Invalid initial value type Id for LIST<Id> at line 34 column 42