Friday, 11 April 2014

Uploading an IMAGE Using VF and Apex Code....

Visual force Code :

<apex:page controller="image">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockSection title="Images" columns="1" >
    <apex:pageBlockSectionItem >
     <apex:outputLabel >Pictures</apex:outputLabel>
      <apex:outputPanel >
        <apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!document.id}"
              height="100" width="100" rendered="{!showimage}"/>
        <apex:inputFile value="{!document.body}" fileName="{!document.name}" ></apex:inputFile>
        <apex:commandButton value="Upload Image" action="{!upload_img}" />  
          <!--reRender="imgref" />-->
        <apex:messages />
      </apex:outputPanel>
    </apex:pageBlockSectionItem>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Apex Programming Code :

public with sharing class image {
    public Document document {
        get {
            if (document == null)
                document = new Document();
                 return document;
        }
        set;        //{ document.type = 'jpg'; }
    }
    public String showimage { get; set; }
    public PageReference upload_img() {
        document.AuthorId = UserInfo.getUserId();
        document.FolderId = UserInfo.getUserId();
        try{
            insert document;          
        }catch(DMLException e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading
              file'));
            return null;
        }
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Image uploaded
                   successfully'));
        return null;
    }
}

Customer Portal using in salesforce


Sales force Customer Portal
Basic steps for enabling customer portal for an app in sales force
1. Create one Contact (Standard Object) record.
2. Create one customer portal.
3. Create one profile.
4. Assign this newly created profile to the new customer portal.
5. Enable login for customer portal in the app/site.
1 .Creation of a new customer portal [Detailed Steps]
1. If you are creating Customer Portal first time in your Account
Go to SetupAppSetUpExpand Customize Expand Customer Portal Click on Settings
Click on Edit Button Enable Customer Portal Check Box and Click on Save Button…
Click on ContinueClick on Cancel.
2. Now Create New Customer Portal
Click On New ButtonProvide mandatory Fields Details as Name, Description.
And Enable Login Enable Check Box Select Administrator Through Lookup as Organization Owner Click On Save Button.
3. Change Portal Fonts and Colors for Newly Created Customer Portal
Click on Change Portal Fonts and Colors Tab Here we can change Color Themes, Tab Styles, PageStyles, SectionStyles, ListStyles.
4. Customize Portal Tabs for newly Created Customer Portal
Click on Customize Portal Tab Buttons select custom Objects from available Tabs and Click on add Button then those selected Objects Moved to Selected Tabs.
Click on Save Button
5. Now we should give the Permission to display Custom Objects in Customer Portal whatever Custom Objects Selected For Customer Portal
Expand AppSetUpExpand CreateClick on Objects.
Click on Edit link of correspondent Custom Object go to Optional Features Check ‘Available for Customer Portal’ and Click on Save Button.
2 .Creation of a new Profile [Detailed Steps]
1. Create one Profile for Customer Portal User
Go to Admin SetupExpand Manage UsersClick on Profiles.
Click on Clone Action for Customer Portal Manager Custom Profile Enter Name For
New Profile And Click On Save Button..
2. In Profile Detail Page we can give the Profile level Privileges for Standard and Custom Objects, Standard and Custom Object Permissions like Read, Write, Edit and Delete.
Click On Edit Button in Profile Detail PageGive the Permissions and Privileges for
Corresponding Custom Objects And Click on Save Button
3 .Assign this newly created profile to the new customer portal [Detailed Steps]
1. Assign Profile to Newly Created Customer Portal
Go to App SetupExpand Customize Expand Customer PortalClick on Settings.
Click on Newly Created Customer PortalGo to AssignProfile Section Click on Edit Profile ButtonSelect /Check Newly Created Profile And Click on Save Buttom.
4 .Enable Login For Customer Portal In The App/Site[Detailed Steps]
1. Enable Login Option In Site . When we Enable This Option For site , Then Customer Portal Features applied to Site like Color Themes .
Go to AppSetUpExpand Develop AreaClick on Sites.
Click on Site Label Click On Login Settings Button In Site Label Detail Page Click On Edit ButtonSelect Dropdown Value as
Newly Created Customer Portal for ‘Enabling Login for’.-Click On Save Button.
5 .Create Contact Record/Customer Portal User For Accessing Customer Portal [Detailed Steps]
1. Create Contact record with user details . This contact record Converted into CustomerPortal User
And SalesForce User.
Go to Standard Contact Object TabClick on New Button-->Enter those Details with Email(mandatory)Click On Save Button.
2. Now we can Enable Customer Portal User for this Contact Record .
In Contact Detailed Page Click On” Work With Portal” Tab Click On Enable Customer Portal.
3. When we click on Enable Customer Portal option It will converted and Opened Corresponding Contact User/customer Portal User Detail page . Now we can Give Role ,User License and Profile.
Click on Edit Button for User Detail pageChane Role Change User License as Customer Portal Manager Custom Change Profile as Newly created profileAnd Click on Save Button.
4. Now we Go to Check Customer Portal Working or not
Go to Contact Record Detail pageExpand Work With Portal tabClick on Login As Portal User.
***************Now Open the Customer Portal for Corresponding User******************

