I have trigger under certain condition, If condition: false
Error occurs. I should display that error message which occurs in database trigger to HTML
web page to notify user!
CREATE TRIGGER check_trigger BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
IF (NEW.name = NEW.firstname) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'same names error. Insertion canceled';
END IF;
END
How to display SET MESSAGE_TEXT=""
to web page? If it's not possible any other ways
You can add error messages in triggers by using Salesforce trigger addError method. For Example - Sobject. addError('Error Messages'); Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
You need to add <apex:pageMessages/> to display error messages on visualforce page. This component displays all messages that were generated for all components on the current page, presented using the Salesforce styling. i tried that too . do i need to write anything in side the <apex:pageMessages/>?
We can implement this requirement by creating new instance of ApexPages. message and then adding message to Apexpages using ApexPages. addmessage. Then displaying these messages in visualforce page.
You can take a look at the documentation here.
It says:
The error values that are accessible after SIGNAL executes are the SQLSTATE value raised by the SIGNAL statement and the MESSAGE_TEXT and MYSQL_ERRNO items. These values are available from the C API:
mysql_sqlstate()
returns theSQLSTATE
value.
mysql_errno()
returns theMYSQL_ERRNO
value.
mysql_error()
returns theMESSAGE_TEXT
value.
Since I'm guessing you're using PHP you can try
if (!$mysqli->query("INSERT INTO your_table SET name='a', firstname='a'")) {
printf("Error message: %s\n", $mysqli->error);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With