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
guna malliguna malli 

How to test class for after update trigger

How to write test class for this trigger

Here S__c different object, and R__c is different object in between no relationship
but S__c have this fields like A__c,B__c,C__c. same like that R__c have same fields
when ever S__c "status" have aprroved i want update the R__c "status" field completed

trigger Sname  on  S__c (after update) {
     for(S__c  s : Trigger.new){
        if(s.Status__c == 'Approved'){
       
            R__c  R = [select id,Status__c from R__c where (A__c =: s.A__c and  B__c =: s.B__c and C__c =: s.C__c)];
            R.Status__c ='Completed';
            update R;
          
        }
    }
}
Best Answer chosen by guna malli
LBKLBK
Let me know if this class helps.
 
@isTest
private class AfterUpdateSNameTest{
	public static testMethod void AfterUpdateAssociatePeopleStaticTestMethod()
	{

		S__c objS =new S__c(Name='TestS' ,A__c='A' ,B__c='B', C__c='C',Status__c='Open');
		insert objS;

		R__c objR =new R__c(Name='TestR' ,A__c='A' ,B__c='B', C__c='C',Status__c='Open');
		insert objR;

		objS.Status__c = 'Approved';
		update objS;
	}

}

 

All Answers

LBKLBK
Let me know if this class helps.
 
@isTest
private class AfterUpdateSNameTest{
	public static testMethod void AfterUpdateAssociatePeopleStaticTestMethod()
	{

		S__c objS =new S__c(Name='TestS' ,A__c='A' ,B__c='B', C__c='C',Status__c='Open');
		insert objS;

		R__c objR =new R__c(Name='TestR' ,A__c='A' ,B__c='B', C__c='C',Status__c='Open');
		insert objR;

		objS.Status__c = 'Approved';
		update objS;
	}

}

 
This was selected as the best answer
guna malliguna malli
Hi LBK,

Thanks for your quick replay.this test class is working fine.thank you  somuch LBK
i am new developer in sfdc please help me easy way to write test classes.
how to call subclasses,methods,and if else and for condition.please send some links or materials you have?
 
Thanks & best Regards
Guna


 
LBKLBK
Hi Guna,

There are basic syntax / keywords like @isTest, @isTest(SeeAllData=true), startTest, stopTest, etc...

You need to learn all of them.

For code coverage improvment, I suggest reading your code, understand the logic flow and prepare test data accordingly.

For example, if you have a condition check like below.
if(opp.StageName == 'Closed Won'){
  //Do stuff
}
else if (opp.StageName == 'Closed Lost'){
 //Do other stuff
}
else{
  //Do some other stuff 
}
These lines of code says that you need at least three opportunity records created (in your test data) with three different opportunity stages to get all three conditions covered.

More the number of conditions, more the number of test records to be created.

Does this make sense?
 
guna malliguna malli
Hi
Please check the below trigger.this trigger same like above trigger but added 1 line how to call that one please help me
when click on runtest shown error like this "Method doen't exist getcontact();"

Here S__c different object, and R__c is different object in between no relationship
but S__c have this fields like A__c,B__c,C__c. same like that R__c have same fields
when ever S__c "status" have aprroved i want update the R__c "status" field completed

trigger Sname  on  S__c (after update) {
     for(S__c  s : Trigger.new){
        if(s.Status__c == 'Approved'){
       
            R__c  R = [select id,Status__c from R__c where (A__c =: s.A__c and  B__c =: s.B__c and C__c =: s.C__c)];
            R.Status__c ='Completed';
            update R;
          
    LogController.fromTrigger(sid); // this is new line (Here  "Logcontroller" is onemore new apexclass,in that class have method like "fromtrigger"that method calling here .when click on runtest shown error like this "Method doen't exist getcontact();")
        }
    }
LBKLBK
Hi Guna,

This has to be an exception from the fromTrigger method. I cannot comment on it, unless I take a look at the code.
LogController.fromTrigger(sid); // this is new line (Here  "Logcontroller" is onemore new apexclass,in that class have method like "fromtrigger"that method calling here .when click on runtest shown error like this "Method doen't exist getcontact();")
Also, you are passing an attribute called sid to the fromTrigger method (in the above line of code). Where are you getting it from?
guna malliguna malli
How to write test class for this  new trigger. here using if else conditions

Here S__c different object, and R__c is different object in between no relationship
but S__c have this fields like A__c,B__c,C__c. same like that R__c have same fields
when ever S__c "status" have aprroved i want update the R__c "status" field completed

trigger Sname  on  S__c (after update) {
for(S__c  s : Trigger.new){
String message = '';
        if(s.Status__c == 'Approved'){
       
            R__c  R = [select id,Status__c from R__c where (A__c =: s.A__c and  B__c =: s.B__c and C__c =: s.C__c)];
            R.Status__c ='Completed';
            update R;
         message =('site'+'S.Name'+'is Approved'); 
}else if(s.Status__c == 'Submitted')  {
message =('site'+'S.Name'+'is Submitted'); 
}else if(s.Status__c == 'Reviewed'){
message =('site'+'S.Name'+'is Reviewed'); 
}else if(s.Status__c == 'Rejected'){
message ='site'+'S.Name'+'is Rejected');
}
String TMTO = ('1345678');
         LES.LSS(message, TMTO);
}
}