0

I try to implement insert as user and catch fields that user has no access to them but I face with problem that method does not exist.

try { update as user items; } catch(DMLException e) { System.debug(e.getFieldNames()); } 

error message that I get during deployment is Method does not exist or incorrect signature: void getFieldNames() from the type System.DmlException

enter image description here

According to SF documentation it should work but I haven't found any working examples in Google https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_enforce_usermode.htm

enter image description here

Any hints how I can make it work by using update as user and not Database.update()?

0

2 Answers 2

1

As per this documentation, the getDmlFieldNames() method is available for the get the names of the field or fields that caused the error described by the ith failed row not the getFieldNames() method.

try { update as user items; } catch(DMLException e) { for(Integer i = 0; i < e.getNumDML(); i++) { System.debug('Error on fields: ' + e.getDmlFieldNames(i)); System.debug('Error message: ' + e.getDMLMessage(i)); } } 
0

We do also have Database.update that accepts AccessLevel. You can use it if you want partial save on the DML else you would have to do with catching the exception as Tushar suggested.

public static Database.SaveResult update(SObject recordToUpdate, Boolean allOrNone, System.AccessLevel accessLevel) 
SaveResult[] results = Database.update( items, false, AccessLevel.USER_MODE ); Id[] failedIds = new Id[0]; String[] fieldsWithErrors = new String[0]; for(Database.SaveResult sr: results){ if( !sr.isSuccess() ){ for(Database.Error err : sr.getErrors()) { fieldsWithErrors.addAll(err.getFields()); //fields with errors } failedIds.add( sr.getId() ); } } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.