Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display trigger error message to Webpage?

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

like image 687
Shukurillo Baykhanov Avatar asked Jul 02 '19 12:07

Shukurillo Baykhanov


People also ask

How do I display error messages in triggers?

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.

How do I display a trigger error message in Visualforce page?

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/>?

How do I show errors in Apex?

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.


1 Answers

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 the SQLSTATE value.

mysql_errno() returns the MYSQL_ERRNO value.

mysql_error() returns the MESSAGE_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);
}
like image 70
Teneff Avatar answered Nov 02 '22 05:11

Teneff