You need to sign in to do that
Don't have an account?
Everton CP7
Test Class
Hi there,
I need a little help for this trigger.
trigger NaoFechaChamado on Case (after update) { Map<Id, Task> taskMap = new Map<Id, Task>(); for(Task t : [SELECT Id, WhatId FROM Task WHERE IsClosed=false AND WhatId IN :trigger.newMap.keySet()]) { taskMap.put(t.WhatId, t); } for(Case c : Trigger.new) { if(taskMap.containsKey(c.Id) && c.IsClosed && c.IsClosed != Trigger.oldMap.get(c.Id).IsClosed) c.addError('Você não pode fechar o chamado enquanto houver tarefas pendentes'); } }
I'm trying do this test class and keep getting me errors.
I'm brand new in Apex.
So, this little help will be a great help for me.
Thanks !
Hi ,
Try the below one :-
@IsTest(SeeAllData=true)
public class NaoFechaChamado_Test{
static testmethod void MyUnitTest(){
User u =[select id from User where name='<some user name in org>'];
Case cse = new Case(Status='New',Origin='Web');
insert cse;
Task tsk = new Task(OwnerId=u.id,Subject='Call',Status='Not Started',WhatId=cse.id);
insert tsk;
cse.Status='Closed';
update cse;
}
}
All Answers
Hi Everton,
Please try below code :-
@IsTest(SeeAllData=true)
public class NaoFechaChamado_Test{
static testmethod void MyUnitTest(){
User u =[select id from User where name='<some user name in org>'];
Case cse = new Case(Status='New',Origin='Web',IsClosed=false);
insert cse;
Task tsk = new Task(OwnerId=u.id,Subject='Call',Status='Not Started',WhatId=cse.id);
insert tsk;
cse.IsClosed=true;
update cse;
}
}
Thanks Vinit, you always helping me here.
I tried a code like this and the error was "Field is not writeable Case.IsClosed"
Now, I tried your code and the same error appears to me.
Thanks again !
Hi ,
Try the below one :-
@IsTest(SeeAllData=true)
public class NaoFechaChamado_Test{
static testmethod void MyUnitTest(){
User u =[select id from User where name='<some user name in org>'];
Case cse = new Case(Status='New',Origin='Web');
insert cse;
Task tsk = new Task(OwnerId=u.id,Subject='Call',Status='Not Started',WhatId=cse.id);
insert tsk;
cse.Status='Closed';
update cse;
}
}
The test runs and save.
But when I will deploy doens't.
I don't undertstand are you able to get the code coverage with this Test Class,if yes then what is the error you are getting while deployong the code.
Please make it clear.
Don't appear any message to me, when I will depoly to my production the Status="Fail"
Everton,
Not sure how to help you.
I think you are deploying through Change Sets.Can you click on View Results link and see what is comeng there?
So sorry, I just notice this button right now.
I think I have to read more about Apex...
There is a way to deploy without using Change Sets?
In the first time this error appears:
NaoFechaChamado_Test.MyUnitTest()
Class
8 1
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A origem do caso só é WEB quando é feita por formando (chamado aberto pelo site, ou pelo facebook): [Origin]", Failure Stack Trace: "Class.NaoFechaChamado_Test.MyUnitTest: line 8, column 1"
NaoFechaChamado
Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
I change the field "Origin" to "Priority".
And this appears:
NaoFechaChamado_Test.MyUnitTest()
Class
14 1
Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 500U0000006kedOIAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Identifique o "TIPO DE REGISTRO".: []", Failure Stack Trace: "Class.NaoFechaChamado_Test.MyUnitTest: line 14, column 1" NaoFechaChamado Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
I have a validation rule based on the status=Closed:
AND ( $RecordType.Name = "Casos Web", ISPICKVAL( Status, "Closed")).
I disabled this validation rule and this appear:
NaoFechaChamado_Test.MyUnitTest()
Class
14 1
Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 500U0000006keeMIAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Você não pode fechar o chamado enquanto houver tarefas pendentes: []", Failure Stack Trace: "Class.NaoFechaChamado_Test.MyUnitTest: line 14, column 1"
So,you are getting errors because of some validation rule in your production org.There are 2 ways to overcome it,
1.) You can deactivate the Validation rule,deploy your code and then reactivate it.(Not Recommended)
2.) Change your test class so that it respects the validation rule in your prod org and then deploy it.(Recommended)
Thank you so much Vinit !
I change the end test class.
cse.addError('Você não pode fechar o chamado enquanto houver tarefas pendentes');
update cse;
You're awesome !
Everton,
Happy to help :)