Get type of record from standard controller without accessing record?











up vote
1
down vote

favorite












I've got a component which is used on multiple object types in their visualforce pages. I have a controller which provides some data, and another to add some relevant fields.



The controller which adds some fields, now needs to conditionally add a field, based solely on the object type. Seems pretty simple, right? Lets just check the type of the record from getRecord() - wait, doing that causes a Cannot Modify Record before calling addFields() error. How about getId()? Works great, except for having to cast the return (a string) to an id. Oh wait, nope! It doesn't work if the record doesn't have an id.



Now I'm kind of stuck - how do I tell the object type of a record without accessing it, or its id? I don't see any methods for accessing this data in the standard controller class.



I've considered some funky methods, such as providing this data via attributes to the controller or trying to serialize the controller/record and parse the result sniffing for types, but these would access the record before calling getFields. In theory I could rely less on the field and use a fieldset instead, but I still need to know what Object in working with.



Heres some sample code:



public class FieldProvider {
public FieldProvider(ApexPages.StandardController controller) {
List<String> fields = new List<String>{
'Field__c',
'Another_Field__c',
'Third_Field__c'
};

// Fails if Id is null
// Can't use `getRecord` before calling `addFields`
if (Id.valueOf(controller.getId()).getsObjectType().getDescribe().fields.getMap().containsKey('Conditional__c')) {
fields.add('Conditional__c');
}

if (!Test.IsRunningTest()) { controller.addFields(fields); }
}
}


Tl;dr: I need to identify the type of this record, so I can tell if this field exists on the given object type. But, I can't access the record or its id.










share|improve this question


















  • 1




    Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
    – Adrian Larson
    Dec 5 at 17:48










  • I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
    – battery.cord
    Dec 5 at 17:52






  • 1




    You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
    – Adrian Larson
    Dec 5 at 17:53








  • 1




    Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
    – sfdcfox
    Dec 5 at 19:13






  • 1




    This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
    – Adrian Larson
    Dec 5 at 19:27















up vote
1
down vote

favorite












I've got a component which is used on multiple object types in their visualforce pages. I have a controller which provides some data, and another to add some relevant fields.



The controller which adds some fields, now needs to conditionally add a field, based solely on the object type. Seems pretty simple, right? Lets just check the type of the record from getRecord() - wait, doing that causes a Cannot Modify Record before calling addFields() error. How about getId()? Works great, except for having to cast the return (a string) to an id. Oh wait, nope! It doesn't work if the record doesn't have an id.



Now I'm kind of stuck - how do I tell the object type of a record without accessing it, or its id? I don't see any methods for accessing this data in the standard controller class.



I've considered some funky methods, such as providing this data via attributes to the controller or trying to serialize the controller/record and parse the result sniffing for types, but these would access the record before calling getFields. In theory I could rely less on the field and use a fieldset instead, but I still need to know what Object in working with.



Heres some sample code:



public class FieldProvider {
public FieldProvider(ApexPages.StandardController controller) {
List<String> fields = new List<String>{
'Field__c',
'Another_Field__c',
'Third_Field__c'
};

// Fails if Id is null
// Can't use `getRecord` before calling `addFields`
if (Id.valueOf(controller.getId()).getsObjectType().getDescribe().fields.getMap().containsKey('Conditional__c')) {
fields.add('Conditional__c');
}

if (!Test.IsRunningTest()) { controller.addFields(fields); }
}
}


Tl;dr: I need to identify the type of this record, so I can tell if this field exists on the given object type. But, I can't access the record or its id.










share|improve this question


















  • 1




    Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
    – Adrian Larson
    Dec 5 at 17:48










  • I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
    – battery.cord
    Dec 5 at 17:52






  • 1




    You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
    – Adrian Larson
    Dec 5 at 17:53








  • 1




    Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
    – sfdcfox
    Dec 5 at 19:13






  • 1




    This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
    – Adrian Larson
    Dec 5 at 19:27













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've got a component which is used on multiple object types in their visualforce pages. I have a controller which provides some data, and another to add some relevant fields.



