Search

Friday, 26 October 2012

Using Ant To Deploy


In addition to the Force.com IDE, you can also use a script to deploy Apex.
Download the Force.com Migration Tool if you want to use a script for deploying Apex from a Developer Edition or sandbox organization to a Database.com production organization using Apache's Ant build tool.
http://www.salesforce.com/us/developer/docs/apexcode/Content/images/help/helpNote_icon.gifThe Force.com Migration Tool is a free resource provided by salesforce.com to support its users and partners but isn't considered part of our services for purposes of the salesforce.com Master Subscription Agreement.
To use the Force.com Migration Tool, do the following:
1.     Visit http://java.sun.com/javase/downloads/index.jsp and install Java JDK, Version 6.1 or greater on the deployment machine.
2.     Visit http://ant.apache.org/ and install Apache Ant, Version 1.6 or greater on the deployment machine. i found it here http://ant.apache.org/bindownload.cgi

3.     Set up the environment variables (such as ANT_HOMEJAVA_HOME, and PATH) as specified in the Ant Installation Guide at http://ant.apache.org/manual/install.html. Dont forget setup the same variables in User and System environment variables. In windows right click on Computer > Properties > Advanced Settings > environment variables

4.     Verify that the JDK and Ant are installed correctly by opening a command prompt, and entering ant –version. Your output should look something like this:
Apache Ant version 1.7.0 compiled on December 13 2006
5.     Log in to Salesforce on your deployment machine. Click Your Name | Setup | Develop | Tools, then click Force.com Migration Tool.
6.     Unzip the downloaded file to the directory of your choice. The Zip file contains the following:
o    Readme.html file that explains how to use the tools
o    A Jar file containing the ant task: ant-salesforce.jar
§  A sample folder containing:
§  codepkg\classes folder that contains SampleDeployClass.cls and SampleFailingTestClass.cls
§  codepkg\triggers folder that contains SampleAccountTrigger.trigger
o    mypkg\objects folder that contains the custom objects used in the examples
o    removecodepkg folder that contains XML files for removing the examples from your organization
o    A sample build.properties file that you must edit, specifying your credentials, in order to run the sample ant tasks in build.xml
o    A sample build.xml file, that exercises the deploy and retrieveAPI calls
7.     Copy the ant-salesforce.jar file from the unzipped file into the ant lib directory. The ant lib directory is located in the root folder of your Ant installation.
8.     Open the sample subdirectory in the unzipped file.
9.     Edit the build.properties file:
§  Enter your Salesforce production organization username and password for the sf.username and sf.password fields, respectively.Note
o    http://www.salesforce.com/us/developer/docs/apexcode/Content/images/help/helpNote_icon.gifThe username you specify should have the authority to edit Apex.
o    If you are using the Force.com Migration Tool from an untrusted network, append a security token to the password. To learn more about security tokens, see “Resetting Your Security Token” in the Salesforce online help.
§  If you are deploying to a sandbox organization, change the sf.serverurl field to https://test.salesforce.com.
10.   Open a command window in the sample directory.
11.   Enter ant deployCode. This runs the deployAPI call, using the sample class and Account trigger provided with the Force.com Migration Tool.
The ant deployCode calls the Ant target named deploy in the build.xml file.
<!-- Shows deploying code & running tests for package 'codepkg' -->
    <target name="deployCode">
      <!-- Upload the contents of the "codepkg" package, running the tests for just 1 class -->
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployroot="codepkg">
        <runTest>SampleDeployClass</runTest>
      </sf:deploy>
    </target>
For more information on deploy, see Understanding deploy.
12.   To remove the test class and trigger added as part of the execution of ant deployCode, enter the following in the command window: ant undeployCode.
ant undeployCode calls the Ant target named undeployCode in the build.xml file.
<target name="undeployCode">
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl=
            "${sf.serverurl}" deployroot="removecodepkg"/>
</target>

Tuesday, 23 October 2012

Really Useful Info On MetaData


Here are all the different meta data types in Salesforce. Why is this useful, because if you are wanting to deploy just a few fields by Eclipse, instead of just selecting the Object in Eclipse if you change the package.xml to below it will just deploy the specific fields you select

     <types>
        <members>X__c.YYY__c</members> //X__c.YYY__c is object.field
        <name>CustomField</name>
    </types>

http://www.salesforce.com/us/developer/docs/api_meta/index.htm

Here is the complete list of types in an Objec, some of which can be deployed using Eclipse, ant or changesets separately from the Object
http://www.salesforce.com/us/developer/docs/api_meta/Content/customobject.htm
http://www.salesforce.com/us/developer/docs/api_meta/Content/meta_workflow.htm
http://www.salesforce.com/us/developer/docs/api_meta/Content/meta_layouts.htm


