Thursday 23 October 2014

Trigger On Attachment Object Salesforce!!

Hi Guys!!

Hope you doing well.

One of my blog reader asked me to share code of trigger on Attachment object . Although , I have already written a trigger on attachment object. For the same click here. but this time I have got a different scenario.

Caution Notice :P :- This blog is meant for beginners like me, who are still learning  salesforce or must say it is for those who are loving salesforce.

Scenario:-  When an attachment is inserted under an account or a contact or Lead  etc.Now, on the basis of a custom field on parent of attachment object, it should check whether any other record exist with same value of field . If there exist some record then this lead should get deleted and and the attachment should be attached to older one. 

So , lets begin with it . Lets Say , I have acustom field on Lead object name as My_name__c on lead objec. On the basis of this field I ll check whether there exist some records or not. so lets Jump to code .




trigger DeleteLeadIFNewMatchesOld on Attachment (Before insert) {
    Set<Id> setOfNewLead = new Set<Id>();
    Set<String> setOfMyname = new Set<String>();
    Set<Id> setOfDeleteLeads = new Set<Id>();
    for(attachment a : trigger.new ){
        setOfNewLead.add(a.ParentId);
    }
    Map<Id,lead> mapOfNewLead = new Map<Id,Lead>([Select id , My_name__c from Lead where id in :  setOfNewLead]);
    Map<Id,Lead> mapOfOldLead = new Map<Id,Lead>([Select id , My_name__c from Lead where  id not in :  setOfNewLead]); 
//    Map<Id,String> = new Map<Id,String>();
    System.debug(mapOfOldLead);
     //if(mapOfNewLead.get(a.ParentId).My_name__c == mapOfOldLead.get())
        for(Lead ld : mapOfNewLead.values()){
           //System.debug('myname'+myname);
            System.debug('mapOfNewLead.values()'+mapOfNewLead.get(ld.Id).My_name__c);
            String myname = mapOfNewLead.get(ld.Id).My_name__c;
            setOfMyname.add(myname);
        }
        for(attachment a : trigger.new ){
            System.debug(setOfMyname);
            System.debug('==19'+mapOfNewLead.get(a.ParentId).My_name__c);
                System.debug('===');
                //Id testId = [Select Id from Lead where My_name__c in]
                for(Id getIdMap : mapOfOldLead.keySet()){
                    if(setOfMyname.contains(mapOfOldLead.get(getIdMap).My_name__c)){
                        setOfDeleteLeads.add(a.ParentId);
                        a.ParentId = getIdMap;
                }

            }
        }
        List<Lead> deleteList = [Select id from Lead where Id in : setOfDeleteLeads];
        delete deleteList;
}




try this Code and then let me know how can I improve this code . I have made it quite complex. 
ThanksAnkush 

Cheers
ankushsalesforce@gmail.com


Please give a look at  my awesome friends Blog piyush Blog

























Saturday 4 October 2014

Trigger On Case(EmailTo Case) using Custom Settings in Salesforce!

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.


 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


oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...