15

You could vote for this Provide an additional addError method on SObject that accepts a field name idea, but as it has got nowhere in the last 5 years there is no reason to expect any progress in the next 5 years.


12

Apex Code is single-threaded. This means that your code runs sequentially, from top to bottom, until the end of the method. If you have validation occurring first, followed by other updates, your code will run to its end. Once it ends, if any records received an error (via addError), and allOrNone is "false", then those records are set aside, any work done ...


9

The first message is provided to you "for free" when you try-catch a DMLException. Further, the reason why you only see one error in the second example is because duplicate messages are collapsed into a single instance. Therefore, even though you added it manually, there was no need to do so, because it was already added for you. If you don't want the first ...


8

Each field may return an independent error (e.g. multiple validation rules failed). In this case, the API allows to report each error on the appropriate field. Remember, this is an API that can be used to show errors on a third-party application, so reducing the number of round-trips is important. It could be from, for example, a half-dozen validation rules ...


7

Salesforce by default escapes any html in the addError String. To work around this, use the addError method with additional 'escape' parameter at the end: oppsByOppId.get(mrf.Opportunity__c).addError(' No Budget record was found for MRF record(s) with ' + errorMessage + '<br/>' + ' [WE_MRFv3]',false); The above should at least put [WE_MRFv3] on a new ...


7

You could call addError()on each record, but that would give you multiple messages instead of the bullet list you want. There is an overloaded form of the addError method that can display your error with un-escaped markup though that might get you what you want. Basically, you could build up an html formatted string with all the content you want to display, ...


7

The problem is this line here: objOpp = new Opportunity(id = mapOfOpportunity.get(eachKey)[0].Id); You're adding an error to a record that exists only in memory. Errors will only surface when you use addError on a record that is inside of Trigger.new or Trigger.old. You probably want to use the following code instead: objOpp = mapOfOpportunity.get(...


6

Your calling context has to allow partial success. This approach will fail every record: insert recordsWithDuplicates; This record will allow successful records to save: Database.insert(recordsWithDuplicates, /*allOrNone*/ false);


6

It only works on trigger context records, but it can be applied to those records outside of a trigger. You cannot call this method on a record which is not yet in a trigger context, then have the error carry through to the trigger context. This code won't prevent DML: Account record = new Account(); record.addError('You cannot insert this record'); insert ...


5

