function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajaMohanRajaMohan 

How to display alert message using trigger??

Hi,

 

I am implementing a trigger in Accounts. If some conditions becomes true then I need to display an alert message to the user.

 

I searched in the discussion boards?? But I didnt find anything reg this.

 

Is there any way to achieve this?

 

If so then pls give some examples..

 

Thanks,

MJ09MJ09

You can't display an alert from a trigger -- a trigger doesn't have any direct access to the user interface. However, your trigger can flag a record as having an error.

 

for (Account a : Trigger.new) {
  if ( /* whatever your error condition is */ ) {
    a.addError('Your custom error message');
  }
}

 

When the code or page that inserted your Accounts runs, it'll detect that you've added an error and display the message you specified.

 

RajaMohanRajaMohan

Hi MJ,

 

Thanks for ur interest.

 

Is there any way to display an alert message using the apex class??

MJ09MJ09

It depends on what you mean by an "alert message."

 

If you mean that you want the user to see an error message at the top of a standard page, then flagging the record with an error in the trigger will accomplish that.

 

If you mean that you want a little window to pop up and display an error message, then you'll need to use a Visualforce page in conjunction with an Apex class that serves as a custom controller or controller extension.

Amit@sf.ax1376Amit@sf.ax1376

its really usefull for me....i m littlebit confused that can i able to show popup by trigger...but ur example that one line solve it.....

 

thanks MJ09....

 

can we chech duplicacy of apicklist in runtime by trgger....suppose i have 2 row in a pageblocktable and 2 picklist in that 2 rows and have same item..if i select item1 from 1st list and again want to chooose item1 from 2nd picklist it will show error at that moment of time...

 

can we do that by any means...MJ09....

 

MJ09MJ09

You've got a choice here. You mentioned having a pageBlockTable, so I assume you mean you're in a Visualforce page. You can write some Apex code that ensures that the data from that pageBlockTable is consistent (no duplicates) before the user saves. Another alternative is to write an Apex Trigger that fires every time one of the child record changes, and verifies that there are no duplicates in the data.

 

There's lots of documentation and training on how to write Triggers -- if you're not familiar with how to do it, you should review the documentation, and maybe even take a course (online or in person). Alternatively, you might want to talk with a consultant (my company, OpFocus, is one good example) who can write the trigger for you and provide you with some training in the process.

 

s singh 46s singh 46
Hi,
From trigger, you can not show the alert message. If you want to show the alert message on the detail page of the record , just create a VF page and put it on the detail page layout as inline page.
Whenever your condition meets, that page can display the message.
 
Amit Kumar1Amit Kumar1
There is another way Try this, trigger allows the user interface directly. But it will only work for a single record.It will be failed in Bulk records.
 
trigger.new[0].adderror('Your message');


Better to use the trigger.new and iterate through all records

for(Account aa: Trigger.new){
aa.adderror('Your Message');
}