+ Start a Discussion

test class for cross object trigger



I need some assitance creating a test class for the trigger below.  I am new to apex coding so I appreciate any help. 




trigger OpportunityStageProposalRequested on Task (after insert) {
 /* Updates the Opportunity Stage to Proposal Requested when Task is
 created with Type = Proposal Request */
 List<ID> taskIds = new List<ID>();
 for (Task t: Trigger.new){
  if(t.type =='Proposal Request'){
 List<Opportunity> taskList = [SELECT ID, StageName 
         FROM Opportunity
         WHERE ID in:taskIds];
 for (integer i=0 ; i < taskList.size(); i++){
  taskList[i].StageName='Proposal Requested';
 update taskList;


Best Answer chosen by Admin (Salesforce Developers) 

Hi ,


Step1:  create a task record with type = Proposal Request


Task testTask  = new Task();

testTask.Name =' test';

testTask.type = 'Proposal Request';

insert testTask;


Step2: Create Opportunity


Opportunity oppty = new Opportunity();

oppty.name= 'testOppty';

oppty.name.StageName = 'Proposal Requested';

oppty.task=testTask.Id //take the task field

insert oppty;


Hope this help you.