Hi Guys!!
Its been long time since I have written anything. Lets start this time with custom settings. Let me straight away walk you through the scenario.
Scenario:
Lets say you have activated the email to case Functionality in salesforce. For more info about it . click here . I have so many clients account and their respective contacts.
Let me first tell you how the Email to case functionality works. Lets say I have my client named TestClient and correspondingly I have an account for it named as TestClient. Also I have a Contact associated with it. Name = TestContact and its email 'testcontact@smile.com'. Now, if this contact comes and mail and a case is created in my org. So when You go in your org and check the case, it will be associated with a client account i.e testClient also a contact with TestContat. Now what if the email was send by a client that was not CONTACT but is a part of clients company. Then in that case, Account and Contact will be null . I,e case will not be associated with any of the account unless incoming email is from contact .
So we Need to associate the case with account incase if it has no contact.
So Guys, Lets start it with writing a trigger and its trigger handler
Note: This trigger is not Bulkified although it will work properly , Because there are very less chances that we can get so many emails at one time. Still the best practices says that every thing we code should be bulkified .
So here is my architecture of the trigger.
We need to build a custom setting named as domain. where we can add fields on basis of our requirement of filtering it .
I have made a field named as Account which is text field. we will create records like gmail.com, Yahoo.co.in etc. if the company name is TestClient then its email must be xyz@testcleint.com. So in this Case you have to make record with name testcleint.com and Account as Test Client.
Here is a code for you.
Remeber: it is trigger handler.
Its been long time since I have written anything. Lets start this time with custom settings. Let me straight away walk you through the scenario.
Scenario:
Lets say you have activated the email to case Functionality in salesforce. For more info about it . click here . I have so many clients account and their respective contacts.
Let me first tell you how the Email to case functionality works. Lets say I have my client named TestClient and correspondingly I have an account for it named as TestClient. Also I have a Contact associated with it. Name = TestContact and its email 'testcontact@smile.com'. Now, if this contact comes and mail and a case is created in my org. So when You go in your org and check the case, it will be associated with a client account i.e testClient also a contact with TestContat. Now what if the email was send by a client that was not CONTACT but is a part of clients company. Then in that case, Account and Contact will be null . I,e case will not be associated with any of the account unless incoming email is from contact .
So we Need to associate the case with account incase if it has no contact.
So Guys, Lets start it with writing a trigger and its trigger handler
Note: This trigger is not Bulkified although it will work properly , Because there are very less chances that we can get so many emails at one time. Still the best practices says that every thing we code should be bulkified .
So here is my architecture of the trigger.
We need to build a custom setting named as domain. where we can add fields on basis of our requirement of filtering it .
I have made a field named as Account which is text field. we will create records like gmail.com, Yahoo.co.in etc. if the company name is TestClient then its email must be xyz@testcleint.com. So in this Case you have to make record with name testcleint.com and Account as Test Client.
Here is a code for you.
Remeber: it is trigger handler.
trigger CreateAccount on EmailMessage (After insert, After Update){ string [] emails ; List<Account> acct ; Map<String,Domain__c> dommap = Domain__c.getAll(); system.debug('===getall'+Domain__c.getAll()); system.debug('===dommap'+dommap); set<id> sid = new set<id>(); Domain__c ccd = new Domain__c(); For(EmailMessage em : trigger.new){ if(em.Incoming == True ){ emails = em.FromAddress.split('@'); system.debug('===Emails'+emails); system.debug('===dommap.containsKey(emails)'+dommap.containsKey(emails[1])); if(dommap.containsKey(emails[1])){ sid.add(em.ParentId); //dmlst.add(ccd.getvalues(emails[1])); } system.debug('\nsid'+sid); //Domain__c dc = new Domain__c(); //system.debug('=='+dc.getAll().values()); //if(em.FromAddress = dc.getAll()){} //em.FromAddress = } } Domain__c cd ; // system.debug(cd.get(emails[1]).values()); // system.debug(cd.getinstanace(emails[1])); //system.debug(cd.getvalues(emails[1])); acct = new list<Account>() ; //acct = [Select id , name from Account where name =: dommap.get('emails[1]').AnkushTest__Account__c]; system.debug('\nacct'+acct); List<Case> lst = new List<Case>(); lst = [Select id , Accountid from case where id in : sid]; system.debug('==lst'+lst); for(case cs : lst){ lst = new List<Case>(); if(cs.Accountid == Null){ system.debug(string.valueof(dommap.get(emails[1]))); system.debug(dommap.get(emails[1]).AnkushTest__Account__c); string st =string.valueof(dommap.get(emails[1]).AnkushTest__Account__c); system.debug(st); Account acctt = [Select id , name from Account where name = :st]; system.debug(acctt); cs.Accountid = acctt.id ; lst.add(cs); } else{ lst.add(cs);} /*for(Domain__c dm : Domain__c.getall(emails[1])){ system.debug(dommap.get(emails[1])); //if(dm.Incoming == True ){ system.debug('\n==='+dommap.get('emails[1]')); if(cs.Accountid == Null){ //cs.Accountid = dommap.get('emails[1]'); } //} }*/ } update lst; system.debug('==lst'+lst); }
For Any Querries
Contact
ankushsalesforce@gmail.com
No comments:
Post a Comment