The controller which adds some fields, now needs to conditionally add a field, based solely on the object type. Seems pretty simple, right? Lets just check the type of the record from getRecord() - wait, doing that causes a Cannot Modify Record before calling addFields() error. How about getId()? Works great, except for having to cast the return (a string) to an id. Oh wait, nope! It doesn't work if the record doesn't have an id.



Now I'm kind of stuck - how do I tell the object type of a record without accessing it, or its id? I don't see any methods for accessing this data in the standard controller class.



I've considered some funky methods, such as providing this data via attributes to the controller or trying to serialize the controller/record and parse the result sniffing for types, but these would access the record before calling getFields. In theory I could rely less on the field and use a fieldset instead, but I still need to know what Object in working with.



Heres some sample code:



public class FieldProvider {
public FieldProvider(ApexPages.StandardController controller) {
List<String> fields = new List<String>{
'Field__c',
'Another_Field__c',
'Third_Field__c'
};

// Fails if Id is null
// Can't use `getRecord` before calling `addFields`
if (Id.valueOf(controller.getId()).getsObjectType().getDescribe().fields.getMap().containsKey('Conditional__c')) {
fields.add('Conditional__c');
}

if (!Test.IsRunningTest()) { controller.addFields(fields); }
}
}


Tl;dr: I need to identify the type of this record, so I can tell if this field exists on the given object type. But, I can't access the record or its id.










share|improve this question













I've got a component which is used on multiple object types in their visualforce pages. I have a controller which provides some data, and another to add some relevant fields.



The controller which adds some fields, now needs to conditionally add a field, based solely on the object type. Seems pretty simple, right? Lets just check the type of the record from getRecord() - wait, doing that causes a Cannot Modify Record before calling addFields() error. How about getId()? Works great, except for having to cast the return (a string) to an id. Oh wait, nope! It doesn't work if the record doesn't have an id.



Now I'm kind of stuck - how do I tell the object type of a record without accessing it, or its id? I don't see any methods for accessing this data in the standard controller class.



I've considered some funky methods, such as providing this data via attributes to the controller or trying to serialize the controller/record and parse the result sniffing for types, but these would access the record before calling getFields. In theory I could rely less on the field and use a fieldset instead, but I still need to know what Object in working with.



Heres some sample code:



public class FieldProvider {
public FieldProvider(ApexPages.StandardController controller) {
List<String> fields = new List<String>{
'Field__c',
'Another_Field__c',
'Third_Field__c'
};

// Fails if Id is null
// Can't use `getRecord` before calling `addFields`
if (Id.valueOf(controller.getId()).getsObjectType().getDescribe().fields.getMap().containsKey('Conditional__c')) {
fields.add('Conditional__c');
}

if (!Test.IsRunningTest()) { controller.addFields(fields); }
}
}


Tl;dr: I need to identify the type of this record, so I can tell if this field exists on the given object type. But, I can't access the record or its id.







apex visualforce controller standardcontroller






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 5 at 17:45









battery.cord

6,63051744




6,63051744








  • 1




    Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
    – Adrian Larson
    Dec 5 at 17:48










  • I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
    – battery.cord
    Dec 5 at 17:52






  • 1




    You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
    – Adrian Larson
    Dec 5 at 17:53








  • 1




    Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
    – sfdcfox
    Dec 5 at 19:13






  • 1




    This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
    – Adrian Larson
    Dec 5 at 19:27














  • 1




    Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
    – Adrian Larson
    Dec 5 at 17:48










  • I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
    – battery.cord
    Dec 5 at 17:52






  • 1




    You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
    – Adrian Larson
    Dec 5 at 17:53








  • 1




    Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
    – sfdcfox
    Dec 5 at 19:13






  • 1




    This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
    – Adrian Larson
    Dec 5 at 19:27








1




1




Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
– Adrian Larson
Dec 5 at 17:48




