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
HarryBHarryB 

Trigger should display "note" once, rather than an error message

Hi,

 

I have to get this implemented and just don't know how:

 

When a user edits a record (opportunity to be exact) and hits "Save", a "before update" trigger should display a note message (via addError) - however if the user hits "Save" again, the record should be saved. So I have to get rid of addError in the 2nd run of the trigger...

 

Any input appriciated!

 

Cheers,

Harry

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

 

<apex:page standardController="Account" extensions="account_extend">
	<apex:form>
		<apex:pageBlock title="Account Edit">
			<apex:pageBlockButtons>
				<apex:commandButton value="Save" action="{!saveRecord}"/>
				<apex:commandButton value="Cancel" action="{!cancel}"/>
			</apex:pageBlockButtons>
			<apex:messages/>
			<apex:pageBlockSection columns="2" title="Account Information">
				<apex:inputField value="{!account.name}"/>
				<apex:inputField value="{!account.industry}"/>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class account_extend {
	public account_extend(ApexPages.StandardController controller) {
		shownWarning = false;
		this.controller = controller;
	}
	private boolean shownWarning;
	private ApexPages.StandardController controller;
	
	public PageReference saveRecord() {
		if(!shownWarning) {
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO,'You are about to do something you can not undo. Press "Save" to confirm.');
			ApexPages.addMessage(msg);
			shownWarning = true;
			return null;
		}
		return controller.save();
	}
}

Try not to get caught up in the "extras", just pay attention to the saveRecord method; in here, I check to see if I've already produced a warning, and if not, I do so, otherwise I save. This is a trivial example, but I hope it helps you out.

 

All Answers

sfdcfoxsfdcfox

You can't do this. Use Visualforce instead. I've already barked up this tree, and I realized that each transaction is a separate transaction, so it can't determine if the addError message has already been shown. I've even tried things like modifying a field value and using addError, and the result is that the changes are lost.

HarryBHarryB

Thanks for the hint. How exactly would you solve this using VF?

 

Regards,

Harald

sfdcfoxsfdcfox

 

<apex:page standardController="Account" extensions="account_extend">
	<apex:form>
		<apex:pageBlock title="Account Edit">
			<apex:pageBlockButtons>
				<apex:commandButton value="Save" action="{!saveRecord}"/>
				<apex:commandButton value="Cancel" action="{!cancel}"/>
			</apex:pageBlockButtons>
			<apex:messages/>
			<apex:pageBlockSection columns="2" title="Account Information">
				<apex:inputField value="{!account.name}"/>
				<apex:inputField value="{!account.industry}"/>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class account_extend {
	public account_extend(ApexPages.StandardController controller) {
		shownWarning = false;
		this.controller = controller;
	}
	private boolean shownWarning;
	private ApexPages.StandardController controller;
	
	public PageReference saveRecord() {
		if(!shownWarning) {
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO,'You are about to do something you can not undo. Press "Save" to confirm.');
			ApexPages.addMessage(msg);
			shownWarning = true;
			return null;
		}
		return controller.save();
	}
}

Try not to get caught up in the "extras", just pay attention to the saveRecord method; in here, I check to see if I've already produced a warning, and if not, I do so, otherwise I save. This is a trivial example, but I hope it helps you out.

 

This was selected as the best answer
HarryBHarryB

Ah, ok. I was hoping there is a solution that does not require to rebuild the entire "edit form" of the concerned object.

 

Thanks anyway, thats helping a lot.

 

Regards,

Harry