Wednesday 29 August 2012

Related List on Custom VFP

Hi Friends ,

In this Post you will found how we can show related list on  Custom Visual force Page.

Consider : I have a account record. This account record have many child record on Contact object.
Contact Object have look up of Account.

There is apex code :-

public class CusttomAccount
{
    public Account acc {set;get;}
    public CusttomAccount()
    {
        acc = new Account();
        acc = [select id ,name from Account limit 1];
    }  

}

There is Visual force Page code :- 


<apex:page controller="CusttomAccount" showChat="false" sidebar="false">  
    <apex:pageblock   >
        <apex:pageBlockSection title="Record Detail">
            <apex:pageBlockSectionItem>
             <apex:outputLabel value="Name" />
                <apex:outputLabel value="{!acc.name}" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageblock>
    <apex:relatedList subject="{!acc.id}"  list="Contacts" />
    <!-- in list attribute we fill the relationship name and subject we pass the account id whose related list will show -->
    <!--  if your want you show enhancelist please uncomment this code  -->
    <!-- <apex:enhancedList  type="contact" height="300" customizable="false" rowsPerPage="10"  /> -->
</apex:page>



Tuesday 7 August 2012

Operations That Are Available/NotAvailable via Force.com and Metadata API

Hello EveryOne,

In this Post I am telling you about Operations that are available/notAvailable via Force.com and Metadata API .

In my project I want to change api name of all custom fields in my all custom object, Actually I want add a prefix in api name of custom fields. So I took my project into eclipse and change the api name of the field in xml file of every Object.(I thought it would be take less time ) and save it. Then I notice some thing which i am sharing here:

Operations that are available via Force.com and Metadata API
(we can do that things by modifying the xml of object )

   1. Add new fields to an object
    2.Make minor changes such as label, field lengths, etc.
    3.Change the ReferenceTo (eg from Account to Contact) for a field
    4.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 API(we can't do that things by modifying the xml of object )

    1.Cannot change the API name of an existing field. It will simply create the new field.
    2.Cannot delete existing picklist values
    3.Cannot delete fields
    4.Cannot change some field type (eg tried to change afield from autonumber to text but it didn’t      make the change).

Let me know if it is useful for you.
   
Thanks

Ashlekh Gera.



Sunday 5 August 2012

Salesforce find object from record id and update the record


Hello Friends,

Today i am publish a new post that tell you how to find the object from a record id .
And after find the object we can update the record.
Before update any record we can check, that field exist or not in a object whose value user want to change.
In my example i am changing  the owner id of a record.

idOfRec:   Id of the record whose owner field will be changed.
OwnerId:  Id of the new owner


    //This fucntion is Genric and change the owner of the record
    public static void changeOwner(String idOfRec ,String OwnerId)
    {
        //Hold object name
        String objName ='' ;
     
        //this var indicate that selected object has ownerid field or not
        Boolean isOwner= false;    

        //Hold record of object
        SObject sObjInstance;
     
        //Hold the query string
        String q ='';            
     
        //Hold all objects in schema
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
     
        //Hold the object api name
        Set<String> keyPrefixSet = gd.keySet();
     
        //Finding the Object by keyPrefix
        for(String sObj : keyPrefixSet)
        {
            //hold particualr description of a object
            Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
         
            //Hold the keyPrifix of present Objet. This is three digit key
            String tempPrefix = r.getKeyPrefix();
         
            //Compare the keyPrefix of present obj and
            // the record's object which is in argument if match then store
            if(idOfRec.subString(0,3) == tempPrefix)
            {
                Map<String, Schema.SObjectField> FsMap = r.fields.getMap();
            //verifying that object contain the ownerid field or
            //not because child object don't contain this field
                isOwner=FsMap.containsKey('ownerid');
                objName = r.getName();
                break;
             
            }
        }
     
        try
        {
         //if owner id fields exist in the object than update the ownerid and update the record
            if(isOwner)
            {
                //Make a query to get a owner of that record    
                q = 'select id, ownerId from '+ObjName+' where id = \''+idOfRec+'\'';
            //making the instance of that record by fetching data by dynamic query
                sObjInstance= Database.query(q);
             
                //Now put the new user id and update that record
                sObjInstance.put('ownerId',OwnerId);
                update sObjInstance;
            }
            else{
                    ApexPages.addMessage(new ApexPages.message
                                                                          (ApexPages.severity.INFO,'Sorry Owner cann;t be change'));
            }
       }catch(Exception e){}  
    }

Thanks