Thursday, March 23, 2017

Apex Trigger On AttachmentObject to Check Has_Attachment(Checkbox Field) in CaseObject when Attachment is Attached to CaseObject and Un Check when attachments are deleted.

 Trigger on Attachment to Check Case have Attachment or Not,If attachment is there then checking a checkbox field in Case,If no attachments are there then making that checkbox false.

Trigger:

trigger TriggerOnAttachement on Attachment (after insert,after delete){
    if(trigger.isinsert){
        Cls_Update_has_attachement_case.UpdatefieldinCasetocheck(trigger.New);
    }
    if(trigger.isdelete){
        Cls_Update_has_attachement_case.UpdatefieldinCasetoUncheck(trigger.old);
    }
}


Class:

public class Cls_Update_has_attachement_case{
    Public static void UpdatefieldinCasetocheck(List<Attachment> atts){
        List<Id> ParentId = new List<Id>();
        List<Case> listCase = new List<Case>();
        for (Attachment attachmentObject: atts)
        {
            ParentId.add(attachmentObject.ParentId);
        }
        List<Case> Caselist = [Select Id,Has_Attachment__c from Case where Id IN:ParentId ];
        if(Caselist!=null && Caselist.size()>0){
            for(Case cs : Caselist){
                cs.Has_Attachment__c = true;
                listCase.add(cs);
            }
        }
        if(listCase.size() > 0)
            update listCase;  
    }
 
    Public static void UpdatefieldinCasetoUncheck(List<Attachment> atts){
        List<Id> ParentId = new List<Id>();
        List<Case> listCase = new List<Case>();
        for (Attachment attachmentObject: atts)
        {
            ParentId.add(attachmentObject.ParentId);
        }
        List<Case> csToUpdate=new List<Case>();
        for(case cs:[Select Id,Has_Attachment__c,(SELECT Id FROM ContentDocumentLinks),(SELECT Id FROM attachments) FROM Case where ID In:ParentId]){
            system.debug('size of atts===>'+cs.ContentDocumentLinks.Size());
            if(cs.ContentDocumentLinks.Size()==0 && cs.Has_Attachment__c==True && cs.attachments.Size()==0 ) {
                cs.Has_Attachment__c=false;
                csToUpdate.add(cs);
            }
        }
        system.debug('csToUpdate===>'+csToUpdate);
        if(!csToUpdate.isEmpty())
            Database.update(csToUpdate, false);
    }
}

Tuesday, March 21, 2017

Dynamic SOQL Query.

 public list<sObject> reclist = new list<sObject>();
 public string selectedObject = 'Account';
 public string SearchKey = 'test';
 string query =  'select id,Name from ' +selectedObject +' where Name LIKE \''+SearchKey+'%\' ORDER BY Name';
 reclist = database.query(Query);
system.debug('reclist =='+reclist );

Above Query will return the accounts whose name start with test.

 string query =  'select id,Name from ' +selectedObject +' where Name LIKE \'%'+SearchKey+'%\' ORDER BY Name';
 reclist = database.query(Query);

Above Query will return the accounts whose name Contains test.

string query =  'select id,Name from ' +selectedObject +' where Name=:SearchKey limit 1'; system.debug('query ==='+query );
sobject sobj = database.query(Query);

Above Query will return the account whose name exactly Equuleus to test(SearchKey).


string conName = 'test';
string conNameLike = '%' + String.escapeSingleQuotes(conName.trim()) + '%';
String query = 'SELECT Id, FirstName, LastName From Contact Where FirstName LIKE :conNameLike';
List<Contact> conList = Database.query(query);
System.debug('conList-' + conList);

Above Query will return the contacts whose FirstName contains test.

How to see Debug Logs for site page.

Create Debug logs for User who login in the site.
Some times User may be Default Site Guest User or Normal salesforce user.
Open the site and Click Inspect element (Click F12 as a shortcut),
Click Console tab,Paste below code and click enter.
document.cookie="debug_logs=.force.com;domain=.force.com"
Now you are able to see the debug logs.