You need to sign in to do that
Don't have an account?
Colbridge
apex code coverage for error records in database method
How to get the coverage for the // dml operation failed part?
// Test class:
// records to be updated received from json jsonBody = '[{"count__c":"445", "downloads__c":"340"}, {"count__c":"440", "downloads__c":"240"}]'; List<Data__c> dList = (List<Data__c>) System.JSON.deserialize(jsonBody, List<Data__c>.class); countList has unique count__c values, say: 445,440 // to use in the IN clause. // Querry parent for those plan ids in daily data json List<Parent__c> parentList = [SELECT Id, Name FROM Parent__c WHERE count__c IN :countList]; List<Data__c> dataToInsert = new List<Data__c>(); // Loop through dList - inner loop for(Data__c dRecords : dList) { for(Parent__c parentRecords : parentList) { if(dRecords.count__c == parentRecords.count__c) { dRecords.downloads__c = parentRecords.downloads__c ; dataToInsert.add(dRecords ); } } } List srList = Database.insert(dataToInsert, false); for(Integer i=0;i<srList.size();i++){ if (srList.get(i).isSuccess()){ srList.get(i).getId(); }else if (!srList.get(i).isSuccess()){ // DML operation failed Database.Error error = srList.get(i).getErrors().get(0); String failedDML = error.getMessage(); recsToInsert.get(i);//failed record from the list system.debug('Failed Id: '+recsToInsert.get(i).Your_field__c); } }
// Test class:
@isTest private class Connect_Test { @isTest static void testCall() { Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator' LIMIT 1]; user intUser = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Integration User', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='intuser@co.com'); insert intUser; System.runAs(intUser) { List<Parent__c> ms = new List<Parent__c>(); Parent__c msRec1 = new Parent__c(count__c = '445'); Parent__c msRec2 = new Parent__c(count__c = '440'); ms.add(msRec1); ms.add(msRec2); insert ms; String strIds = '445, 440'; list<String> countList = strIds.split(','); List<Parent__c> parentList = [SELECT Id, Name FROM Parent__c WHERE count__c IN :countList]; List<Data__c> recsToInsert = new List<Data__c>(); Data__c ddRecs1 = new Data__c(downloads__c = 'a1IO001110AakH2MAJ'); Data__c ddRecs2 = new Data__c(downloads__c = null); recsToInsert.add(ddRecs1); recsToInsert.add(ddRecs2); try { List<Database.SaveResult> srList = Database.insert(recsToInsert, false); } catch(DMLException e) { throw new DMLException('Unable to Perform the DML Operation: ' +e.getMessage()); } Test.startTest(); Test.setMock(HttpCalloutMock.class, new MockHttpResponse()); Connect.call(); Test.stopTest(); } } }
Data__c ddRecs2 = new Data__c();
Eventhough coverage has increased from 66 to 73, the
}else if (!srList.get(i).isSuccess()){
part is still shown in red!
All Answers
Can you provide the error message?
Please find the below article.
https://salesforce.stackexchange.com/questions/265160/how-to-write-test-class-for-database-error
If this solution helps, Please mark it as best answer.
Thanks,
Data__c ddRecs2 = new Data__c();
Eventhough coverage has increased from 66 to 73, the
}else if (!srList.get(i).isSuccess()){
part is still shown in red!