Yeah with an Id, it's simple, but without, not so much. Why not just move everything to dynamic SOQL? Is that an option?
– Adrian Larson
Dec 5 at 17:48












I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
– battery.cord
Dec 5 at 17:52




I don't think so, there's two other extensions using the same pattern on the page. Plus it has to use the standard controller, otherwise I would have replaced the separate controllers with a single one already.
– battery.cord
Dec 5 at 17:52




1




1




You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
– Adrian Larson
Dec 5 at 17:53






You don't really need the field to be queried when it's new, though. Or do you get an exception in that case? Can you just do your Id check in the case where getId() returns a value? Calling addFields should essentially do nothing when you pass in a record not yet saved to the database.
– Adrian Larson
Dec 5 at 17:53






1




1




Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
– sfdcfox
Dec 5 at 19:13




Why would you need to "addFields" if it's a new record? There's no purpose in doing so, since you can't query a record that doesn't exist.
– sfdcfox
Dec 5 at 19:13




1




1




This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
– Adrian Larson
Dec 5 at 19:27




This is exactly why I was trying to figure out which exception you were running into. That's quite easy to work around. I thought you were running into an exception at a different step in the flow control.
– Adrian Larson
Dec 5 at 19:27










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You should just check if getId returns a value. Note that you can assign a String value to an Id variable.



Id recordId = controller.getId();
SObjectType sObjectType = (recordId == null) ?
null : recordId.getSObjectType();

if (sObjectType == Account.SObjectType || sObjectType == Contact.SObjectType)
{
fields.add('MyField__c');
}





share|improve this answer





















  • I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
    – battery.cord
    Dec 5 at 19:46










  • Yep. It's a query. Sounds like an informative morning. :)
    – Adrian Larson
    Dec 5 at 19:53


















up vote
3
down vote













So, I guess the only way to get the type of an object in a ApexPages.StandardController without accessing the record itself is to use String.ValueOf to get a text representation of the object underneath. Thankfully, someone had the foresight to make this useful information.



String objectType = String.valueOf(controller);
// This string looks like this:
// "ApexPages.StandardController[Account]"
// So we can "parse" it and get that object name,
// or we can use `Contains` to try and find a name inside the string

if (objectType.contains('[Lead]')) {
// Could also create new instance, or get describe by name
fields.add('Conditional__c');
} else if (objectType.contains('[Account]')) {
// Doesn't have field, don't add anything
} else if (objectType.contains('[Contact]')) {
fields.add('Conditional'); // Standard field, as example
}


This doesn't count as accessing the record and allows the controller to describe its type, even though it feels really brittle. Would really like another method to figure this out.





So this is actually a non-issue. AddFields is only nessescary when information has to be pulled out of the database, and does not affect the rendering of "new record" pages when not used. So, for any record with a valid ID, you can call the methods against the Id, for the rest of the records, you can just skip the call.



See: X-Y Problem.






share|improve this answer























  • So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
    – Adrian Larson
    Dec 5 at 18:47












  • Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
    – battery.cord
    Dec 5 at 19:20











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241547%2fget-type-of-record-from-standard-controller-without-accessing-record%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You should just check if getId returns a value. Note that you can assign a String value to an Id variable.



Id recordId = controller.getId();
SObjectType sObjectType = (recordId == null) ?
null : recordId.getSObjectType();

if (sObjectType == Account.SObjectType || sObjectType == Contact.SObjectType)
{
fields.add('MyField__c');
}





share|improve this answer





















  • I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
    – battery.cord
    Dec 5 at 19:46










  • Yep. It's a query. Sounds like an informative morning. :)
    – Adrian Larson
    Dec 5 at 19:53















up vote
1
down vote



accepted










You should just check if getId returns a value. Note that you can assign a String value to an Id variable.



Id recordId = controller.getId();
SObjectType sObjectType = (recordId == null) ?
null : recordId.getSObjectType();

