You need to sign in to do that
Don't have an account?
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
Please iterate over the keys of global describe results and check if it contains MyCustomObject__c as one of the key.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AHVtIAO
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectName.toLowerCase());