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
Andrew Hoban 6Andrew Hoban 6 

Test class for .adderror

Hi all,

I have created a trigger that has 75% code coverage. I am unsure how to set an .adderror from my trigger. Dones anyone have a code sample of how this can be achieved?

Thanks

Trigger:
trigger PicklistLimitTrigger on Software_Assets__c(before insert,before update){
Integer count =[select count() from Software_Assets__c where Software_Product__c='Windows 7'];
for(Software_Assets__c a:trigger.new){
if(count==3&&a.Software_Product__c=='Windows 7'){
a.addError('Please chose some other pick-list Values');
}
}
}

Test Class:
@isTest
private class PickListLimitTriggerTest {
    static testMethod void PickListLimitTrigger() {
 Software_Assets__c sa = new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7');
 
 insert sa;
 
 }
}

 
Best Answer chosen by Andrew Hoban 6
MissedCallMissedCall
Try this code, (make sure you include an appropriate value in line 22 for Asset_Status__c)
 
@isTest(seeAllData=true)
private class PickListLimitTriggerTest {
    
	static testMethod void PickListLimitTrigger() {
	
		List<Software_Assets__c> softwars = new list<Software_Assets__c>();
		List<Software_Assets__c> upSoftwars = new list<Software_Assets__c>();

		Integer startCountSoftwars = [SELECT count() FROM Software_Assets__c WHERE Software_Product__c = 'Windows 7' AND CreatedDate = TODAY];
		
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		
		Database.insert(softwars);
		
		Integer endCountSoftwars = [SELECT count() FROM Software_Assets__c WHERE Software_Product__c = 'Windows 7' AND CreatedDate = TODAY];
		
		System.assetEquals(startCountSoftwars + 3, endCountSoftwars);
		
		for(Software_Assets__c sA: softwars){
			upSoftwars.add(new Software_Assets__c(Id = sA.Id, Asset_Status__c = 'Some other value'));		
		}
		
		Database.update(upSoftwars);
	
	}
}

 

All Answers

Subhash GarhwalSubhash Garhwal
Hi Andrew,

Replace your test call by this one
@isTest
private class PickListLimitTriggerTest {
    static testMethod void PickListLimitTrigger() {
	
	List<Software_Assets__c> softwars = new list<Software_Assets__c>();

	softwars.adds(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
	softwars.adds(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
	softwars.adds(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
	
	insert softwars;
	
	//Start Test fro here
	Test.startTest();

	//Update
	softwars[0];

	//End Test here
	Test.StopTest();

}
}

Regards
Subhash
Andrew Hoban 6Andrew Hoban 6
Hi, I am getting the error message 
Error: Compile Error: Method does not exist or incorrect signature: [LIST<Software_Assets__c>].adds(SOBJECT:Software_Assets__c) at line 7 column 5

Thanks
Subhash GarhwalSubhash Garhwal
Repalce softwars.adds to softwars.add 
Andrew Hoban 6Andrew Hoban 6
Thankyou however I am now getting the error message Expression cannot be a statement at line 17 column 14
MissedCallMissedCall
Try this code, (make sure you include an appropriate value in line 22 for Asset_Status__c)
 
@isTest(seeAllData=true)
private class PickListLimitTriggerTest {
    
	static testMethod void PickListLimitTrigger() {
	
		List<Software_Assets__c> softwars = new list<Software_Assets__c>();
		List<Software_Assets__c> upSoftwars = new list<Software_Assets__c>();

		Integer startCountSoftwars = [SELECT count() FROM Software_Assets__c WHERE Software_Product__c = 'Windows 7' AND CreatedDate = TODAY];
		
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		softwars.add(new Software_Assets__c(Asset_First_Recorded__c = date.today(), Asset_Status__c = 'Operational', Vendor__c = 'Microsoft', Software_Product__c = 'Windows 7'));
		
		Database.insert(softwars);
		
		Integer endCountSoftwars = [SELECT count() FROM Software_Assets__c WHERE Software_Product__c = 'Windows 7' AND CreatedDate = TODAY];
		
		System.assetEquals(startCountSoftwars + 3, endCountSoftwars);
		
		for(Software_Assets__c sA: softwars){
			upSoftwars.add(new Software_Assets__c(Id = sA.Id, Asset_Status__c = 'Some other value'));		
		}
		
		Database.update(upSoftwars);
	
	}
}

 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thanks for your reply. I am getting the error message "System.DmlException: Update failed. First exception on row 0 with id a1Rf0000000gYQlEAM; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please chose some other pick-list Values: []" (This is the validation error that was created in the trigger)

Thanks
MissedCallMissedCall
Remove line 25 and insert this snippet there,
 
try{
			Database.update(upSoftwars);
		} Catch (exception ex){
		
		}

 
Andrew Hoban 6Andrew Hoban 6
Thats worked. Thank you to all.