You need to sign in to do that
Don't have an account?
jonathanbernddf20131.387825590468351E12
Need help - Not able to update value in Test class record
Hi folks
As a newbie to SF, can someone explain to me why nothing I do actually updates my record AND activates my trigger?
I have a trigger on Opportunity to a custom object. It creates a record based on the stage value. Trigger and Class work in sandbox.
I'm trying to get test coverage and I'm running into a roadblock.
I have managed to update a StageName value in the Test class but it didn't call the trigger. (which is supposed to update a previous custom object record and create a new one with the new StageName).
I tried putting in Test.startTest(); and Test.stopTest(); but it didn't make a difference.
I tried creating bulk records, but only succeeded in creating multiple initial inserts, but did not manage to call the update part of the trigger.
I tried a single opportunity test record, and did update the value in StageName but did not call the trigger.
I'm still trying with a single record and have the test code below. Please can someone explain to me what I clearly don't understand? Thanks
As a newbie to SF, can someone explain to me why nothing I do actually updates my record AND activates my trigger?
I have a trigger on Opportunity to a custom object. It creates a record based on the stage value. Trigger and Class work in sandbox.
I'm trying to get test coverage and I'm running into a roadblock.
I have managed to update a StageName value in the Test class but it didn't call the trigger. (which is supposed to update a previous custom object record and create a new one with the new StageName).
I tried putting in Test.startTest(); and Test.stopTest(); but it didn't make a difference.
I tried creating bulk records, but only succeeded in creating multiple initial inserts, but did not manage to call the update part of the trigger.
I tried a single opportunity test record, and did update the value in StageName but did not call the trigger.
I'm still trying with a single record and have the test code below. Please can someone explain to me what I clearly don't understand? Thanks
@isTest public class TestOppStageHistory { static testmethod void createInsOppData() { Test.startTest(); //Create an opportunity Opportunity insOpp = new Opportunity(); insOpp.Name = 'TestUpdate'; insOpp.StageName = 'Qualification/Prospecting'; insOpp.Probability = 1; insOpp.CloseDate = date.TODAY() + 10; insOpp.Type = 'New Business'; insert insOpp; //Test to see if opportunity stage history record was created List<Opportunity> insTestOpps = [Select Id, StageName FROM Opportunity WHERE Id = :insOpp.Id]; String stval1 = insTestOpps[0].Id; System.debug('Value of Id in Opportunity in record is ' + stval1); Opportunity_Stage_History__c newOppsh = [Select Id, Opportunity__c, Stage__c, Start_Date__c, Completion_Date__c FROM Opportunity_Stage_History__c WHERE Opportunity__c = :insOpp.Id]; System.assertEquals(newOppsh.Stage__c, insOpp.StageName); System.assertEquals(newOppsh.Start_Date__c, date.TODAY()); System.assertEquals(newOppsh.Completion_Date__c, NULL); System.assertEquals(newOppsh.Opportunity__c, insOpp.Id); //Do we have the right values? List<Opportunity_Stage_History__c> insertedShRecs= [SELECT Id, Stage__c, Start_Date__c, Completion_Date__c, Opportunity__c FROM Opportunity_Stage_History__c WHERE Opportunity__c = : insOpp.Id ]; System.debug('The number of recs in the list is ' + insertedShRecs.size()); String stval2 = insertedShRecs[0].Stage__c; System.debug('Value of Stage__c in record is ' + stval2); //update StageName on opportunity Opportunity updOpp = [SELECT Id, StageName, Probability FROM Opportunity WHERE Id= :insOpp.Id]; String stval3 = updOpp.Id; System.debug('Value of Id in updOpp record is ' + stval3); String stval4 = updOpp.StageName; System.debug('Value of StageName in updOpp record is '+stval4); updOpp.StageName = 'Needs Analysis'; updOpp.Probability = 25; //update.updOpp(); - can't do this. Do I need to map something? Do I need to break to two methods - how to use same record? Test.stopTest(); } }
All Answers
Could you uncomment it and use it as given below, because above syntax is wrong,
This should invoke your update trigger.
Also you need not query the object to update it you could simply update the object that you have inserted
If this does not work check if your trigger has update context declared. e.g.
Hope this helps!
Thanks,
Prem
Thanks,
Prem