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
LudivineLudivine 

How to get the id of my new created opportunity?

I want in my test trigger to update an opportunity.
How can  I get the Opportunity.id that I have just inserted instead of taking the opportunity.name?
@isTest
private class TestFireApproval_Trigger {

    private static testMethod void triggersTestFireApprovalAFEA(){
        
        Profile p = [select id from profile where name='Standard User'];
        User u = new User(alias = 'standt', email='standarduser4@testorg.com',emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='TESTUSER23@testorg.com');
        
        System.runAs(u) {  


//Create a new Opportunity for AFEA

        Opportunity Op = new Opportunity();

 // Fill in mandatory fields       
        Op.stageName ='Idea Action';
        Op.CurrencyIsoCode ='EUR';
        Op.Accountid ='00120000003XRRY';//'Amcor Ltd';
        Op.Business_group__c = 'Amcor Flexibles Europe & Americas';
        Op.Business_Unit__c ='FAME and extrusion';
        Op.Division__c ='AF Cambe';
        Op.Manufacturing_plant__c ='Cambe';
        Op.Step_change__c ='N/A';
        Op.industry__c ='Capsules';
        Op.Segment__c='Capsules';
        Op.Sub_Segment__c ='Capsules';     
        Op.Type = 'Volume Change';
        Op.Detailed_Action_Type__c = 'New Amcor Product Innovation';
        Op.Action_Risk__c ='Low';
        Op.budgeted_Action__c ='No';
        Op.Value_Plus_Site_Review__c = 'No';
        Op.ongoing__c ='Yes';
        Op.Customer_Value__c ='No';
        Op.CloseDate = system.today();        
        Op.Unadj_Expected_Annual_Impact__c = 30000;
        Op.Expected_Implementation_Date__c = system.today();
        Op.Expected_Impact_Date__c = system.today();
        Op.Expected_PM_of_New_Volume__c =30;
        Op.Expected_Units_Annually__c =100000;
        Op.Unit_of_Measure__c ='MSI';
        Op.Expected_Sales_Revenue__c =500000;
        Op.name ='TestFireApproval1'+ system.now();
        Op.Actual_Implementation_Date__c = system.today();
            
        try{
            insert Op; 

            } 
        catch (System.DmlException e){
            System.debug('we caught a dml exception: ' + e.getDmlMessage(0));  
            
           // String OpId1;
          //  OpId1 = Op.Id; 
        } 


//--> Select the record        
        Opportunity B1 = [Select Id,
             stageName,
             Expected_Impact_Date__c,
             Expected_Units_Annually__c,
             Expected_Sales_Revenue__c,
             Expected_PM_of_New_Volume__c,
             Actual_Implementation_Date__c,
             Actual_Impact_Date__c,
             Actual_Units_Annually__c,
             Unadjusted_Actual_Annual_Impact__c,
             Actual_Sales_Revenue__c 
                           from Opportunity 
                            where name ='TestFireApproval1'];
                            //--->Where Id =  OpId1 ;
            // ----->How to find the Id of Opportunity above?? 

            update B1;

Thanks.
kiranmutturukiranmutturu
You simply query the opportuntiy object with out any where condition because  you will get the records in the test context only as your are using annotation @isTest...
Hermes Yan 11Hermes Yan 11
line 72 can simply be changed to
where Id = Op.id];

When you insert the Op on line 48, the reference to the opportunity will have the id field populated after insert if it was succesful