Skip to content

Instantly share code, notes, and snippets.

@mannharleen
Last active November 9, 2016 01:32
Show Gist options
  • Save mannharleen/57f32fd4d9a8b6a87dab813ac5602e63 to your computer and use it in GitHub Desktop.
Save mannharleen/57f32fd4d9a8b6a87dab813ac5602e63 to your computer and use it in GitHub Desktop.
Note: Sorry about the naming that has gone wrong:)
What this does: Displays a list of all accessable/creatable/updatable objects in a picklist. User selects one and the sceen displays
the inputfields. User fills in and can save the record.
public class createAnyObjectRecord {
public Map<String, Schema.SObjectType> gd_map_full;
//public List<Schema.SObjecttype> gd_lst_full; **delete
public Map<String, Schema.SObjectType> gd;
/*public List<Schema.SObjectField> field_names_lst{get;set;}
Schema.sObjectField field_name;
public schema.SObjectType object_name;*/
public sobject object_1{get;set;} //sobject
//public List<String> parent_field{get;set;} // **delete
public List<String> parent_field_iterator{get;set;} // parent fields to display
public String parent_sl{get;set;} //selectlist
public List<selectOption> parent_so{get;set;} //selectoption
public createAnyObjectRecord () {
//display sucess message if it is a redirect
system.debug('ApexPages.currentPage().getParameters().get ='+ApexPages.currentPage().getParameters().get('message'));
if(ApexPages.currentPage().getParameters().get('message') != null) {
ApexPAges.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,ApexPages.currentPage().getParameters().get('message')));
}
gd_map_full = Schema.getGlobalDescribe(); //get all object tokens
gd = new Map<String, Schema.SObjectType>();
for( String gd_string : gd_map_full.keyset()) { //add to gd only those objects that are creatable by user
if(gd_map_full.get(gd_string).getDescribe().isCreateable()
&& gd_map_full.get(gd_string).getDescribe().isAccessible()
&& gd_map_full.get(gd_string).getDescribe().isQueryable()
) {
gd.put(gd_string, gd_map_full.get(gd_string));
}
}
populateSelectOption();
}
public void populateSelectOption() {
system.debug('entering populateSelectOption');
parent_so = new List<selectOption>(); //populate with object names
parent_so.add(new SelectOption('None','None'));
List<String> gd_key_lst = new List<String>(gd.keySet());
gd_key_lst.sort();
for (String s1:gd_key_lst) {
parent_so.add(new SelectOption(s1,s1));
}
}
public void renderParent() {
system.debug('parent_sl='+parent_sl);
String s2 = parent_sl;
object_1 = gd.get(s2).newSobject(); //create sobject for the selected object
//get all fields for that object
system.debug(gd.get(s2).getDescribe().fields.getMap().keyset());
parent_field_iterator = new List<String>();
for (String s3 :gd.get(s2).getDescribe().fields.getMap().keyset()) {
if(gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isAccessible()
&& gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isUpdateable()
&& !gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isCalculated()
&& gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isCreateable()
) {
system.debug('field = '+s3+' '+gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isAccessible()+
' '+gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isUpdateable()+
' '+gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isCalculated()+
' '+gd.get(s2).getDescribe().fields.getMap().get(s3).getDescribe().isCreateable()
);
parent_field_iterator.add(s3);
}
}
}
public pageReference reset() {
pageReference pr = ApexPages.currentPage();
system.debug('pr= '+pr);
pr.setRedirect(true);
return pr;
//populateSelectOption();
//return null;
}
public pagereference save() {
for (String iterator: parent_field_iterator) {
object_1.put(iterator, object_1.get(iterator));
}
try {
Database.upsert(object_1);
} catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
pageReference pr = ApexPages.currentPage();
pr.getParameters().put('message','Records successfully created');
system.debug('pr= '+pr);
pr.setRedirect(true);
return pr;
}
}
<apex:page controller="createAnyObjectRecord" showHeader="false" title="Parent and Child creator" >
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="Parent and child object creator" >
<!--apex:pageBlockSection title="pbsection"-->
<apex:outputPanel id="id_parent_field">
<apex:actionRegion >
<apex:pageBlockSection columns="1">
<apex:selectList value="{!parent_sl}" size="1" label="Parent object" title="Parent object." id="id_parent_sl">
<apex:selectOptions value="{!parent_so}"/>
<apex:actionSupport event="onchange" action="{!renderParent}" reRender="id_parent_field" />
</apex:selectList>
</apex:pageBlockSection>
<apex:commandButton value="Reset" action="{!reset}" immediate="true" />
</apex:actionRegion>
<apex:actionRegion >
<apex:pageBlockSection columns="2">
<apex:repeat value="{!parent_field_iterator}" var="iterator">
<apex:pageBlockSection columns="2">
<apex:inputfield value="{!object_1[iterator]}"></apex:inputfield>
</apex:pageBlockSection>
</apex:repeat>
</apex:pageBlockSection>
<apex:commandButton value="Save" action="{!save}"/>
</apex:actionRegion>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment