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
ANJALIRAJIANJALIRAJI 

Test class for trigger Before update

 

The Trigger class is:

trigger FAAsettingCurrentLimit on Request__c(before update) {
    for( Request__cparent: Trigger.new)
    {
        if((parent.Request_Status__c=='Approved') && (Trigger.new[0].Request_Status__c != Trigger.old[0].Request_Status__c)){
            List<Limit__c> children = [ SELECT Id,Name,FAA_Request__c,Current_US_Dollar_Limit__c,Desired_US_Dollar_Limit__c from
            Limit__c where Request__c= :parent.Id];
        
            List<Limit__c> childrenToUpdate = new List<Limit__c>();
        
            for(Limit__c thischild: children )
            {
                thischild.Current_US_Dollar_Limit__c =thischild.Desired_US_Dollar_Limit__c  ;
           
                childrenToUpdate.add(thischild);
        
            }
            update childrenToUpdate;
        }
    }
}

There are two objects ,object 1-Request__c and object 2-Limit__c

 

The test class for this trigger class is

 

@isTest
private class FAACurrentLimitTest {

static testMethod void myUnitTest() {
Request__c[] faalist=[select id,Request_Status__c,Agent_Redelegate__c,Delegator_Approval_Status__c,
Delegator_Name__c,Delegator_Status_Date__c from Request__c limit 1];

Limit__c CategoryLimit=new Limit__c (Category__c = 'Treasury',
Desired_US_Dollar_Limit__c=100.00,Current_US_Dollar_Limit__c=120.00,Request__c=faalist[0].Id);
insert CategoryLimit;
Limit__c[] clist=[select id,Category__c ,Desired_US_Dollar_Limit__c,Current_US_Dollar_Limit__c,
Request__c where id=:CategoryLimit.id];
clist[0].Desired_US_Dollar_Limit__c=120.00;
update clist;


}
}

 

But its Showing error in (:CategoryLimit.id) and

if i remove that line the trigger name itself not displayed after running the  test class.

 

Navatar_DbSupNavatar_DbSup

Hi,

 

In below query there from clause is missing add the from clause in query

 

Limit__c[] clist=[select id,Category__c ,Desired_US_Dollar_Limit__c,Current_US_Dollar_Limit__c,
Request__c where id=:CategoryLimit.id];

 

Try the below code snippet as reference:

 

Limit__c[] clist=[select id,Category__c ,Desired_US_Dollar_Limit__c,Current_US_Dollar_Limit__c,
Request__c from Limit__c where id=:CategoryLimit.id];

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

@isTest
private class FAACurrentLimitTest {

static testMethod void myUnitTest() {
Request__c req = new Request__c();

req.Request_Status__c='Approved';

.

.//here insesrt the mandatory fields
.

insert req;

.Limit__c CategoryLimit=new Limit__c (Category__c = 'Treasury',
Desired_US_Dollar_Limit__c=100.00,Current_US_Dollar_Limit__c=120.00,Request__c=req.Id);
insert CategoryLimit;

req.Request_Status__c='Open';


update req;
}
}

 

Regards,

Rajesh S.