You need to sign in to do that
Don't have an account?
Rajesh SFDC
test calss how to get above 75% in apex trigger?
how to increase code coverage now i am geeting a 55% in apex trigger...
trigger test on Case (before update )
{
Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();
case caseold=trigger.old[0];
For (Case cs : Trigger.new)
{
for (string fieldName : caseFields.keySet())
{
if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
{
// cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to ' +string.valueOf(cs.get(fieldName));
string oldvalue = string.valueOf(caseold.get(fieldName));------------->doesnot executing
string newvalue= string.valueOf(cs.get(fieldName));------------->doesnot executing
cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
break;
}
}
}
}
=============================================
@isTest
private class Testcasehistory
{
static testMethod void updatefields()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cslist.add(cs);
test.startTest();
insert cslist;
Case updcase = [SELECT Id,Origin,Reason,Priority,Status,Type FROM Case WHERE Id=:cs.Id];
update updcase;
test.stopTest();
}
}=====================
trigger test on Case (before update )
{
Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();
case caseold=trigger.old[0];
For (Case cs : Trigger.new)
{
for (string fieldName : caseFields.keySet())
{
if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
{
// cs.Track_status_history__c = 'Field ' + fieldName + ' changed from ' + string.valueOf(Trigger.oldMap.get(cs.id).get(fieldName)) + ' to ' +string.valueOf(cs.get(fieldName));
string oldvalue = string.valueOf(caseold.get(fieldName));------------->doesnot executing
string newvalue= string.valueOf(cs.get(fieldName));------------->doesnot executing
cs.Track_status_history__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue;
break;
}
}
}
}
=============================================
@isTest
private class Testcasehistory
{
static testMethod void updatefields()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cslist.add(cs);
test.startTest();
insert cslist;
Case updcase = [SELECT Id,Origin,Reason,Priority,Status,Type FROM Case WHERE Id=:cs.Id];
update updcase;
test.stopTest();
}
}=====================
You have to make some change in your test class in update statement.
see following code.
Test class:
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in
All Answers
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
To cover the uncovered lines, you just need to update one field just before doing the "update updcase;" in your unit test.
You can try by doing something like :
You have to make some change in your test class in update statement.
see following code.
Test class:
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in