+ Start a Discussion

Account Updation with the help of opportunity id

HI Everyone,


public class TestPage
     public String id{get; set;}
      Boolean userIdNotFound = false;
    List<opportunitywrapper> opportunityList = new List<opportunitywrapper>();//for opportunity
        List<opportunity> selectedopportunity = new List<opportunity>();//for opportunity
        public List<opportunitywrapper> getopportunity()
            for(opportunity o : [select Id, Name from Opportunity Where id =:ApexPages.currentPage().getParameters().get('id')])
            opportunityList.add(new opportunitywrapper(o));
            return opportunityList ;
         public PageReference getSelectedopp()
            for(opportunitywrapper oppwrapper: opportunityList)
            if(oppwrapper.selected == true)
            return null;
        public List<opportunity> GetSelectedopportunity()
            return selectedopportunity;//Update Account here
            return null;
        public class opportunitywrapper
        public opportunity opp{get; set;}
            public Boolean selected {get; set;}
            public opportunitywrapper(opportunity o)
                opp= o;
            selected = false;


The above code is working fine when i give any opportunity id on the url to get the opportunit name.

My problem is that when i get the records in list i dnt want to show them on the page and update the account associated with that opportunity.


Please help on this as i am new to the salesforce.

Thanxs in advance




Idd BaksheeshIdd Baksheesh

Hi can you be a bit clear? You want to update the account record? Or you want to update the Account reference in opportunity?


Also may I suggest don't use DML in getters, you can update or use DML in Action Functions.




My problem is that i want to update the account which is associated with that opportunity.


I am able to get the records from the list used for the opportunity part.





If you wish to fetch the account record in the list above, you will have to fetch the Account object along with the query.


To do so, you can use Account__r.fieldname in the object.


Finally if you wish to update or perform any DML operation on the account, you can directly write the DML statement as


insert Opportunity.Account__r;


It works as Account__r stores the reference of the account, while account__c in the opportunity object stores the ID only. Also the opportunity should be single opportunity, if you have it inside a list, suggest you put account in a separate list of accounts and update them (this to prevent DML inside for loop)





And finally

update ListOfAccounts.


Hope it helps,