Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Last active April 9, 2017 01:00
Show Gist options
  • Save msrivastav13/ea1e0cf2cc9a31a01d4b697a584dff0d to your computer and use it in GitHub Desktop.
Save msrivastav13/ea1e0cf2cc9a31a01d4b697a584dff0d to your computer and use it in GitHub Desktop.
Metadata Wrapper over apex metadata
public with sharing class ApexMetadataUtility {
/**
* Call apex controller method fetch metadata
* @param
* @return void
*/
@AuraEnabled
public static list<SobjectMetadataWrapper> getSobjectMetadata(String[] sobjectarray){
list<SobjectMetadataWrapper> lstsobjectMetadata = new list<SobjectMetadataWrapper>();
// Make the describe call
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(sobjectarray);
//System.debug('Got describe information for ' + results.size() + ' sObjects.');
// For each returned result, get some info
for(Schema.DescribeSobjectResult res : results) {
//System.debug('sObject Label: ' + res.getLabel());
//System.debug('Number of fields: ' + res.fields.getMap().size());
//System.debug(res.isCustom() ? 'This is a custom object.' : 'This is a standard object.');
SobjectMetadataWrapper Sobjectmetdata = new SobjectMetadataWrapper();
Sobjectmetdata.objectlabel = res.getLabel();
Sobjectmetdata.objectName = res.getName();
Sobjectmetdata.objectprefix = res.getKeyPrefix();
Sobjectmetdata.isCustomObject = res.isCustom();
list<SobjectMetadataWrapper.FieldMetadataWrapper> lstfieldmetadata = new list<SobjectMetadataWrapper.FieldMetadataWrapper>();
list<SobjectMetadataWrapper.ChildRelationshipMetadataWrapper> lstchildRelationships = new list<SobjectMetadataWrapper.ChildRelationshipMetadataWrapper>();
for(String sobjectfield : res.fields.getMap().keyset()){
SobjectMetadataWrapper.FieldMetadataWrapper fieldmetadata = new SobjectMetadataWrapper.FieldMetadataWrapper();
Schema.DescribeFieldResult dfr = res.fields.getMap().get(sobjectfield).getDescribe();
fieldmetadata = mapFieldMetadata(dfr);
lstfieldmetadata.add(fieldmetadata);
}
Sobjectmetdata.fieldmetadata = lstfieldmetadata;
// Get child relationships
Schema.ChildRelationship[] rels = res.getChildRelationships();
if (rels.size() > 0) {
//System.debug(res.getName() + ' has ' + rels.size() + ' child relationships.');
for(Schema.ChildRelationship childrelationship :rels){
SobjectMetadataWrapper.ChildRelationshipMetadataWrapper childRels = new SobjectMetadataWrapper.ChildRelationshipMetadataWrapper();
childRels = mapChildRelationships(childrelationship);
lstchildRelationships.add(childRels);
}
}
Sobjectmetdata.childrelationship = lstchildRelationships;
lstsobjectMetadata.add(Sobjectmetdata);
}
return lstsobjectMetadata;
}
/**
* This method maps Special data type Schema.DescribeFieldResult to Primitive
* @param
* @return SobjectMetadataWrapper.FieldMetadataWrapper
*/
public static SobjectMetadataWrapper.FieldMetadataWrapper mapFieldMetadata(Schema.DescribeFieldResult dfr){
SobjectMetadataWrapper.FieldMetadataWrapper fieldmetadata = new SobjectMetadataWrapper.FieldMetadataWrapper();
fieldmetadata.byteLength = dfr.getByteLength();
fieldmetadata.fieldname = dfr.getName();
fieldmetadata.label = dfr.getLabel();
fieldmetadata.relationshipOrder = dfr.getRelationshipOrder();
fieldmetadata.relationshipName = dfr.getRelationshipName();
fieldmetadata.type = dfr.getType().name();
return fieldmetadata;
}
/**
* This method maps Special data type Schema.ChildRelationship to Primitive
* @param
* @return SobjectMetadataWrapper.FieldMetadataWrapper
*/
public static SobjectMetadataWrapper.ChildRelationshipMetadataWrapper mapChildRelationships(Schema.ChildRelationship rels){
SobjectMetadataWrapper.ChildRelationshipMetadataWrapper childRels = new SobjectMetadataWrapper.ChildRelationshipMetadataWrapper();
childRels.isCascadeDelete = rels.isCascadeDelete();
childRels.isRestrictDelete = rels.isRestrictedDelete();
childRels.relationshipName = rels.getRelationshipName();
childRels.fieldToken = mapFieldMetadata(rels.getField().getDescribe());
//list<string> childObjectname = new list<string>();
//childObjectname.add(rels.getChildSObject().getDescribe().getName());
childRels.childobjectName = rels.getChildSObject().getDescribe().getName();
return childRels;
}
}
public with sharing class SobjectMetadataWrapper {
@AuraEnabled
public String objectlabel;
@AuraEnabled
public String objectprefix ;
@AuraEnabled
public String objectName ;
@AuraEnabled
public list<FieldMetadataWrapper> fieldmetadata;
@AuraEnabled
public list<ChildRelationshipMetadataWrapper> childrelationship ;
@AuraEnabled
public Boolean isCustomObject ;
public with sharing class FieldMetadataWrapper{
@AuraEnabled
public Integer byteLength;
@AuraEnabled
public String label;
@AuraEnabled
public String fieldname;
@AuraEnabled
public String relationshipName;
@AuraEnabled
public Integer relationshipOrder;
@AuraEnabled
public String type;
}
public with sharing class ChildRelationshipMetadataWrapper{
@AuraEnabled
public String relationshipName ;
@AuraEnabled
public Boolean isCascadeDelete ;
@AuraEnabled
public Boolean isRestrictDelete;
@AuraEnabled
public FieldMetadataWrapper fieldToken ;
@AuraEnabled
public String childobjectName ;
//public list<SobjectMetadataWrapper> childSobject ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment