+ Start a Discussion
Santhosh ParakunnathSanthosh Parakunnath 

Wrapper class with field to be updated.

HI

I have a similar requirement as below..

https://developer.salesforce.com/page/Wrapper_Class

I also have to have a dropdown checkbox on the VF page to capture values from Picklist to insert into the new record. Everything seems to work fine in the below attached code except that when the records are inserted it inserts the same value for all the records it is inserting rather than getting the value for the inserting record from the picklist on the VF.

public OpportunityContactInsertController2 (){
        optyQueryString = 'SELECT FirstName,LastName,CreatedDate,AccountId from Contact where AccountId=:acId and Id not in (select Contact_Name__c from opportunity_Contact__c where Opportunity__c=:prId)';
        contList = new List<cContact>();
        optySetController = new ApexPages.Standardsetcontroller(Database.getQueryLocator(optyQueryString+' limit 10'));
        searchRecord = new Contact();
        helperRecord = new Reassign_Helper__c();
        isSuccess=false;
        searchPerformed = false;
        tooManyResults= false;
    }
    public void refreshContactListBySearch(){
        contList.clear();
        isSuccess = false;
        String userFilterQuery='';
        if (searchRecord.AccountId<>null)  userFilterQuery += ' and AccountId= \''+searchRecord.AccountId+'\'';             
        String optyQueryString =optyQueryString +  userFilterQuery ;
        optyQueryString += ' order by Name limit 2';
        
        List<Sobject> sortedResults= new List<SObject>();
        try{
            sortedResults = Database.query(optyQueryString);
            searchPerformed = true;
        } catch (Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
        }
        for (SObject foundObject:sortedResults){
            Contact opty = (Contact)foundObject;
            contList.add(new cContact(opty));      
        }
    }
    public void Assign(){     
        list<Opportunity_Contact__c> ContactInsertList=new list<Opportunity_Contact__c>();
        for (cContact opty:contList)
            if (opty.selected)
                ContactInsertList.add(new Opportunity_Contact__c(Contact_Name__c=opty.oContact.id, Opportunity__c=prId,Role__c='role', Loyalty__c=loyalty,Rank__c='1' ));
            try {
                Insert ContactInsertList;
            }
            catch (Exception e) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
            } 
			
            integer n=contList.size();
            for (integer i=n-1;i>=0;i--){
                if (contList[i].selected) contList.remove(i);
            }  

            if (ContactInsertList.size()>0) isSuccess = true;
        } 
    public class cContact{
        public Contact oContact {get;set;}
        public Boolean selected {get;set;}
        public cContact(Contact oContact){
            this.oContact = oContact;
            selected=false;
            }

    }
    public list<SelectOption> getItems (){
         list<SelectOption> options = new list<SelectOption>();
         options.add(new SelectOption('','Select one'));
         options.add(new SelectOption('Entrenched Competitior','Entrenched Competitior'));
         options.add(new SelectOption('Switchable Competitior','Switchable Competitior'));
         return options;
     } 

        public string getSelectedItem(){
          return loyalty;
        }   

    	public void setSelectedItem(String loyalty){
         this.loyalty = loyalty;
     	}    
}


Best Answer chosen by Santhosh Parakunnath
Avidev9Avidev9
I think the problem is in this line

ContactInsertList.add(new Opportunity_Contact__c(Contact_Name__c=opty.oContact.id, Opportunity__c=prId,Role__c='role', Loyalty__c=loyalty,Rank__c='1' ));

You are setting the same value for loyalty for every other record.

You probably need to set the loyalty value in the wrapper itself, instead of setting value to a single variable.


All Answers

Avidev9Avidev9
I think the problem is in this line

ContactInsertList.add(new Opportunity_Contact__c(Contact_Name__c=opty.oContact.id, Opportunity__c=prId,Role__c='role', Loyalty__c=loyalty,Rank__c='1' ));

You are setting the same value for loyalty for every other record.

You probably need to set the loyalty value in the wrapper itself, instead of setting value to a single variable.


This was selected as the best answer
Santhosh ParakunnathSanthosh Parakunnath
Thanks Avi. I have tried this and dont seem to get it. Could you help by updating the Wrapper class. Appreciate the help.