Thursday 19 November 2015

Apex Describe | Dynamic retrieval of object label & field label

     Dynamic retrieval of object label & field label

You can describe sObjects either by using tokens or the describeSObjects Schema method.

Apex Class

public with sharing class DescibeDemoController 
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public List<FieldWrapper> listField{get;set;}

    public DescibeDemoController() 
        listField = new List<FieldWrapper>();

    // find all sObjects available in the organization
    public  List<SelectOption> getListObejectName() 
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
        return objNames;

    // Find the fields for the selected object
    public void showFields() 
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
            schema.describefieldresult dfield = sfield.getDescribe();
            FieldWrapper wObj = new FieldWrapper();
            wObj.fieldName = dfield.getLabel ();
            wObj.fieldAPIName = dfield.getname();

    public class FieldWrapper
        public String fieldName {get; set;}
        public String fieldAPIName {get; set;}

VF Page

<apex:page controller="DescibeDemoController">
    <apex:form id="Describe">
        <apex:pageBlock id="block2" >
            <apex:pageblockbuttons location="top" >
                    <apex:commandButton value="Show Fields" action="{!showFields}" />
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Object Name</apex:outputLabel>
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!ListObejectName}"/>
        <apex:pageBlock id="result" title="Field Detail for {!selectedObject}" rendered="{!if(listField.size > 0 ,true,false)}"   >
            <apex:pageBlockTable value="{!listField}" var="field" rendered="{!if(listField.size > 0 ,true,false)}"> 
                <apex:column value="{!field.fieldName }" headerValue="Name" />
                <apex:column value="{!field.fieldAPIName }"  headerValue="API Name"/>

Related link 

Sunday 8 November 2015

SingleEmailMessage Vs MassEmailMessage | Send Email by Apex

SingleEmailMessage Vs MassEmailMessage

Multiple recipients?YesYes
Personalized body?Yes (single body only)Yes
Special permission needed?NoYes, has to be enabled
Merge fields?YesYes
Personalized merge fields?Yes (only one record at a time)Yes
Template possibilities?Text/HTML/Visualforce/Custom TemplatesText/HTML/Custom Template


Single emails are like regular individual emails that may go to one or more addresses (to/cc/bcc), but each of these emails has the same body

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { '', '' };
message.optOutPolicy = 'FILTER';
message.subject = 'Opt Out Test Message';
message.plainTextBody = 'This is the message body.';
Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

if (results[0].success) 
    System.debug('The email was sent successfully.');
} else 
    System.debug('The email failed to send: ' + results[0].errors[0].message);
Please check below post to see all other method 


Mass emails typically go to a large number of addresses (currently capped to 250 per email), with personalized message bodies.
public void SendEmail()
 List<contact> lstcon=[Select id from contact limit 2];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];
 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setSenderDisplayName('System Admin');
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
 Please check below post to see all other method 

Related link 
Please let us know if this will help you

Amit Chaudhary


Default from address while sending emails | Controlling 'From Address' in salesforce | Salesforce Org Wide Email Address

If you want to control the From Email address in salesforce then you can control this with "Organization-Wide Email Addresses".
An organization-wide email address associates a single email address to a user profile. Each user in the profile can send email using this address. Users will share the same display name and email address

Step 1:-  Setup "Organization-Wide Email Addresses"

1. Navigate Setup -> Email Administration ->  Organization-Wide Email Addresses

2. Click on Add button.

3. Enter email Id and display name of sender.
4. In order to complete this process you need to get verified email id you are putting here

Step 2:- Fetch "Org Wide Email Address" in code like below code.

public class EmailHelper 
 public static void sendEmail() 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
  string body = 'Demo Body ';
  String[] toAddresses = new String[] {''}; 
  mail.setSubject('Test Subject');  
  for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) 
   if(owa.DisplayName.contains('System Admin'))
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Some Use Full Link :-

Please let us know if this will help you

Amit Chaudhary