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



No comments:

Post a Comment