+ Start a Discussion
mt0859mt0859 

get "new object page" URL not using API

All, 

 

I'm looking for a way to retrieve the address of a create new object page using apex rather than api functions. For instance, in the api dvelopers guide in Java you use "describeSObject()" to populate a "describeSObjectResult" object from which you can use the field "urlNew"

 

Is there an equivalent of this in Apex or do I have to use a webservice to call out, then the Java api function to retrieve it! 

 

Thanks,

 

M

 

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

In Apex you can use describeSObject(), then from the result getKeyPrefix().  This gets you the basis for the 'new object page'.  Just add '/e' onto the end of the key prefix and you're away:

 

Schema.Describesobjectresult result = Schema.Sobjecttype.Account; ApexPages.Pagereference ref = new PageReference('/' + result.getKeyPrefix() + '/e');

All Answers

XactiumBenXactiumBen

In Apex you can use describeSObject(), then from the result getKeyPrefix().  This gets you the basis for the 'new object page'.  Just add '/e' onto the end of the key prefix and you're away:

 

Schema.Describesobjectresult result = Schema.Sobjecttype.Account; ApexPages.Pagereference ref = new PageReference('/' + result.getKeyPrefix() + '/e');

This was selected as the best answer
mt0859mt0859

apologies, I just realised I left out a key part of the problem,  I need to be able to do it when the object type is not known until run time so "schema.objecttype.Account" etc won't work. I need to be able to do it with a parameter retrieved from a record.

 

Thanks,

 

XactiumBenXactiumBen

You can also do this:

 

// Creates a map of all objects Map<String, Schema.SObjectType> objects = Schema.getGlobalDescribe(); // Get my key prefix String prefix = objects.get(myObject).getDescribe().getKeyPrefix(); PageReference ref = new PageReference('/' + prefix + '/e');

 

Or if you know the Id of the object you can just get the first 3 characters to get the key prefix too:

 

 

String prefix = ((String)myId).substring(0, 3); PageReference ref = new PageReference('/' + prefix + '/e');

 

 

Hope that helps.