Sunday 24 March 2013

Run As Partner Portal User or Partner Community User in Apex Test Class

There are some scenarios where we need to do unit testing as a Partner Portal/Partner Community user. Below code can be used to create partner portal user and run the logic as that user.

Syntax:

@isTest
private class testClass{
    public static testMethod void testFunction(){               
        //Create account
        Account portalAccount1 = new Account(
            Name = 'TestAccount'
        );
        Database.insert(portalAccount1);
     
        //Create contact for the account, which will enable partner portal for account
        Contact contact1 = new Contact(
            FirstName = 'Test',
            Lastname = 'Amit',
            AccountId = portalAccount1.Id,
            Email = 'test@test.com'
        );
        Database.insert(contact1);
       
        //Create user for the contact
        Profile portalProfile = [SELECT Id FROM Profile WHERE Name = 'Gold Partner User' Limit 1];
        User user1 = new User(
            Username = 'test12345test@test.com',
            ContactId = contact1.Id,
            ProfileId = portalProfile.Id,
            Alias = 'test123',
            Email = 'test12345@test.com',
            EmailEncodingKey = 'UTF-8',
            LastName = 'Kumar',
            CommunityNickname = 'test12345',
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            LanguageLocaleKey = 'en_US'
        );
        Database.insert(user1);
       
        system.runAs(user1){
            Account a = new Account(name='test'); 
            insert(a); 
        }                               
    }
}

Note:
  1. Owner of the user should have a role. so make sure your admin user has a role or you can create one admin user in the test class with appropriate role.
  2. Make sure partner portal / partner community is enable for your org and respective profile has the access of it.

Send an email from Apex using Messaging.sendEmail

Apex provides us the oppurtunity to send an email from controller/trigger. There are few situation where you cannot send an email via workflow because of any reason, then you use below code to achieve this.

Apex Code:

  • For sending an email without using email template:
    private void sendMailToUser()
    {
         string messageBody = 'Hi abc,\n\n This is the test email body' +  '\n\n Thanks';
       
         Messaging.SingleEmailMessage objMail = new Messaging.SingleEmailMessage();
         String[] toAddress = new String[]{};
         toAddress.add('test@test.com'); // Adding recipient address in a list
         objMail.setToAddresses(toAddress); // setting to address in the email
         objMail.setReplyTo('donotreply@test.com');
         objMail.setSenderDisplayName('Administrator');
         objMail.setSubject(Test Subject');
         objMail.setPlainTextBody(messageBody); //adding message body
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { objMail }); // sending an email
    }

  • For sending an email using email template to a single user:
     public void SendEmail()
    {
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         mail.setTargetObjectId(con.Id); // id of the contact, lead, or User
         mail.setTemplateId('00***********XU'); // Id of the email template

         mail.setSaveAsActivity(false);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

  • To send mass email to a group of users:
     public void SendEmail()
    {
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         mail.setTargetObjectIds(conList); // List of  ids of the contact, lead, or User
         mail.setTemplateId('00***********XU'); // Id of the email template

         mail.setSaveAsActivity(false);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }



Template Id:
You can use SOQL query to fetch the id of the particular email template.
Syntax:
EmailTemplate e : [select Id,Name,Subject,body from EmailTemplate where name = 'abcd'];

Thursday 21 March 2013

Page Block Component in Visual Force (Salesforce)

Page Block Component in salesforce
This component is used in visualforce to get the same look and feel which salesforce provides in details page of any record.
This component is used as a child component of <apex:page> tag and also has few child tags.
Syntax:
<apex:pageBlock>
                ……….
</apex:pageBlock>

Child component of <apex:pageBlock>:
1.       <apex:pageBlockSection>: This component creates a section in the page block which is exactly similar to the section displayed for standard detail page of any record.

·         There is a limitation in salesforce standard section that the number of columns can not be more than two but with the help of this component  you can define the number of columns in your VF page.
·         Attribute name: columns
·         Each column contains to cells, one for label and one for value.
·         If the number of columns defined for this component is two then it will place the two child component in a single row and if there are more child components then it will add it in next row.
·         If you use <apex:inputField> or <apex:outputField> component as a child component of this tag then it automatically assigns the label and value in the column.

2.       <apex:pageBlockSectionItem>: This component is used to define data in one row.
·         It can support upto two child components.
·         It renders the components dynamically in the row. For example if only one child component is defined then it will span both the column automatically.
·         This component cannot be rerendered.
·         If you will use <apex:inputField> or <apex:outputField> inside this component then its label will not be displayed in the page.

3.       <apex:pageBlockTable>: This component is used to get the list of records present in an object. This will provide you the exact look and feel of standard list view.
·         It displays one record in a row.
·         It contains one or more column component which specify the information to be displayed.
4.       <apex:pageBlockButtons>: This component is used to display the command buttons in a page with the same look and feel of standard button present in detail or edit page.

Example:

<!-- Page: -->
<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>

</apex:page>