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
Prema -Prema - 

Hello Friends could you please help me to update the id of Opportunity's description with opportunity id inside trigger

trigger CreateOpportunityRelatedAccount on Opportunity (before insert) 
{
    public string ids;
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert)
    {
        for(Opportunity opp : Trigger.new)
        {        oppLstToUpdate.add(opp);
                 ids=opp.id;
                 opp.Description=ids;
        }   
    }

}

 
Best Answer chosen by Prema -
Ajay K DubediAjay K Dubedi
Hi Prema,

We don't have id on before insert so we need to fire the trigger on after insert.
Please refer to the code.

trigger CreateOpportunityRelatedAccount on Opportunity (after insert) 
{
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert)
    {
        for(Opportunity opp : Trigger.new) 
        {       
            
            Opportunity opp1 =  new Opportunity();
            opp1.Id = opp.Id;
            opp1.Description=opp1.Id;
            oppLstToUpdate.add(opp1);
        }   
    }
    Database.update(oppLstToUpdate,false);
}

Thank you 
Ajay Dubedi

All Answers

Prema -Prema -
the id is not getting updated inside description whenever i am creating a new record. please help me
Pradeep SinghPradeep Singh
Hi Prema, We don't have id in before Insert as ID is generated when it is inserted/stored into the database and before insert executes before this.
So you have to use after insert here.
Refer below code:-
trigger CreateOpportunityRelatedAccount on Opportunity (after insert){
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert){
        for(Opportunity opp : [select id from Opportunity where id in: Trigger.new]){
            opp.Description = opp.id;
            oppLstToUpdate.add(opp);
            
        }
    }
    update oppLstToUpdate ;
}
Prema -Prema -
But i am getting error when i use after insert. Please refer to this screenshot. User-added image
Prema -Prema -
I want to see the id that's it. But after this i want to create an account related to this opportunity. Please help
Ajay K DubediAjay K Dubedi
Hi Prema,

We don't have id on before insert so we need to fire the trigger on after insert.
Please refer to the code.

trigger CreateOpportunityRelatedAccount on Opportunity (after insert) 
{
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert)
    {
        for(Opportunity opp : Trigger.new) 
        {       
            
            Opportunity opp1 =  new Opportunity();
            opp1.Id = opp.Id;
            opp1.Description=opp1.Id;
            oppLstToUpdate.add(opp1);
        }   
    }
    Database.update(oppLstToUpdate,false);
}

Thank you 
Ajay Dubedi
This was selected as the best answer
Prema -Prema -
Wow that works thank you so much sir.. Thanks again :)
Prema -Prema -
But may i know why did we create opportunity object ? COuld u please help me understand this logic.
 for(Opportunity opp : Trigger.new) 
        {       
            
            Opportunity opp1 =  new Opportunity();
            opp1.Id = opp.Id;
            opp1.Description=opp1.Id;
            oppLstToUpdate.add(opp1);
        }   
    }
    Database.update(oppLstToUpdate,false);
Ajay K DubediAjay K Dubedi
This is because you are in an after insert/update trigger and the records are read-only in that context as they have been written, but not committed, to the database. Unfortunately your trigger is relying on the ids of the records, which means you won't be able to use before insert as the ids won't be populated at that time (as the records haven't been written to the database at that point so while you can write to them, database generated fields aren't populated).
In this instance, you'll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies.
Prema -Prema -
Thanks..