Unfortunately there are many things that cannot be deployed using Eclipse, ant or changesets which are listed below. Come on Salesforce some of these things are needed.



  • Account Teams
  • Activity Button Overrides
  • Activity Settings
  • Analytic Settings
  • Approval Processes
  • Auto-number on Customizable Standard Fields
  • Business Hours
  • Campaign Influences
  • Case Assignment Rules
  • Case Contact Roles
  • Case Escalation Rules
  • Case Feed Layouts
  • Case Team Roles
  • Chatter Approvals
  • Chatter Settings
  • Console Layouts
  • Contract Line Items
  • Contract Settings
  • Currency Exchange Rates
  • Data Category Visibility Settings
  • Delegated Administration
  • Divisions
  • Email Services
  • Email Settings
  • Email-to-Case
  • Entitlement Processes
  • Entitlement Settings
  • Entitlement Templates
  • Field History Tracking – Currency and Owner Fields
  • Fiscal Year
  • Forecasts
  • Holidays
  • HTML Document and Attachment Settings
  • Ideas Comment Validation Rules
  • Ideas Settings
  • Label Renames
  • Lead Assignment Rules
  • Lead Settings
  • Mail Merge Templates
  • Milestones
  • Mobile Administration
  • Mobile Users and Devices
  • Offline Briefcase Configurations
  • Opportunity Big Deal Alerts
  • Opportunity Competitors
  • Opportunity Sales Processes
  • Opportunity Settings
  • Opportunity Team and Account Team Roles
  • Opportunity Update Reminders
  • Organization Wide Email Addresses
  • Partner Management
  • Predefined Case Teams
  • Product Schedule Setup
  • Product Settings
  • Public and Resource Calendars
  • Quote Templates
  • Salesforce to Salesforce
  • Search Layouts on Standard Objects
  • Search Settings
  • Self-Service Portal Font and Colors
  • Self-Service Portal Settings
  • Self-Service Portal Users
  • Self-Service Public Solutions
  • Self-Service Web-to-Case
  • Sharing Organization Wide Defaults
  • Sites.com
  • Social Account/Contact Settings
  • Solution Categories
  • Solution Settings
  • Support Auto-Response Rules
  • Support Settings
  • Tab renames
  • Tag Settings
  • Territory Assignment Rules
  • User Interface Settings
  • Web Links on Person Account Page Layouts
  • Web-to-Lead
  • Web-to-Lead Auto-Response

    •  
    See http://www.salesforce.com/us/developer/docs/api_meta/Content/meta_unsupported_types.htm for a complete list which may change

    Apex:Variable

    Oh how much I love this Apex:Variable

    you can use it to do so many things, such as in an Apex:Repeat and tracking which element of the Repeat you are in, so if the user selects an item and you need to send this 1 change back to the controller and identify which item it is in the List object in the controller, you can easily do this using Apex:Variable


    <apex:variable value="{!-1}" var="counter">
        <apex:Repeat value="{!zz}" var="x" >
            <apex:variable value="{!counter + 1}" var="counter" />

    <apex:pageBlockSection title="" columns="2" >
    <apex:pageBlockSectionItem >                  
                                   <apex:outputLabel value="{!$ObjectType.XXX__c.fields.xxx.label}" />



    <apex:selectList value="{!x.a__c}" size="1"  >
    <apex:SelectOptions value="{!ss}"></apex:SelectOptions>
    <apex:actionsupport event="onchange" action="{!ff}" rerender="qq" >
                               <apex:param name="g" assignTo="{!h}" value="{!x.tt__c}" />
    <!-- <apex:param name="k" assignTo="{!k}" value="{!x.j__c}" /> -->
    <apex:param name="v" assignTo="{!v}" value="{!counter}" />
    </apex:actionsupport>
                          </apex:selectList>

    </apex:pageBlockSectionItem>
                                                           
                            </apex:pageBlockSection>
                        </apex:outputpanel>
                       </apex:Repeat>






    Or if you use <Apex:Variable value="{!X}" name="val" />

    <Apex:outputText >
        <Apex:Param value="{!X}">
    <Apex:outputText >

    The outputText automatically inherits the param value which is set from the <Apex:Variable>

    Here's a real example: This ensures that the outputText value number is displayed with , separated and to 2 decimal places even though the number could be provided to page in a different format


                       <apex:outputPanel >
                       <apex:variable var="tot" value="{!(l.dd__c * l.uu__c)}" />
                       <apex:outputText value="{0, number, ###,###,###,###.00}">
                        <apex:param value="{!l.ppp__c tot}"/>
    </apex:outputText>
                  </apex:outputPanel>



    Add Values To A MultiPicklist In Apex

    How easy is this

    Simple as this create a String which separates the individual values with a ';' and assign the picklist to this String

    myobj__c.myMultiPicklist__c = 'PicklistA;PicklistB;PicklistC';
    update myobj__c;

    Reflection In Salesforce

    Where and how can we use reflection in Salesforce, well here's a good example

    Say you have 3 classes that map values from 1 object to another.

    Class A matches 3 fields from Object X to ObjectY
    Class B matches 5 different fields from Object X to ObjectY
    Class C matches 12 fields, same fields in Class A and B + 4 other fields from Object X to ObjectY

    Instead of hard coding this mapping in the code provide it as a lookup to a custom object, which will allow you to change any mappings without having to rewrite and deploy new code. Also, will allow the code to be re-used in future projects.

    Custom object Mapping__c
    Name
    FieldMappingGroup__c

    Custom object FieldMappingGroup__c
    Name

    Custom object FieldMappings__c
    Fields
    FieldMappingGroup__c
    Name - not unique
    SourceObject__c
    SourceField__c
    DestinationObject__c
    DestinationField__c


    Mapping__c[] listofFieldToCopy = [Select (Select id From FieldMappingGroup__c ) From  Mapping__c where Name='XXX'];

    set<id> mapIDs = new set<id>();
    for (Mapping__c eachmap: listofFieldToCop){
         mapIDs.add(eachmap.FieldMappingGroup__.id);
    }

    FieldMappings__c[]  fieldMappings = [Select FieldMappingGroup__c , Name , SourceObject__c, SourceField__c, DestinationObject__c, DestinationField__c From FieldMappings__c where Id In: mapIDs];
    For(String fieldName : fieldMappings )
    Object1.put(fieldname, Object2.get(fieldname));


    After writing all of this I've heard that Salesforce maybe introducing Reflection so this may not be necessary arrrgggghhh