Saturday, January 5, 2013

Salesforce: Create Custom Fields using Eclipse

Today I had to add different data types fields to my Custom object (Metadata__c). I could have done this by manually creating each field, but the thought of clicking through over different screens didn't take my fancy. So, I thought I’d take the Force.com Metadata API for a spin. I decided to do it via the IDE.

Fortunately, there’s plenty of information available on this topic, the best of which are:

First I added my new Force.com project to IDE and then I simply added Metadata__c metadata to my project and it appeared in my Package Explorer.

Now I can see custom object details there in source tab.

So, It looks like as given below:---


<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <actionOverrides>
        <actionName>Accept</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Clone</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Delete</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Edit</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>List</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>New</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>Tab</actionName>
        <type>Default</type>
    </actionOverrides>
    <actionOverrides>
        <actionName>View</actionName>
        <type>Default</type>
    </actionOverrides>
    <deploymentStatus>Deployed</deploymentStatus>
    <enableActivities>true</enableActivities>
    <enableFeeds>false</enableFeeds>
    <enableHistory>true</enableHistory>
    <enableReports>true</enableReports>
    <label>Metadata</label>
    <listViews>
        <fullName>All</fullName>
        <filterScope>Everything</filterScope>
        <label>All</label>
    </listViews>
    <nameField>
        <displayFormat>META - {00000}</displayFormat>
        <label>Metadata Name</label>
        <trackHistory>false</trackHistory>
        <type>AutoNumber</type>
    </nameField>
    <pluralLabel>Metadata</pluralLabel>
    <searchLayouts/>
    <sharingModel>ReadWrite</sharingModel>
</CustomObject>


Now for adding custom adding custom fields. Please added XML in that source file given below and you can see change for the custom object by saving XML to the project and by refreshing the custom object definition page on browser.

Adding New Custom Fields:--

1. Auto Number

<fields>
        <fullName>Auto_Number__c</fullName>
        <description>Auto Number Creation.</description>
        <displayFormat>AN - {0000}</displayFormat>
        <externalId>false</externalId>
        <inlineHelpText>Auto Number Field</inlineHelpText>
        <label>Auto Number</label>
        <type>AutoNumber</type>
    </fields>

2. Checkbox

<fields>
        <fullName>Checkbox__c</fullName>
        <defaultValue>false</defaultValue>
        <description>This is a checkbox field.</description>
        <externalId>false</externalId>
        <label>Checkbox</label>
        <type>Checkbox</type>
    </fields>

3. Currency (14,2)

<fields>
        <fullName>Currency__c</fullName>
        <defaultValue>2000</defaultValue>
        <description>This is a currency field.</description>
        <externalId>false</externalId>
        <label>Currency</label>
        <precision>16</precision>
        <required>false</required>
        <scale>2</scale>
        <trackHistory>false</trackHistory>
        <type>Currency</type>
    </fields>

4. Date

<fields>
        <fullName>Date__c</fullName>
        <defaultValue>TODAY()</defaultValue>
        <description>This is a date field.</description>
        <externalId>false</externalId>
        <label>Date</label>
        <required>false</required>
        <type>Date</type>
    </fields>

5. Date Time

 <fields>
        <fullName>Date_Time__c</fullName>
        <defaultValue>Now()</defaultValue>
        <description>This is a Date Time Field.</description>
        <externalId>false</externalId>
        <label>Date Time</label>
        <required>false</required>
        <type>DateTime</type>
    </fields>

6. Email

<fields>
        <fullName>Email__c</fullName>
        <description>This is a Email field.</description>
        <externalId>false</externalId>
        <label>Email</label>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <type>Email</type>
        <unique>false</unique>
    </fields>

7. Geolocation

    <fields>
        <fullName>Geolocation__c</fullName>
        <description>This is a Geostation field.</description>
        <displayLocationInDecimal>false</displayLocationInDecimal>
        <externalId>false</externalId>
        <label>Geolocation</label>
        <required>false</required>
        <scale>5</scale>
        <trackHistory>false</trackHistory>
        <type>Location</type>
    </fields>

