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
hogehogehogehoge 

Help me! Apex Trigger test code

trigger ResultCustomerTrigger on Result__c (Before insert,Before update) {
   
   Set<Id> oppIdSet  = new Set<Id>();
   for (Result__c m : Trigger.new){
      oppIdSet.add(m.vupOpportunity__c);
   }

   List<Opportunity> OpportunityList =[SELECT Id,Vup_No__c,Account.Name FROM Opportunity WHERE Id =: oppIdSet];
   Map<Id,Opportunity> OppMap = new Map<Id,Opportunity>();
   for (Opportunity opp : OpportunityList){
      OppMap.put(opp.Id,opp);
   }

   for (Result__c m : Trigger.new){
      Opportunity opp = (Opportunity)OppMap.get(m.vupOpportunity__c);
      if ( opp != null) {
         if (m.DeliveryDestination__c == null && m.ProjectNo__c == null) {
         m.DeliveryDestination__c = opp.AccountId;
         m.ProjectNo__c = opp.Vup_No__c;
      }
       
   		}

	}
}

Please tell me how to write the test class.
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
@istest
public class ResultCustomerTrigger_Test{

static testmethod void insertResultCustomer(){
//insert mandatory fields for account 
Account acc = new account();
acc.Name = 'test';

insert acc;
//insert mandatory fields for opportunity 
opportunity opp = new opportunity();
opp.Name = 'test opp';
opp.accountid = acc.id;
opp.closedate = system.today()+5;
opp.stageName = 'Prospect';
opp.Vup_No__c = 12345;

insert opp;
//insert mandatory fields for Result__c 
Result__c  r = new Result__c ();
r.Name ='test';
r.vupOpportunity__c = opp.id;

insert r;



}

}

Let me know if any issues.

If this helps, Please mark it as best answer.

Thanks!!