they will execute linear in your trigger. So: public void beforeUpdate( sObject oldObj, sObject newObj ){ // will execute first method1( oldObj, newObj ); // will execute second method2( oldObj, newObj ); } If you use a trigger factory to call a trigger handler and execute bulk methods first, you will see the bulk execution pass your ...


5

I don't think there is a direct method for determining if an sObject has errors on it. There might be a method to tell, but I would rely on the triggers order of operations here. Any DML operations will fail if there's an added error, so by running those processes in an after trigger context will ensure that the record survived the initial process without ...


5

You can only use addError on variables which are in trigger context. That means you can only addError in variable you get from Trigger.New or Trigger.newMap Here I can see, you are using Insertion_Order__c in SOQL and then adding error in it iol0 is not in triggger context , it fails in addError. Thus your code will be trigger IO_Billing_Entity_Update on ...


5

You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation: ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you ...


4

You'd have to actually perform a DML operation to call the trigger if you want to handle addError. Something like this would work: public void execute(Database.BatchableContext context, Foo__c[] scope) { Database.SaveResult[] results = Database.update(scope, false); // Process errors here } You can only use addError in a DML or Visualforce context. ...


4

If you're using try-catch, consider using addError(Exception) if you want to write smaller code[citation needed], and if you're not, use addError(String). In my experience, properly bulkified code will typically use addError(String). The addError(Exception) method is typically used when you're writing atomic updates, usually within a Visualforce page, and ...


4

The difference isn't apparent until you take a look further down on the documentation page you linked. When you get to the detailed descriptions of each method, you'll see additional information in the "signature" section addError(errorMsg) Marks a record with a custom error message and prevents any DML operation from occurring. Signature ...


4

I just directly rethrow DMLException. They are actually handled properly by the framework. So, my error handling often looks like this: try { upsert value; } catch(DmlException e) { throw e; } catch(Exception e) { throw new AuraHandledException(e.getMessage()); } If there is a DMLException, it arrives in Lightning as ...


4

addError won't block future DML operations, only those already in progress. You can use addError in any class, but it won't have any effect on future operations. As an example of what would work: trigger myFieldBlock on Opportunity { myFieldBlock.validate(Trigger.new); } public class myFieldBlock { public static void validate(Opportunity[] records) { ...


4

The decision of whether to allow partial success - permitting the 99 to go through to the database, while the 1 fails and is rejected - lies with the code that initiates the DML rather than with the trigger itself. Once the operation gets to the trigger, that decision has been made. The key distinction is how the caller initiated the operation. If they ran ...


4

I don't believe that this code can throw any system exceptions at all. If no Accounts are found, or if the input is nonsensical, the class will simply return an empty list. You need to add more code only if you want to provide a semantic response for the caller, such as "this request doesn't make sense", "no matching Account found", etc. In that case you ...


3

The save triggers a transaction. The transaction doesn't fail and get blocked immediately, but if there are several validation failures, each gets bundled up in its own instance of Database.Error for you to check. To illustrate this I created two validation rules on account. One if the account name was too short (fewer than 5 characters), the other that ...


3

You cant achieve this via trigger for sure as you have figured it out .With add error you will see everything rolling back and at same time you want to update status of lead as well which is not possible . Yes with your custom button you should be able to open a visualforce page and achieve desired output .But this means lot of visualforce and apex work . ...


3

You can't have it both ways. Using addError removes all side effects of the transaction, including sending emails, kicking off batch/scheduled/future calls, and committing data that was otherwise created during the trigger's life cycle. If you want to prevent conversion in some circumstances, you can override the lead convert button with a custom Visualforce ...


3

Basically, no. You can't do this with a trigger, because no memory is retained between each transaction on the server, except by "view state", which only occurs in Visualforce. You'll need to use Visualforce to get this kind of behavior. Also see this idea on using S-Controls and this idea for having standard confirmation dialogs for things you can vote on.


3

I have tried something similar in the past. For click to clear action Although I didn't get any close symbol, I was able to capture any click on the outputPanel which contained the pageMessage, and call an actionFunction which would simply reRender the panel. This would clear the messages. You could try to inject some symbol or message through JavaScript. ...


3

That should be possible. You would want to change it to be more like this though: ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Potential duplicate <a href="' + o.Id +'">Click Me</a>')); The markup will just be part of the String. You also need to render the apex:pageMessage with escape="false" for the HTML to be ...


3

No, each individual request is isolated, even if they come from the same user. The flag applies only to the current DML context. It's possible to use this flag in your apex code even when the originating request doesn't request it, but this will only apply to those records modified by the apex code, not the outer transaction. Also, there's no way to tell ...


3

Both CustomActivity__c and StudyTeam__c are children of Study__c, so you first need to get all the Study__c Records that are related to your list of CustomActivity__c records. You could do this with a set or map, I generally prefer maps, but it's not totally necessary here as your not really doing anything with the Study__c object, but I use a map anyway ...


2

The trouble is in the following line: if(!thisCase.SuppliedEmail.contains('directlineinc')) In your test you didn't initatlized this field thus this field is null and you tried to call contains() on null. You have to fill this field in your test: existingCase.SuppliedEmail= 'test@test.com'; But more correct way for fix this trouble is checking that the ...


2

First of all, the duplicate checking can be done in a couple of ways: Use the trigger recipe like this one Use SFDC OOB Duplicate Management - introduced in Spring(?) 15 As @ajay noted, addError() needs to be used against Trigger.new (or any variable that is a reference to Trigger.new) Duplicate checking involves checking intrabatch (are there any ...


Only top voted, non community-wiki answers of a minimum length are eligible