8. Multi Select Picklist

    <fields>
        <fullName>Multi_Select_Picklist__c</fullName>
        <description>This is multi select picklist field alphabatically shorted and first value as default</description>
        <externalId>false</externalId>
        <label>Multi Select Picklist</label>
        <picklist>
            <picklistValues>
                <fullName>A</fullName>
                <default>true</default>
            </picklistValues>
            <picklistValues>
                <fullName>C</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>D</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>G</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>H</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>V</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Y</fullName>
                <default>false</default>
            </picklistValues>
            <sorted>false</sorted>
        </picklist>
        <trackHistory>false</trackHistory>
        <type>MultiselectPicklist</type>
        <visibleLines>4</visibleLines>
    </fields>

9. Number

    <fields>
        <fullName>Number__c</fullName>
        <defaultValue>52</defaultValue>
        <externalId>false</externalId>
        <label>Number</label>
        <precision>18</precision>
        <required>false</required>
        <scale>2</scale>
        <trackHistory>false</trackHistory>
        <type>Number</type>
        <unique>false</unique>
    </fields>

10. Percent

    <fields>
        <fullName>Percent__c</fullName>
        <defaultValue>0.023</defaultValue>
        <description>This is a Percent Field.</description>
        <externalId>false</externalId>
        <label>Percent</label>
        <precision>18</precision>
        <required>false</required>
        <scale>2</scale>
        <trackHistory>false</trackHistory>
        <type>Percent</type>
    </fields>

11. Phone

    <fields>
        <fullName>Phone__c</fullName>
        <description>This is a phone field.</description>
        <externalId>false</externalId>
        <label>Phone</label>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <type>Phone</type>
    </fields>

12. Picklist

    <fields>
        <fullName>Picklist__c</fullName>
        <description>This is a picklist field.</description>
        <externalId>false</externalId>
        <label>Picklist</label>
        <picklist>
            <picklistValues>
                <fullName>AD</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>FG</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>GG</fullName>
                <default>false</default>
            </picklistValues>
            <sorted>false</sorted>
        </picklist>
        <trackHistory>false</trackHistory>
        <type>Picklist</type>
    </fields>
    <fields>
        <fullName>Text_Area_Long__c</fullName>
        <defaultValue>&quot;Testing...&quot;</defaultValue>
        <description>This is a text area long field.</description>
        <externalId>false</externalId>
        <inlineHelpText>Text area long field help.</inlineHelpText>
        <label>Text Area Long</label>
        <length>32768</length>
        <trackHistory>false</trackHistory>
        <type>LongTextArea</type>
        <visibleLines>3</visibleLines>
    </fields>

13. Text Area Rich

    <fields>
        <fullName>Text_Area_Rich__c</fullName>
        <description>This is a Text Area rich type field.</description>
        <externalId>false</externalId>
        <inlineHelpText>Text Area Rich Help</inlineHelpText>
        <label>Text Area Rich</label>
        <length>32768</length>
        <trackHistory>false</trackHistory>
        <type>Html</type>
        <visibleLines>25</visibleLines>
    </fields>

14. Text Area

    <fields>
        <fullName>Text_Area__c</fullName>
        <defaultValue>&quot;Test Area Test&quot;</defaultValue>
        <description>This is the text area field.</description>
        <externalId>false</externalId>
        <inlineHelpText>Text Area field Help</inlineHelpText>
        <label>Text Area</label>
        <required>true</required>
        <trackHistory>false</trackHistory>
        <type>TextArea</type>
    </fields>

15. Text Encripted

    <fields>
        <fullName>Text_Encripted__c</fullName>
        <description>This is a text encripted type field.</description>
        <externalId>false</externalId>
        <inlineHelpText>Text Encripted field Help</inlineHelpText>
        <label>Text Encripted</label>
        <length>20</length>
        <maskChar>asterisk</maskChar>
        <maskType>all</maskType>
        <required>true</required>
        <trackHistory>false</trackHistory>
        <type>EncryptedText</type>
    </fields>

16. Text

    <fields>
        <fullName>Text__c</fullName>
        <caseSensitive>false</caseSensitive>
        <description>This is a text field.</description>
        <externalId>false</externalId>
        <label>Text</label>
        <length>23</length>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <type>Text</type>
        <unique>true</unique>
    </fields>

