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
Bhaskar2013Bhaskar2013 

Why Schema.getGlobalDescribe().get(ObjectName) not returning SObjectType?

I am working on Metadata API to develop Webservices for Custom Object management.

 

Here is part of  the code, which accepts a  string of object name and gets all the fileds. The line-2 of the code is not working.

 

So I had to get all the objects in Schema, looping through them and get matching object type of what I require.

But is there an efficient way of doing the same? Please help me.

==================

 

String sObjectName='MyCustomObject__c';

//Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectName); //This is returning null

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();

Schema.SObjectType objToken;

for(Schema.SObjectType obj: gd){        

    if(Obj.getDescribe().getName().toLowerCase()==sObjectName.toLowerCase()){   

         objToken=obj;            

         break;           

    }

 }

Schema.DescribeSObjectResult typedescription = objToken.getDescribe();

 

//...Now I can continue working with my object  field changes

Swati GSwati G
Hi,

Please iterate over the keys of global describe results and check if it contains MyCustomObject__c as one of the key.
nbknbk
can u check this link. Check 'createDynamicQuery' method, hope it helps.

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AHVtIAO
Gunther Roskams 27Gunther Roskams 27
Probably you found allready the answer. The key in the map is only with lower case. You need to convert your string to lowercase.
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectName.toLowerCase());