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
shifali sharma 11shifali sharma 11 

opportunity list update

I am having one  list  opportunity having one record -(Opportunity:{Id=0060l000004aCkfAAE, Name=Test 12112230 Test asddwd0, StageName=New}
and I want to update the stage name of the opportunity and another field too with the same id .what can I do?
I Tried the below but it is throwing error duplicate id-
opportunity opp1 = new Opportunity();  
        opp1.MembershipID__c='1234';
        opp1.StageName='Quoted';
        opp1.Id=oppList[0].id;
        oppList1.add(opp1);
        oppList.addall(oppList1);
Steven NsubugaSteven Nsubuga
The id field is the primary key of the salesforce record, and primary keys are never reused.
opportunity opp1 = new Opportunity();  
        opp1.MembershipID__c='1234';
        opp1.StageName='Quoted';
        oppList1.add(opp1);
        oppList.addall(oppList1);
        upsert oppList;

 
Akshay_DhimanAkshay_Dhiman
Hi shifali sharma,


Try this code. I did this code in three types try the which you want to do as shown below:

Type( According to Your Requirements):
1) If you want to update the single record of Opportunity(fields) without List.
2) If you want to update the single record of Opportunity(fields) with List.
3) If you want to update multiple records of Opportunity(fields) with List.

Your mistake in the code was you override the id of opportunity. In salesforce when we insert any records than automatically salesforce gives its uniques id.

 
public class ForForum {
    //Method 1 updates opportunity with the help of Object
    public static void updateBySingleObj()
    {
        Opportunity opList = new Opportunity();
        opList = [SELECT Id, StageName , Name from Opportunity limit 1];
        system.debug('Feteched--->'+opList);
        opList.Name='Test';
        opList.MembershipID__c='1234';
        opList.StageName ='ClosedWon';
        update opList;
        system.debug('updated--> '+opList);
    }
    //Method2 updates opportunity with the help of List for single record
    public static void testByList()
    {
        List<Opportunity> opportunityToUpdate = new List<Opportunity>();
        opportunityToUpdate = [SELECT Id, StageName , Name from Opportunity limit 1];
        system.debug('Fetched Record--> '+opportunityToUpdate);
        opportunityToUpdate[0].Name='Test2';
        opportunityToUpdate[0].MembershipID__c='1234';
        opportunityToUpdate[0].StageName = 'ClosedLost';
        update opportunityToUpdate;
        system.debug('Updated Record on same record id--> '+opportunityToUpdate);
    }
    //Method3 updates opportunity with the help of List for multiple record
    public static void testByListforMultipleRecord()
    {
        List<Opportunity> opportunityToUpdate = new List<Opportunity>();
        opportunityToUpdate = [SELECT Id, StageName , Name from Opportunity limit 20];
        system.debug('Fetched Record--> '+opportunityToUpdate);
        for(Integer i=0; i<opportunityToUpdate.size();i++)
        {
            opportunityToUpdate[i].Name='Test3';
            opportunityToUpdate[i].StageName = 'ClosedLost';  
            opportunityToUpdate[i].MembershipID__c='1234';    
        }
        update opportunityToUpdate;
        system.debug('Updated Record on same record id--> '+opportunityToUpdate);
    }
}



Please Mark the best answer if this code helps you.

Thank You