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
kingsixkingsix 

Don't know how to write test class for trigger

I have wrote some triggers in sandbox, but I don't know how to write test class for trigger?

 

This is one of my trigger, very simple,

 

 

trigger Accountchecking on Account (before insert) {
   if (Trigger.new[0].Name != NULL && Trigger.new[0].group__C != NULL && Trigger.new[0]. BillingCountry  != NULL)  {
       Account[] acc = [ select id from account WHERE name = :Trigger.new[0].Name and group__C=: trigger.new[0].group__C and  BillingCountry =:Trigger.new[0].BillingCountry];
       if (acc.size() > 0) {
           Trigger.new[0].Name.addError('Account Name ,Group and Country cannot be repeated!');
       }
   }
}

 

So, I should write one test class in apex, and then the trigger can be seen in org?

 

Great Thanks

 

 

Shashikant SharmaShashikant Sharma

 

@isTest
private class testTrigger
{
    private void static isTestMethod testAccountchecking()
    {
           Account acc = new Account();
           acc.Name = 'TsetName';
           acc.group__c = 'TestGroup';
           acc.BillingCountry = 'United States';
           insert acc;
           test.startTest();
           Account accNew = new Account(); 
           accNew.Name = 'TsetName';
           accNew.group__c = 'TestGroup';
           accNew.BillingCountry = 'United States';
           insert accNew; 
           system.assert(accNew.id == null); 
           test.stopTest()
    }
}

 

Here is your code

 

 

Shashikant SharmaShashikant Sharma

 

@isTest
private class testTrigger
{
    public static TestMethod void testAccountchecking()
    {
           Account acc = new Account();
           acc.Name = 'TsetName';
           acc.group__c = 'TestGroup';
           acc.BillingCountry = 'United States';
           insert acc;
           test.startTest();
           Account accNew = new Account(); 
           accNew.Name = 'TsetName';
           accNew.group__c = 'TestGroup';
           accNew.BillingCountry = 'United States';
           insert accNew; 
           system.assert(accNew.id == null); 
           test.stopTest()
    }
}

 Use this one instead of earlier

 

Ankit AroraAnkit Arora

Just a FYI my dear friend, you can edit your previous reply.

 

 

Thanks
Ankit Arora

 

kevin.chileskevin.chiles

Perhaps you can assist me with my test class as well?  Here is my trigger. 

 

 

Trigger updateRelatedOpportunityclosedlost on dsfs__DocuSign_Status__c(after update){

List<Id> oppIds = new List<Id>();
for(dsfs__DocuSign_Status__c dc:trigger.new){
if(dc.dsfs__Declined_Date_Time__c <>NULL){ //Assuming the field on Docusign object is Status__c
oppIds.add(dc.dsfs__Opportunity__c); //Assuming the relationship field on Docusign object is Opportunity__c

}
}

List<Opportunity> oppList = [select StageName from Opportunity where id in:oppIds];
List<Opportunity> oppListnew = new List<Opportunity>();

for(Opportunity op : oppList){
op.StageName='Closed Lost';
oppListnew.add(op);
}
update oppListnew;
 
}

The object this is on is the dsfs__DocuSign_Status__c object.  Enveloper # is the only required field

NGhosNGhos
hey  I Don't know how to write test class for trigger..can anyone help me pls

trigger is:

trigger DeliverySeasonalBeforeUpdateTrigger on Delivery_Seasonal__c (before insert, before update) {
    List<Delivery_Seasonal__c> deliverySeasonals= Trigger.new;
    Vaccine_Ordering_Form__c vaccine_Ordering_Form=null;
    for (Delivery_Seasonal__c delivery_Seasonal :deliverySeasonals) {       
        vaccine_Ordering_Form=[select CustomerType123__c, Product_Area__c,Order_Type__c,Total_Order_Value__c from Vaccine_Ordering_Form__c where id=:delivery_Seasonal.DeliverySeasonal__c];
        if(vaccine_Ordering_Form.Order_Type__c == 'In-Season' || vaccine_Ordering_Form.Order_Type__c == 'Seasonal'){
        vaccine_Ordering_Form.Order_Type__c = 'None';
        }
        Pricing_Model__c[] pricingModels=[select id from Pricing_Model__c where
        Product__c=:delivery_Seasonal.Product__c and
        Customer_Type__c=:vaccine_Ordering_Form.CustomerType123__c and
        Product_Area__c=:vaccine_Ordering_Form.Product_Area__c and
        Order_Type__c=:vaccine_Ordering_Form.Order_Type__c and
        (
            //Order_Volume__c='NA' or
            (Order_Volume_Higher_Limit__c=null and Order_Volume_Lower_Limit__c=null) or
            (Order_Volume_Higher_Limit__c=null and Order_Volume_Lower_Limit__c<=:delivery_Seasonal.Total__c) or
            (Order_Volume_Lower_Limit__c<=:delivery_Seasonal.Total__c and Order_Volume_Higher_Limit__c>=:delivery_Seasonal.Total__c)
        )];
       
        String newPricingModel=null;
        if(!pricingModels.isEmpty()){
            newPricingModel=pricingModels[0].id;
        }else{
            delivery_Seasonal.addError('No Pricing Model found. Please put correct Product detail & Order Volume.');
        }
        if(delivery_Seasonal.price__c!=newPricingModel){
            delivery_Seasonal.price__c=newPricingModel;
            //update delivery_Seasonal;
        }
       
    }
}