Portal Login with login Page
Customer Portal login has Two ways
1. By using Contact Record of Customer Portal User as I mentioned above Steps
2. Login_portal.jsp login page (url).
Following Steps are using to customer portal login by Login page:
1. Expand Your NameClick on SetUpexpand AppSetupexpand Customer PortalClick on Settings.
2. Click on Newly created Customer PortalGo to Portal Default Settings
Copy Url as Login url And Paste that url in any Browser address bar.
Like “https://na12.salesforce.com/secur/login_portal.jsp?orgId=00DU0000000HIuc&portalId=060U0000000HPdc
3. Now it Opens one Login Page , Enter username and passwordClick on
Login Button.
Example : username : test@test.com
Password: salesforce generate password and send to your mail address

Now Customer Portal Open with Corresponding portal user(like Profile level and Object level permissions) .

Thursday, 3 April 2014

Annotations And Limits



Annotations
An Apex annotation modifies the way a method or class is used, similar to annotations in Java.
Annotations are defined with an initial @ symbol,

Apex annotations:
·          @Deprecated
·         @Future
§  @IsTest
§  @ReadOnly
§  Apex REST annotations:
o    @RestResource(urlMapping='/yourUrl')
o    @HttpDelete
o    @HttpGet
o    @HttpPatch
o    @HttpPost
o    @HttpPut

@depreceated annotation
Use the deprecated annotation to identify methods, classes, exceptions, enums, interfaces, or variables that can no longer be referenced in subsequent releases of the managed package in which they reside.
@deprecated
  // This method is deprecated. Use myOptimizedMethod(String a, String b) instead.
  
global void myMethod(String a) {
  
}

@future annotation

Use the future annotation to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce has available resources.
Methods with the future annotation must be static methods, and can only return a void type.

global class MyFutureClass {
 
  @future 
  static void myMethod(String a, Integer i) {
    System.debug('Method called with: ' + a + ' and ' + i);
    //do callout, other long running code
  }
}

No more than 10 method calls per Apex invocation
§  Salesforce also imposes a limit on the number of future method invocations: 200 method calls per full Salesforce user license or Force.com App Subscription user license, per 24 hours.
§  The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
§  Me thods with the future annotation cannot take sObjects or objects as arguments.
§  Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.

@istest annotation
Use the isTest annotation to define classes or individual methods that only contain code used for testing your application. The isTest annotation is similar to creating methods declared as testMethod.
Classes defined with the isTest annotation don't count against your organization limit of 3 MB for all Apex code.
Classes and methods defined as isTest can be either private or public. Classes defined as isTest must be top-level classes.
@isTest
private class MyTestClass {
 
   // Methods for testing
   @isTest static void test1() {
      // Implement test code
   }
 
   @isTest static void test2() {
      // Implement test code
   }
 
}

IsTest(SeeAllData=true) Annotation
For Apex code saved using Salesforce.com API version 24.0 and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create.
@ReadOnly annotation
The @ReadOnly annotation allows you to perform unrestricted queries against the Force.comdatabase. All other limits still apply. this annotation, while removing the limit of the number of returned rows for a request, blocks you from performing the following operations within the request: DML operations, calls toSystem.schedule, calls to methods annotated with @future, and sending emails.
@Remote action annotation
The RemoteAction annotation provides support for Apex methods used in Visualforce to be called via JavaScript. This process is often referred to as JavaScript remoting. Methods with the RemoteAction annotation must be static and either global or public.
To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
In your controller, your Apex method declaration is preceded with the @RemoteAction annotation like this:
@RemoteAction
global static String getItemId(String objectName) { ... }


