Play Games

Search This Blog

Thursday, June 29, 2017

Difference between 'Hard delete' and 'Delete' in salesforce

Answer: If you 'Delete' the salesforce records,those records will be present in recycle bin for some time period so that we can restore those records if needed.
If you 'Hard Delete' the salesforce records,those records are permanently deleted from the system and those records won't be available in recycle bin to restore.

Emails are not sent after sandbox is refreshed.

Emails are not sent after sandbox is refreshed.

Solution: Following may be one of the reason.Check it.
Go to Setup --> Email Administration --> Deliverability.
In the access to send Email section,check Access Level.If Access Level is 'NO Access' or 'System Emails Only',all types of emails won't be sent.

Change Access Level to 'All Email' so that all types of emails will be sent.

Friday, June 16, 2017

How to skip piece of code in Apex Class from running while associated test class is running in Salesforce

Solution: Use if(!Test.isRunningTest()) {} to skip piece of code.
Example:
public void validate() {
if(!Test.isRunningTest()) {
Account objAccount  = new Account();
objAccount.name ='Test Account';
insert objAccount;
}
}
Note: The 3 lines written inside of if loop won't be triggered when associated test class is running.

How to send Email through Apex in Salesforce(Batch Apex)

Requirement:Need to send email to all the contacts(get emailid from email field of contact object)
Solution:
global class EmailToContactBatch implements Database.Batchable<sObject> {
    global database.querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([select id,name,Email from Contact where id='00328000015TLBD']);
    }
   
    global void execute(Database.BatchableContext BC, Sobject[] scope) {
        List<Messaging.SingleEmailMessage> lstEmails = new List<Messaging.SingleEmailMessage>();
        for(Contact objContact :(List<contact>) scope) {
            Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
            //Prepare SendToEmail List          
            List<String> lstSendToEmails = new List<String>();
            if(objContact.Email != null) {
                lstSendToEmails.add(objContact.Email);
            }
            objEmail.setToAddresses(lstSendToEmails);
            //Prepare CCEmailList
            List<String> lstCCToEmails = new List<String>();
            if(objContact.Email != null) {
                lstCCToEmails.add(objContact.Email);
            }
            objEmail.setCcAddresses(lstCCToEmails);
            // Set From Email Address
            //objEmail.setOrgWideEmailAddressId('Organisation Wide Email Address Id');
           
            //Set Email Subject
            objEmail.setSubject('Testing Emails');
           
            //Set Email Template
            //objEmail.setTemplateId('Email Template Id');
           
            //Set Email Body
            String body = 'Dear Contact,please ready to contact if you have any issues';
            objEmail.setHtmlBody(body);
            lstEmails.add(objEmail);
        }
        if(!Test.isRunningTest()) {
            Messaging.SendEmailResult[] results = Messaging.sendEmail(lstEmails);
            // To Check whether Email is sent or not,We update processed field of each contact.
            if(results[0].success) {
                List<Contact> lstContactToUpdate = new List<Contact>();
                for(Contact objContact :(List<Contact>)scope) {
                    objContact.Processed__c = true;
                    lstContactToUpdate.add(objContact);
                }
                update lstContactToUpdate ;
                system.debug('lstContactToUpdate:'+lstContactToUpdate );
            }
        }
    }
    global void finish(Database.BatchableContext BC) {
   
    }
}
Note: Change the soql query as per your requirement.
Output:

Thursday, June 15, 2017

How to query records of an object which were created in Last 24 hours in apex salesforce

Problem: I want to fetch/process all the lead records that were created in Last 24 hours
Solution: 
Use SOQL query like this
datetime objDateTime = System.now()-1;
List<Lead> lstLeads = new List<Lead>();
lstLeads = [select id,name from Lead where createddate >=:objDateTime];
System.debug('Leads:'+lstLeads);
Output:

Tuesday, June 13, 2017

How to fetch/query an object records created today in dynamic SOQL salesforce

How to fetch/query an object records created today in dynamic SOQL salesforce

Solution:
string query = 'select id,name from Lead where createddate=TODAY';
List<Lead> lstLead = new List<Lead>();
lstLead = Database.query(query);
system.debug('List of Leads:'+lstLead);
Output:

In Batch Apex:
public string query = 'select id,name from Lead where createddate=TODAY';
global database.querylocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}