17. URL

    <fields>
        <fullName>URL__c</fullName>
        <defaultValue>&quot;https://ssl.salesforce.com/&quot;</defaultValue>
        <description>This is a URL field.</description>
        <externalId>false</externalId>
        <inlineHelpText>URL field Testing...</inlineHelpText>
        <label>URL</label>
        <required>true</required>
        <trackHistory>false</trackHistory>
        <type>Url</type>
    </fields>

18. Master Detail (Master - Account)

<fields>
        <fullName>Account__c</fullName>
        <externalId>false</externalId>
        <label>Account</label>
        <referenceTo>Account</referenceTo>
        <relationshipLabel>Metadata</relationshipLabel>
        <relationshipName>Metadata</relationshipName>
        <relationshipOrder>0</relationshipOrder>
        <reparentableMasterDetail>false</reparentableMasterDetail>
        <trackHistory>false</trackHistory>
        <type>MasterDetail</type>
        <writeRequiresMasterRead>false</writeRequiresMasterRead>
    </fields>

19. Lookup Relationship (Parent -- Contact)

<fields>
        <fullName>Contact__c</fullName>
        <deleteConstraint>SetNull</deleteConstraint>
        <externalId>false</externalId>
        <label>Contact</label>
        <referenceTo>Contact</referenceTo>
        <relationshipLabel>Metadata</relationshipLabel>
        <relationshipName>Metadata</relationshipName>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <type>Lookup</type>
    </fields>

20. Formula Field

<fields>
        <fullName>Formula_Text__c</fullName>
        <externalId>false</externalId>
        <formula>Text__c</formula>
        <label>Formula Text</label>
        <required>false</required>
        <trackHistory>false</trackHistory>
        <type>Text</type>
        <unique>false</unique>
    </fields>

21.  List View

<listViews>
        <fullName>My_List_View</fullName>
        <columns>NAME</columns>
        <columns>Account__c</columns>
        <columns>Auto_Number__c</columns>
        <columns>Checkbox__c</columns>
        <columns>Contact__c</columns>
        <columns>Currency__c</columns>
        <filterScope>Everything</filterScope>
        <label>My List View</label>
    </listViews>

I think this will be useful.. Enjoy Coding,


Salesforce: Understanding Eclipse IDE and Metadata API


Understanding Eclipse IDE and Metadata API for Custom Object

Salesforce Deployment Tool Eclipse IDE (Advantages and Disadvantages)

Force.com IDE/ Eclipse IDE
The Force.com IDE is a powerful client application for creating, modifying and deploying Force.com applications into production environment. Based on the Eclipse platform, it provides a comfortable environment for programmer’s familiar with integrated development environments, allowing you to code, compile, test, and package and deploy all from within the IDE itself.

The IDE also features an embedded schema explorer same as Sales force Explorer for a live view of your database and metadata components. Using the standard synchronization features of the IDE, you can create a project that enables multiple developers to develop against a shared source code repository.
Following are the advantages and disadvantage of tool.

Advantage:

1)It provides a comfortable environment for programmers familiar with integrated development environments, allowing you to the code, compile, test, and package and deploy all from within the IDE itself.

2)It provide the code editor following are features:
3)Syntax highlighting for the Apex key words.
4)Code assist for Apex system classes, user-defined classes, and schema objects.
5)Click-to-line integration with errors and testing views.
6)Unlimited undo and redo.
7)Tabbed views of source code and XML metadata definition on app.
8)We can validate the selected classes/trigger etc before deployment.
9)Execute Anonymous view, which allows you to run the anonymous block of Apex on the server. Anonymous blocks help you to the quickly evaluate Apex on the fly, or to write scripts that change dynamically at runtime.
10)Force.com IDE provides an Apex Test Runner view where you can test your methods to see which the tests are are passing or failing helpful in Test class.
11)Metadata files are the text-based, they also can be compared using text diff tools, managed in a version control system such as CVS or Subversion, and even deployed from one Salesforce organization to another
The Schema Explorer is the tool for browsing the objects and fields in a Force.com organization. The browser presents the logged-in user’s view of the Force.com database model, including object visibility, permissions, data types, lookup values and other information that is useful in developing applications on the Force.com platform.
12)Project Synchronization working Online or Offline project.
13)If deployment fail it will shows the errors with line number.
14)A version control system allows you to track changes to a set of files over the time.

