You need to sign in to do that
Don't have an account?

Deploy Triggers
Hi all,
I am a beginner developing apex code in salesforce. Now I have a problem with deploying triggers.
I have a trigger that will prevent updating opportunity:
trigger LockDownOpportunity on Opportunity (before update) { Opportunity[] opps = [select stageName from Opportunity where id in :Trigger.new]; Profile f = [select name from Profile where id=:UserInfo.getProfileId()]; for(Opportunity opp:opps){ if((opp.stageName=='Invoicing Complete'&&f.name=='xxxxx')||(opp.stageName=='Order Complete and Closed'&&f.name=='xxxxx')){ Opportunity actualRecord = Trigger.newMap.get(opp.Id); actualRecord.adderror('You do not have administrative rights to reopen this closed opportunity.'); } }
I wrote a test method for it and got a 100% code coverage. But it won't let me to deploy it and the error is: Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 0067000000Str1qAAB; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have administrative rights to reopen this closed opportunity.: []", Failure Stack Trace: "Class.testLockOpportunity.test: line 22, column 1"
It seems it stops me deploying it because of updating fail. But as I mentioned, I need the update to fail. How do I deploy the trigger? thank you all.
And here is my test code
@isTest public class testLockOpportunity{ static testMethod void test(){ Profile p = [select id from Profile where name='xxxxxx']; User testUser = new User(alias = 'u1', email='u1@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles', username='u1@testorg.com'); insert testUser; Opportunity opp = new Opportunity(stageName = 'Invoicing Complete', closeDate = Date.today()+30, Name = 'test', OwnerId = testUser.id); insert opp; System.runAs(testUser){ opp.Client_Email_Address__c = 'test@test.com'; Test.startTest(); update opp; Test.stopTest(); } } }
Put the insert statement of the test code inside a try-catch block, and catch the error from the trigger. Then you just create both negative and positive tests with asserts to validate correct behavior. Sorry I cannot write you better sample code, I am sending from my smartphone.
Try {
insert opp;
} catch (exception e) {
//assert your error message or record I'd is null....
}
// if error was not thrown then opp should have been inserted
System.assert(opp.Id !=null, 'some message with info in case assert fails...');
All Answers
Put the insert statement of the test code inside a try-catch block, and catch the error from the trigger. Then you just create both negative and positive tests with asserts to validate correct behavior. Sorry I cannot write you better sample code, I am sending from my smartphone.
Try {
insert opp;
} catch (exception e) {
//assert your error message or record I'd is null....
}
// if error was not thrown then opp should have been inserted
System.assert(opp.Id !=null, 'some message with info in case assert fails...');
My trigger is a update trigger, so I put the "update opp" statement in the try block but not the insert. But I got your idea and fixed my problem. Thank you so much.