function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
o rajuo raju 

How to give save functionality Dynamically comming object through URL?

Hi,
              Here object name through URL will come look like

https://Raju.ap1.visual.force.com/apex/Dynamicsetpage?DFN=ramobj
In My apex class i get this objects as using schema method and i displayed all fields

strtemp=ApexPages.currentPage().getParameters().get('DFN');
  
    this.DFN=strtemp;

     system.debug('*********objectselected//**********'+strtemp);
     String strFieldName = 'raju__'+DFN + '__c'; //Object name
       system.debug('*********objects**********'+strtemp);

My Question:How to give save functonality of this Dyamically getting object?

help me..........
Best Answer chosen by o raju
Vamsi KrishnaVamsi Krishna
Raju,
in your question you mentioned that you are passing the object name in the URL.. but seems you are passing in the field name..
the code will work if you pass in the object name..

if your URL is
https://Raju.ap1.visual.force.com/apex/Dynamicsetpage?DFN=ramobj

and ramobj is your Object Name, then your code should be 
public void doSave() {

String strObjName = ApexPages.currentPage().getParameters().get('DFN');
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(strObjName);
SObject myObj = targetType.newSObject();
insert myObj;
       
}

All Answers

Vamsi KrishnaVamsi Krishna
Raju,

you can try something like this
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
SObject myObj = targetType.newSObject();
insert myObj;

o rajuo raju
Hi Vamsi Krishna,
             Thank u for Reply,
   
                                  I try to above code like this
public void doSave() {
   Schema.SObjectType targetType = Schema.getGlobalDescribe().get(strFieldName);
  System.debug('******targetType*******'+targetType);
 
SObject myObj = targetType.newSObject();
System.debug('*****myObj*******'+myObj);

insert myObj;
       
   }
I got following error :
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!doSave}' in component <apex:commandButton> in page raju:dynamicfieldsetpage

Class.Bisoftsols.DynamicBinding.doSave: line 9, column 1/
 Here:line 9 meansSObject myObj = targetType.newSObject();
            And object name is---> strFieldName(look like string strFieldName = 'Raju__'+strtemp + '__c';)


Help me.............
Vamsi KrishnaVamsi Krishna
Raju,
in your question you mentioned that you are passing the object name in the URL.. but seems you are passing in the field name..
the code will work if you pass in the object name..

if your URL is
https://Raju.ap1.visual.force.com/apex/Dynamicsetpage?DFN=ramobj

and ramobj is your Object Name, then your code should be 
public void doSave() {

String strObjName = ApexPages.currentPage().getParameters().get('DFN');
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(strObjName);
SObject myObj = targetType.newSObject();
insert myObj;
       
}

This was selected as the best answer
o rajuo raju
Hi Vamsi Krishna,

                                It's working fine Thank you  but in an object records inserted but I gave some field values(like amount,name) related to object and click save only record inserted related object but fields values not inserted in database.

How to insert field values also ?
Help me...........
o rajuo raju
Hi VamsiKrishna,
                           Only Records created related object, but i gave some fields those field values not saved in that records.

please help me.......