Disadvantage:

1)It takes long time for loading and deployments which is initialization step of our project.
2)We can’t run our visual force page GUI on browser.
3)Detailed logs are not generated in Integrated Development Environment. For example system log file.
4)We are not able to update user profiles using eclipse IDE. Like configuration changes not done in this tool.

=======================================
Understanding Metadata related to Custom Object
=======================================

For increasing understanding of your's follow the steps given below:-

1. Make a new force.com project in Eclipse IDE with your developer org credentials.
2. Include Metadata for Custom Object
3. Add a new Custom Object in your org (Let suppose I have created a Custom Object with label Metadata  in my org)
4. Go to Eclipse and refresh the project from server
5. Go to Custom Object
6. Now Go to Particular object (In my case I go to Metadata (Custom Object))
7. Go to Source tab and now you can see the XML Metadata details for this object
8. Now read out details description given below about source code.

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
 
1. [Standard Buttons and Links ---> Accept]

<actionOverrides>
        <actionName>Accept</actionName>
        <type>Default</type>
</actionOverrides>

2. [Standard Buttons and Links ---> Clone]

    <actionOverrides>
        <actionName>Clone</actionName>
        <type>Default</type>
    </actionOverrides>

3. [Standard Buttons and Links ---> Delete]

    <actionOverrides>
        <actionName>Delete</actionName>
        <type>Default</type>
    </actionOverrides>

4. [Standard Buttons and Links ---> Edit]

    <actionOverrides>
        <actionName>Edit</actionName>
        <type>Default</type>
    </actionOverrides>

5. [Standard Buttons and Links ---> List]

    <actionOverrides>
        <actionName>List</actionName>
        <type>Default</type>
    </actionOverrides>

    6. [Standard Buttons and Links ---> New]

<actionOverrides>
        <actionName>New</actionName>
        <type>Default</type>
    </actionOverrides>

7. [Standard Buttons and Links ---> Tab]

    <actionOverrides>
        <actionName>Tab</actionName>
        <type>Default</type>
    </actionOverrides>

    8. [Standard Buttons and Links ---> View]

<actionOverrides>
        <actionName>View</actionName>
        <type>Default</type>
    </actionOverrides>

    9. [Deployment Status ---> Deployed/In Development]

<deploymentStatus>Deployed</deploymentStatus>
 
10. [Track Activities ---> True/False]

<enableActivities>true</enableActivities>

11. [Track Field History or Not / Enable Feeds or Not]
 
<enableFeeds>false</enableFeeds>
<enableHistory>true</enableHistory>
 
10. [Enable Reports ---> True/False]

<enableReports>true</enableReports>
 
11. [Label for the custom object]

<label>Metadata</label>
 
12. [List Views Details for the custom object]

<listViews>
        <fullName>All</fullName>
        <filterScope>Everything</filterScope>
        <label>All</label>
    </listViews>

    13. [Name field detail for Custom Object]

<nameField>
        <displayFormat>META - {00000}</displayFormat>
        <label>Metadata Name</label>
        <trackHistory>false</trackHistory>
        <type>AutoNumber</type>
    </nameField>

    11. [Plural Label for the custom object]

<pluralLabel>Metadata</pluralLabel>

    11. [Search layout detail]
<searchLayouts/>
 
11. [Sharing Access]
<sharingModel>ReadWrite</sharingModel>

</CustomObject>

Operations that are available via Force.com and Metadata API
  • Add new fields to an object
  • Make minor changes such as label, field lengths, etc.
  • Change the ReferenceTo (eg from Account to Contact) for a field
  • Changed a field type (eg from currency to number). Logically, some conversions might not be possible?
Operations that are not available via Force.com and Metadata AP
  • Cannot change the API name of an existing field. It will simply create the new field.
  • Cannot delete existing picklist values
  • Cannot delete fields
  • Cannot change some field type (eg tried to change afield from autonumber to text but it didn’t make the change).
That's All. Thanks...