Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have an odd problem. A trigger was creating a record which failed to insert due to workflow field update being too long for the field. The error did appear in error log, but did not in the standard UI.

Here's code excerpt:

trigger cloneElecAMsignOff on Electricity_Meter__c (before update) {
     RecordType elecRT = [select Id from RecordType where Name = 'Elec' and SobjectType = 'Electricity_Meter__c' limit 1];
     List<Electricity_Meter__c> myList = new List<Electricity_Meter__c>();     
     for(Electricity_Meter__c a: trigger.new) {
        if (
        a.Account_Manager_Sign_Off_Date_Time__c != Null &&
        a.Clone_Date_Time_Stamp__c == Null &&
        a.RecordTypeId==elecRT.Id &&
        (
        a.Deal_Done_Not__c == 'Deal Done (Existing client / Existing meter)' ||
        a.Deal_Done_Not__c == 'Deal Done (New Meter, New Client)' ||
        a.Deal_Done_NOt__c == 'Deal Done (New Meter, Existing Client)' ))
        {       
        a.Clone_Date_Time_Stamp__c = System.Now();       
        Electricity_Meter__c b = new Electricity_Meter__c();                
        b.Name = a.Name;         
        myList.add(b);         
        }
        try {
        insert myList;
   } catch (system.Dmlexception e) {
        system.debug (e);      
    }                      
}

How do I ensure that:

1) The user gets error message.

2) a.Clone_Date_Time_Stamp__c = System.Now(); is not commited to the database.

Many thanks

share|improve this question
    
Can you give a try of throwing error in catch block like throw e; –  tandonprateek Jul 8 at 14:50

3 Answers 3

In your trigger code you are catching the exception. An uncaught exception would appear on the UI. You could consider something like catching the exception (in order to log it) then throwing an other more "user friendly" exception?

Also you can use Database.savepoint and rollback if you want to prevent changes being committed.

share|improve this answer

I usually recommend allowing partial saves while rolling up any remaining errors. You can do that like this:

SObject[] cloneList = new SObject[0], insertList = new Sobject[0];
for(SObject record: Trigger.new) {
    if(record meets clone conditions) {
        cloneList.add(record);
    }
}
for(Sobject record: cloneList) {
    insertList.add(new SObject( ... ));
}
Database.SaveResult[] saveResults = Database.insert(insertList, false);
for(Integer index = 0; index < saveResults.size(); index++) {
    if(!saveResults[index].isSuccess()) {
        cloneList[index].addError('Failed to clone: '+saveResults[index].getErrors()[0].getMessage());
    }
}

Note This is mad-lib code, you'll have to fill in the blanks/generics.

By rolling up errors to the appropriate record, you'll allow partial saves (e.g. data loader, import wizard), instead of an outright failure of the entire transaction by throwing/not handling an exception.

share|improve this answer

I think you're trying to solve the wrong problem here. IMO, your workflow field needs to be set to truncate at the maximum number of characters the field in your trigger can handle. You could use an Apex display message, but that won't fix a workflow problem which is at the root of your issue.

share|improve this answer
    
I am well aware of the root problem, however there may be N other problems down the road. My goals here is to ensure that record is not updated (prevent incorrect data entry) and display message to the user and allow him to correct the records before invoking admin. Good point though. –  dzh Jul 8 at 13:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.