I want a user to be able to create new new record for an object, but then be shown a message and not the inserted record itself, is there a method I can use for displaying a message other than addError()?
Attribution to: Kishan Trambadiya
Possible Suggestion/Solution #1
Displaying a Message After Insert
To allow a user to create a new record, but then see a message instead of the record you'll need to implement a VisualForce page using a controller extension, where the extension takes care of saving the record and then toggling a public variable used to control the onscreen display.
Some rough page could look something like this:
<apex:pageMessages/>
<apex:pageBlock rendered="{!theVariableToggledToFalse}">
<apex:pageBlockButtons>
<apex:commandButton action="{!SaveRecord}" value="Save" rerender=""/>
</apex:pageBlockButtons>
<apex:pageBlockSection>
<!-- fields here -->
</apex:pageBlockSection>
</apex:pageBlock>
Now you could add a new information message to be displayed when you save the record:
ApexPages.addMessage(new ApexPages.pageMessage(ApexPages.SEVERITY.INFO, "Record Saved!"));
Or, redirect them elsewhere via the PageReference
returned by your action method.
Note, this code isn't tested, it's just been written in browser!
Reporting Errors From Triggers (old answer)
If you're looking for an alternative to using addError()
you can also throw a custom exception, though it's handling by standard pages is less graceful than addError()
.
First define a new exception class:
public class MyException extends Exception {}
Then throw a new instance of this exception where you want things to grind to a halt:
if(someCondition == false)
{
throw new MyException("No good!");
}
Attribution to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1152