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
vivek singh08481200707119766vivek singh08481200707119766 

Hi please help me this is a triger how we make its test class i m new in salesforce

trigger chekDocSign on dsfs__DocuSign_Recipient_Status__c (After insert, After update) {
    for(dsfs__DocuSign_Recipient_Status__c ob :trigger.new){
        if(ob.dsfs__Date_Signed__c != null){
         dsfs__DocuSign_Recipient_Status__c objDRS = [select dsfs__Parent_Status_Record__r.dsfs__Opportunity__r.id from dsfs__DocuSign_Recipient_Status__c where id =: ob.Id limit 1];
         Opportunity objOpp = [select id, StageName from Opportunity where id =: objDRS.dsfs__Parent_Status_Record__r.dsfs__Opportunity__r.id limit 1];
         objOpp.StageName = 'Closed Won - Signed';
         update objOpp;
        }else{
            if(ob.dsfs__Date_Declined__c != null){
                dsfs__DocuSign_Recipient_Status__c objDRS = [select dsfs__Parent_Status_Record__r.dsfs__Opportunity__r.id from dsfs__DocuSign_Recipient_Status__c where id =: ob.Id limit 1];
          Opportunity objOpp = [select id, StageName from Opportunity where id =: objDRS.dsfs__Parent_Status_Record__r.dsfs__Opportunity__r.id limit 1];
          objOpp.StageName = 'Closed Lost (Cancelled)';
          update objOpp;
            }          
        }       
    }
}
Best Answer chosen by vivek singh08481200707119766
ShashForceShashForce
Hi,

You can try something like this:

@isTest
public class triggerTestClass(){
	
	private void testmethod insertRecordwithDateSigned(){
		
		opportunity op = new opportunity();
		op.name = 'test opp';
		//followed by other opportunity fields which need to be populated to create a test opportunity record;
		
		test.startTest();
		
		insert op;
		
		dsfs__DocuSign_Recipient_Status__c tr = new dsfs__DocuSign_Recipient_Status__c();
		tr.name = 'test';
		tr.dsfs__Date_Signed__c = 'some date';
		tr.Opportunity = op.Id;
		//followed by other fields any to create a test record;
		
		insert tr;
		opportunity opt = [select stage from opportunity where Id=:op.Id];
		system.assert(opt.stagename=='Closed Won - Signed');
		
		test.stopTest();
		
	}
	
	private void testmethod insertRecordwithDateDeclined(){
		
		opportunity op = new opportunity();
		op.name = 'test opp';
		//followed by other opportunity fields which need to be populated to create a test opportunity record;
		
		test.startTest();
		
		insert op;
		
		dsfs__DocuSign_Recipient_Status__c tr = new dsfs__DocuSign_Recipient_Status__c();
		tr.name = 'test';
		tr.dsfs__Date_Declined__c = 'some date';
		tr.Opportunity = op.Id;
		//followed by other fields which need to be populated to create a test record;
		
		insert tr;
		opportunity opt = [select stage from opportunity where Id=:op.Id];
		system.assert(opt.stagename=='Closed Lost (Cancelled)');
		
		test.stopTest();
		
	}
	
}

I would also suggest you to go through this link for more help on test classes: https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank