Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Inserting sObject Dynamically in Salesforce

Be First!
by March 9, 2012 APEX, Visual Force
How I can insert sObject record dynamically?
Visualforce Page code :

   
       
           
               
           

           
           
               
                   
                   
               

               
                   
                   
               

           

       

   


Apex Code :
public class InsertDynamicSobjectController
{
    public String ObjectName {get; set;}
   
    public String RecordName {get; set;}
   
    public InsertDynamicSobjectController()
    {
        ObjectName = ” ;
        RecordName = ” ;
    }
   
    public PageReference Save()
    {
        //use GlobalDecribe to get a list of all available Objects
        Map gd = Schema.getGlobalDescribe();
        Set objectKeys = gd.keySet();
        if(objectKeys.contains(Objectname.toLowerCase()))
        {
            try
            {
                //Creating a new sObject
                sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
                sObj.put(‘name’ , RecordName) ;
                insert sObj ;
               
                PageReference pg = new PageReference(‘/’+sObj.Id) ;
                return pg ;
            }
            Catch(Exception e)
            {
                ApexPages.addMessages(e) ;
                return null ;
            }
        }
        else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,’Object API name is invalid’)) ;
            return null ;
        }
    }
}

(489)

Previous
Next

Leave a Reply