Execution Governors and Limits


Because Apex runs in a multitenant environment, the Apex runtime engine strictly enforces a number of limits to ensure that runaway Apex does not monopolize shared resources. These limits, or governors, track and enforce the statistics outlined in the following table. If some Apex code ever exceeds a limit, the associated governor issues a runtime exception that cannot be handled.
Description
Limit
Total number of SOQL queries issued1
100
Total number of SOQL queries issued for Batch Apex and future methods1
200
Total number of records retrieved by SOQL queries
50,000
Total number of records retrieved by Database.getQueryLocator
10,000
Total number of SOSL queries issued
20
Total number of records retrieved by a single SOSL query
200
Total number of DML statements issued2
150
Total number of records processed as a result of DML statementsApproval.processor database.emptyRecycleBin
10,000
Total number of executed code statements
200,000
Total number of executed code statements for Batch Apex and future methods
1,000,000
Total heap size3
6 MB
Total heap size for Batch Apex and future methods
12 MB
Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements4
16
For loop list batch size
200
Total number of callouts (HTTP requests or Web services calls) in a request
10
Maximum timeout for all callouts (HTTP requests or Web services calls) in a request
120 seconds
Default timeout of callouts (HTTP requests or Web services calls) in a request
10 seconds
Total number of methods with the future annotation allowed per Apex invocation5
10
Maximum size of callout request or response (HTTP request or Web services call)6
3 MB
Total number of sendEmail methods allowed
10
Total number of describes allowed7
100
Total number of classes that can be scheduled concurrently
25


Remote Action  Example
<apex:page controller="MyJSRemoting">
<script type="text/javascript">
function getAccountJS() {
    var accountNameJS = document.getElementById('accountname').value;
    vignan.MyJSRemoting.getAccount( accountNameJS, function(result, event)
    {
        if (event.status)
        {
           document.getElementById("{!$Component.theBlock.pbs.pbsi2.accId}")
                                  .innerHTML = result.Id;
           document.getElementById("{!$Component.theBlock.pbs.pbsi1.name}")
                                  .innerHTML = result.Name;
           document.getElementById("{!$Component.theBlock.pbs.pbsi3.phone}")
                                  .innerHTML = result.Phone;
         document.getElementById("{!$Component.theBlock.pbs.pbsi4.type}")
                                  .innerHTML = result.Type;
       document.getElementById("{!$Component.theBlock.pbs.pbsi5.noofemployees}")
                                .innerHTML = result.NumberOfEmployees;
        }
    }, {escape:true});
}
</script>
    <input id="accountname" type="text"/>
    <button onclick="getAccountJS();">Get Account</button>
    <apex:pageblock id="theBlock">
        <apex:pageblocksection columns="3" id="pbs">
            <apex:pageblocksectionitem id="pbsi1">
                 <apex:outputtext id="name" ></apex:outputtext>
            </apex:pageblocksectionitem>
            <apex:pageblocksectionitem id="pbsi2">
                <apex:outputtext id="accId"> </apex:outputtext>
           </apex:pageblocksectionitem>
             <apex:pageblocksectionitem id="pbsi3">
                <apex:outputtext id="phone"> </apex:outputtext>
           </apex:pageblocksectionitem>
            <apex:pageblocksectionitem id="pbsi4">
                <apex:outputtext id="type"> </apex:outputtext>
           </apex:pageblocksectionitem>
           <apex:pageblocksectionitem id="pbsi5">
                <apex:outputtext id="noofemployees"> </apex:outputtext>
           </apex:pageblocksectionitem>
        </apex:pageblocksection>
    </apex:pageblock>
</apex:page>
global with sharing class MyJSRemoting
{
    public static Account account { get; set; }

    @RemoteAction
    global static Account getAccount(String accountName) {
        account = [select id, name, phone, type, NumberOfEmployees from
             Account where name = :accountName limit 1];
        return account;
    }
    }
-------------