Wednesday, May 3, 2017

Get a Map of Populated SObject Fields in Salesforce

With Summer 16 Release a new function in Apex Sobject Class which enables us to get the list of fields which are populated in memory.

The returned map contains only the fields that have been populated in memory for the SObject instance, which makes it easy to iterate over those fields. A field is populated in memory in the following cases.
1) The field has been queried by a SOQL statement.
2) The field has been explicitly set before the call to the getPopulatedFieldsAsMap() method.

Let’s take example for both the cases,

Case 1:  Fields are queried in the SOQL Statement.

Account a = new Account();
a.name = 'TestMapAccount1';
insert a;
a = [select Id,Name from Account where id=:a.Id];
Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();

for (String fieldName : fieldsToValue.keySet()){

    System.debug('field name is ' + fieldName + ', value is ' + 
        fieldsToValue.get(fieldName));

}

Debug for this case: 

DEBUG|field name is Id, value is 001R0000003EPPkIAO

DEBUG|field name is Name, value is TestMapAccount1

Case 2: Fields are explicitly set before calling the getPopulatedFieldsAsMap() method.

Account a = new Account();
a.name = 'TestMapAccount2';
a.phone = '123-4567';
insert a;
Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();

for (String fieldName : fieldsToValue.keySet()) {
    System.debug('field name is ' + fieldName + ', value is ' + 
        fieldsToValue.get(fieldName));
}

Debug for this case: 

DEBUG|field name is Name, value is TestMapAccount2
DEBUG|field name is Phone, value is 123-4567

DEBUG|field name is Id, value is 001R0000003EPPpIAO


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.

Monday, January 30, 2017

Email-to-Case Setup in Salesforce

Salesforce can automatically create a case when an email is sent to one of your company's email addresses, such as support@company.com. This Email-to-Case functionality auto-populates case fields from the content of each email. For example, an email subject heading becomes a case subject. Your organization can set up Email-to-Case or On-Demand Email-to-Case to efficiently resolve and correspond with customer inquiries via email. Salesforce allows you to choose one of two Email-to-Case options:

Email-to-Case
Email-to-Case requires downloading the Email-to-Case agent from wiki.developerforce.com/index.php/Email_To_Case and installing the agent behind your network's firewall.
Use Email-to-Case if you have a requirement to keep all email traffic within your firewall, and you want to accept email attachments larger than 25 MB from customers.

On-Demand Email-to-Case
On-Demand Email-to-Case uses Apex email services to convert email to cases, without you having to download and install an agent behind your network's firewall.
Use On-Demand Email-to-Case if you are not concerned about keeping email traffic within your firewall and you do not need to accept attachments larger than 25 MB from customers.


Setting Up On-Demand Email-to-Case
1. Click Setup ➤ Customize ➤ Cases ➤Email-to-Case ➤ Edit ➤ Check Enable Email-to-Case ➤ Check Enable On-Demand Service ➤ Click Save.


2. From the Routing Addresses related list, click the ‘New’ button to create a new Routing Address. 

Enter Routing Name (some name),Email Address (Customer complaints email address),If you want to create a Task When case comes then Select Create Task from Email,Task Status.Select Case Priority and Case Origin,Click  Save.

   

 3. Open Your Email Inbox in my case it is (Support@company.com) and Click on verification link to verify the email.

4. After Verification it will take you to salesforce Home page,Open Email-to-Case from Setup,You will see Some Information in Routing Addresses related list. 

5. Open Your Email ( In my Case it is GMAIL),Click on Settings,Click on Forwarding and POP/IMAP Tab.


6.Click on Add a forwarding address Copy and paste the Email Service Address from Routing Addresses related list.

Thats all.

Now send an email to Email Address ( In my case it is Support@company.com ).

See that ,The case is automatically created in salesforce.





Wednesday, January 25, 2017

                                                                          Field Analyzer

Enhancements done by Me.


1.No Need To add Remotesite Settings Manually.


2.Added a Link in the Page If we click on it it will Redirect to the Details page of the Analysis.




3.Added User Input Validation .(While adding new Org If user try to Save Org details with Empty Fields Validation will thrown).




4.Added Org Details Validation .(While adding New Org If User Enter Wrong Information and try to save Validation will thrown).




Tuesday, January 24, 2017

RelationShip Between Opportunity ,OpportunityLineItem ,Product, PriceBook And PriceBookEntry.




OpportunityLineItem is the Junction Object Between Opportunity and PriceBookEntry.

PriceBookEntry is the Junction Object Between Pricebook and Product.