Thursday, October 24, 2013

What are the defined fields for each salesforce object

In salesforce(www.salesforce.com) there are many objects like accounts, users, leads...Etc. When using REST API or when executing a SOQL we need to give the exact name of the field.
Since salesforce does not support SELECT * from XXXX

This is way I used to extract necessary information from salesforce

1. Login to salesforce
2. Go to Setup->Develop API
3. Generate Partner WSDL (If you working with sandbox environment it might gives some errors but partner WSDL is sufficient)
4. Generate stub jar file Ref :http://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_code_set_up_client.htm
5. Use the following code sample

       ConnectorConfig sfconfig = new ConnectorConfig();
        sfconfig.setUsername("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        sfconfig.setPassword("xxxxxxxxxxxxxxxxxxxxxxQNx26Lb4PTw");        
        PartnerConnection  connection = Connector.newConnection(sfconfig);
         DescribeSObjectResult[] describeSObjectResults =  connection.describeSObjects(
                   new String[] { "account"});
        for (int i=0;i < describeSObjectResults.length; i++)
        {
            DescribeSObjectResult desObj = describeSObjectResults[i];
            String objectName = desObj.getName();
            com.sforce.soap.partner.Field[] fields = desObj.getFields();
            if (desObj.getActivateable()) System.out.println("\tActivateable");
            for(int j=0;j < fields.length; j++)       {                        
                com.sforce.soap.partner.Field field = fields[j];
                System.out.println("\tField: " + field.getName());
                System.out.println("\t\tLabel: " + field.getLabel());
                if (field.isCustom()) 
                    System.out.println("\t\tThis is a custom field.");
                System.out.println("\t\tType: " + field.getType());
                if (field.getLength() > 0)
                    System.out.println("\t\tLength: " + field.getLength());
                if (field.getPrecision() > 0)
                    System.out.println("\t\tPrecision: " + field.getPrecision());
                if (field.getType() == FieldType.picklist)
                {                            
                  
                    PicklistEntry[] picklistValues = field.getPicklistValues();
                    if (picklistValues != null && picklistValues[0] != null)
                    {
                        System.out.println("\t\tPicklist values = ");
                        for (int k = 0; k < picklistValues.length; k++)
                        {
                            System.out.println("\t\t\tItem: " + picklistValues[k].getLabel());
                        }
                    }
                }
                if (field.getType() == FieldType.reference)
                {                            
                   
                    String[] referenceTos = field.getReferenceTo();
                    if (referenceTos != null && referenceTos[0] != null)
                    {
                        System.out.println("\t\tField references the following objects:");
                        for (int k = 0; k < referenceTos.length; k++)
                        {
                            System.out.println("\t\t\t" + referenceTos[k]);
                        }
                    }
                }
            }            
        }

No comments:

Post a Comment