Retrieve all fields for particular sObject it is working fine but then i insall app which is part of manage package also retrieving fields of manage package. how to prevent to retrieve.

objects/fpst__formrec.object: cannot create a new component with the namespace: fpst. only components in the same namespace as the organization can be created through the API

     public static Map<String, String> getAllFields(String sobjectname){
    if(!Schema.getGlobalDescribe().containsKey(sobjectname)) return new Map<String, String>{'Exception' => 'Invalid object name'};
    Map<String, Schema.SObjectField> fields = Schema.getGlobalDescribe().get(sobjectname).getDescribe().SObjectType.getDescribe().fields.getMap();

    List<String> accessiblefields = new List<String>();

    for(Schema.SObjectField field : fields.values()){

    String allfields='';

    for(String fieldname : accessiblefields)
        allfields += fieldname+',';

    allfields = allfields.subString(0,allfields.length()-1);

    return new Map<String, String>{sobjectname => allfields};

You could check if each field's local name is the same as the global (namespaced) name i.e.

for(Schema.SObjectField field : fields.values()){
    DescribeFieldResult fieldDescribe = field.getDescribe();
    if(fieldDescribe.isAccessible() && fieldDescribe.getName() == fieldDescribe.getLocalName())

See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fields_describe.htm#apex_Schema_DescribeFieldResult_getLocalName


To get all field, other approach we can create a fieldSet and in that just add all the required fields except managed package fields. And then retrieve fields of fieldset in apex using below code -

    String query = 'SELECT ';
    for(Schema.FieldSetMember fsm : SObjectType.Account.FieldSets.FieldSetName.getFields()){
        query += fsm.getFieldPath()+', ';
    query = query.substring(0, query.length()-2);
    query += ' FROM Account';
  • fieldset has 40 fields limit and in my scentio case has around 125 fields and other object also has more than 40 field and it is generic for all object so it wont work Mar 11 '19 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.