if (sObjectType == Account.SObjectType || sObjectType == Contact.SObjectType)
{
fields.add('MyField__c');
}





share|improve this answer





















  • I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
    – battery.cord
    Dec 5 at 19:46










  • Yep. It's a query. Sounds like an informative morning. :)
    – Adrian Larson
    Dec 5 at 19:53













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You should just check if getId returns a value. Note that you can assign a String value to an Id variable.



Id recordId = controller.getId();
SObjectType sObjectType = (recordId == null) ?
null : recordId.getSObjectType();

if (sObjectType == Account.SObjectType || sObjectType == Contact.SObjectType)
{
fields.add('MyField__c');
}





share|improve this answer












You should just check if getId returns a value. Note that you can assign a String value to an Id variable.



Id recordId = controller.getId();
SObjectType sObjectType = (recordId == null) ?
null : recordId.getSObjectType();

if (sObjectType == Account.SObjectType || sObjectType == Contact.SObjectType)
{
fields.add('MyField__c');
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 5 at 19:31









Adrian Larson

104k19111234




104k19111234












  • I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
    – battery.cord
    Dec 5 at 19:46










  • Yep. It's a query. Sounds like an informative morning. :)
    – Adrian Larson
    Dec 5 at 19:53


















  • I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
    – battery.cord
    Dec 5 at 19:46










  • Yep. It's a query. Sounds like an informative morning. :)
    – Adrian Larson
    Dec 5 at 19:53
















I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
– battery.cord
Dec 5 at 19:46




I guess I confused the usage of the method.The documentation doesn't mention anything about this being used to make a query, the exact line is "This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well." Note the "access", not query. I assumed that the fields were used for more than just a basic query, and that a "reference" of some kind was needed for EVERY field regardless of context (none is given on that page, ie "dont call this for new records").
– battery.cord
Dec 5 at 19:46












Yep. It's a query. Sounds like an informative morning. :)
– Adrian Larson
Dec 5 at 19:53




Yep. It's a query. Sounds like an informative morning. :)
– Adrian Larson
Dec 5 at 19:53












up vote
3
down vote













So, I guess the only way to get the type of an object in a ApexPages.StandardController without accessing the record itself is to use String.ValueOf to get a text representation of the object underneath. Thankfully, someone had the foresight to make this useful information.



String objectType = String.valueOf(controller);
// This string looks like this:
// "ApexPages.StandardController[Account]"
// So we can "parse" it and get that object name,
// or we can use `Contains` to try and find a name inside the string

if (objectType.contains('[Lead]')) {
// Could also create new instance, or get describe by name
fields.add('Conditional__c');
} else if (objectType.contains('[Account]')) {
// Doesn't have field, don't add anything
} else if (objectType.contains('[Contact]')) {
fields.add('Conditional'); // Standard field, as example
}


This doesn't count as accessing the record and allows the controller to describe its type, even though it feels really brittle. Would really like another method to figure this out.





So this is actually a non-issue. AddFields is only nessescary when information has to be pulled out of the database, and does not affect the rendering of "new record" pages when not used. So, for any record with a valid ID, you can call the methods against the Id, for the rest of the records, you can just skip the call.



See: X-Y Problem.






share|improve this answer























  • So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
    – Adrian Larson
    Dec 5 at 18:47












  • Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
    – battery.cord
    Dec 5 at 19:20















up vote
3
down vote













So, I guess the only way to get the type of an object in a ApexPages.StandardController without accessing the record itself is to use String.ValueOf to get a text representation of the object underneath. Thankfully, someone had the foresight to make this useful information.



String objectType = String.valueOf(controller);
// This string looks like this:
// "ApexPages.StandardController[Account]"
// So we can "parse" it and get that object name,
// or we can use `Contains` to try and find a name inside the string

if (objectType.contains('[Lead]')) {
// Could also create new instance, or get describe by name
fields.add('Conditional__c');
} else if (objectType.contains('[Account]')) {
// Doesn't have field, don't add anything
} else if (objectType.contains('[Contact]')) {
fields.add('Conditional'); // Standard field, as example
}


This doesn't count as accessing the record and allows the controller to describe its type, even though it feels really brittle. Would really like another method to figure this out.





So this is actually a non-issue. AddFields is only nessescary when information has to be pulled out of the database, and does not affect the rendering of "new record" pages when not used. So, for any record with a valid ID, you can call the methods against the Id, for the rest of the records, you can just skip the call.



See: X-Y Problem.






share|improve this answer























  • So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
    – Adrian Larson
    Dec 5 at 18:47












  • Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
    – battery.cord
    Dec 5 at 19:20













up vote
3
down vote










up vote
3
down vote









So, I guess the only way to get the type of an object in a ApexPages.StandardController without accessing the record itself is to use String.ValueOf to get a text representation of the object underneath. Thankfully, someone had the foresight to make this useful information.



String objectType = String.valueOf(controller);
// This string looks like this:
// "ApexPages.StandardController[Account]"
// So we can "parse" it and get that object name,
// or we can use `Contains` to try and find a name inside the string

if (objectType.contains('[Lead]')) {
// Could also create new instance, or get describe by name
fields.add('Conditional__c');
} else if (objectType.contains('[Account]')) {
// Doesn't have field, don't add anything
} else if (objectType.contains('[Contact]')) {
fields.add('Conditional'); // Standard field, as example
}


This doesn't count as accessing the record and allows the controller to describe its type, even though it feels really brittle. Would really like another method to figure this out.





So this is actually a non-issue. AddFields is only nessescary when information has to be pulled out of the database, and does not affect the rendering of "new record" pages when not used. So, for any record with a valid ID, you can call the methods against the Id, for the rest of the records, you can just skip the call.



See: X-Y Problem.






share|improve this answer














So, I guess the only way to get the type of an object in a ApexPages.StandardController without accessing the record itself is to use String.ValueOf to get a text representation of the object underneath. Thankfully, someone had the foresight to make this useful information.



String objectType = String.valueOf(controller);
// This string looks like this:
// "ApexPages.StandardController[Account]"
// So we can "parse" it and get that object name,
// or we can use `Contains` to try and find a name inside the string

if (objectType.contains('[Lead]')) {
// Could also create new instance, or get describe by name
fields.add('Conditional__c');
} else if (objectType.contains('[Account]')) {
// Doesn't have field, don't add anything
} else if (objectType.contains('[Contact]')) {
fields.add('Conditional'); // Standard field, as example
}


This doesn't count as accessing the record and allows the controller to describe its type, even though it feels really brittle. Would really like another method to figure this out.





So this is actually a non-issue. AddFields is only nessescary when information has to be pulled out of the database, and does not affect the rendering of "new record" pages when not used. So, for any record with a valid ID, you can call the methods against the Id, for the rest of the records, you can just skip the call.



See: X-Y Problem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 19:32

























answered Dec 5 at 18:03









battery.cord

6,63051744




6,63051744












  • So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
    – Adrian Larson
    Dec 5 at 18:47












  • Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
    – battery.cord
    Dec 5 at 19:20


















  • So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
    – Adrian Larson
    Dec 5 at 18:47












  • Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
    – battery.cord
    Dec 5 at 19:20
















So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
– Adrian Larson
Dec 5 at 18:47






So simple. What an interesting find. I'd probably use regex instead but very neat trick! if (Pattern.compile('ApexPages.StandardController[(Account|Contact)]').matcher(String.valueOf(controller)).find()) { fields.add('Condditional__c') }
– Adrian Larson
Dec 5 at 18:47














Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
– battery.cord
Dec 5 at 19:20




Can't take credit for this one, I think I found the String.ValueOf trick somewhere else on the network.
– battery.cord
Dec 5 at 19:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Salesforce Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241547%2fget-type-of-record-from-standard-controller-without-accessing-record%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents