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
Vincent van Drunen LittelVincent van Drunen Littel 

Add function not working in code *urgent*

Hi, ive been working around this script and its almost as perfect as it can get for me, just the "most important" function is not working
the button "add target" 
Error: Compile Error: Variable does not exist: selectedlstAccounts at line 27 column 40
Can someone give me a hand fixing this one??

Controler
public with sharing class CustomPaginationController 
{

    public Contact acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController ()
    {
       acc = new Contact();
       lstAccount = new List<Contact>();
    }
    public List<Contact> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Contact>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    
    
    //added by me to test
     public PageReference addTarget() {
        //system.debug('sssssssss');
        List<Contact> lstAccountsIds = selectedlstAccounts.split(',');
        List<Target__c> lstAccount = new List<Target__c>();
        //system.debug('rrrrrrr');
        for (string s : lstAccountsIds )
        {
      //system.debug('lstAccountsIds: ' + lstAccountsIds);
            system.debug('s: ' + s);
            system.debug('Name: ' + accountMap.get(s).Name);
            Target__c newtarget = new Target__c (Target__c = lstAccount, Name = lstAccountsMap.get(s).Name); 
            Target.add(newtarget);
        }
        //system.debug('yyyyyyy');
        if (Target.size() > 0)
        {
            //system.debug('ppppppp');
            insert lstAccount;
        }
        string strPageRef = '"javascript:window.close();"';
        return new PageReference(strPageRef);
    }   
    
    
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Account.Name != null && (acc.Account.Name).trim() !='' )
        {
            strFilter  = strFilter  +  ' where Name Like \''+acc.Account.Name+'%\'' ;
           
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, Account.Name, Cargo__c from Contact '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(10);
        }
        else
        {
        }
       return null;
    }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}
VFpage
<apex:page controller="CustomPaginationController" sidebar="false">

//I added this
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
<script type="text/javascript">
    $ = jQuery.noConflict();
    function Select1stAccounts(chk, contactId, Account.Name)
        {
            alert( '1' );
            var lstAccounts = $('input[id$="lstAccounts"]');
            var selectedlstAccounts = lstAccounts.val();
            
            if (chk.checked == true)
            {
                alert( '2A' );
                if (selectedlstAccounts == '')
                {
                    alert( '2A1' );
                    selectedlstAccounts = lstAccounts;
                } 
                else
                {
                    alert( '2A2' );
                    selectedlstAccounts = selectedlstAccounts + ',' + lstAccounts;
                }
            }
            else 
            {
                alert( '2B' );
                var accountIndex = selectedlstAccounts.indexOf(contactId);
                var accountToRemove = '';

                if (selectedlstAccounts.length == lstAccounts.length) {
                    accountToRemove = lstAccounts;
                }
                else {
                    if (accountIndex == 0){
                        accountToRemove = lstAccounts+ ',';
                    }
                    else{
                        accountToRemove = ',' + lstAccounts;
                    }
                }
            
                selectedlstAccounts = selectedlstAccounts.replace(accountToRemove, '');
            }
            alert( 'B' );
            lstAccounts.val(selectedlstAccounts);
            alert (lstAccounts.val());
    }
</script>
//Orignal Code
    <apex:form >
        <apex:inputHidden value="{!selectedlstAccounts}" id="lstAccountsIds"/>
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!acc.Name}" label="Name"/> 
                
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock id="resultId" rendered="{!if(lstAccount != null && lstAccount.size > 0, true,false )}">
            <apex:pageBlockButtons >
                <div style="text-align:right"> 
                  Total Records Found: {!Con.resultSize}  
      <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
      <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
      <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
      <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
      <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>           
      <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
      <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
      <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> 
      <img src="/s.gif" title="Last Page" alt="Last Page" class="last"/>         
                </div>
            </apex:pageBlockButtons>                
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!lstAccount}" var="acc" >
                <apex:column headerValue="Select" width="100px;">
                    <apex:inputCheckbox onclick="javascript:SelectAccounts(this, '{!acc.Id}');"/>
                    </apex:column>
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Account.Name}"/>
                    <apex:column value="{!acc.Cargo__c}"/>
                </apex:PageblockTable>
                <apex:commandButton value="Add Target" action="{!addTargets}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


 
Best Answer chosen by Vincent van Drunen Littel
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
public class TargetControllerNew
{
     public Account acc {get;set;}
     public String strName {get;set;}
     public List<wrapContact> wrapContactList {get; set;}    
     public TargetControllerNew() // CHANGE THIS (SelectConcorrenteController TO ConcorrenteController)
    {
  wrapContactList = new List<wrapContact>();
  
        String parentAccountId = ApexPages.currentPage().getParameters().get('accId');
  
  if(parentAccountId != null)
  {
   acc = [select Id, Name from Account where id = :parentAccountId ];
        }
    }
    public PageReference addTarget() 
 {
        List<Target__c> Targets= new List<Target__c>();
        
        for (wrapContact selCont : wrapContactList)
        {
   if(selCont.selected == true)
   {
    Target__c Target= new Target__c(Target__c= selCont.cont.id, Name = selCont.cont.Name , Target_del__c = acc.id); 
    Targets.add(target);
   } 
        }
        if (Targets.size() > 0)
        {
            insert Targets;
        }
        string strPageRef = '"javascript:window.close();"';
        return new PageReference(strPageRef);
    }

 public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(strName != null && (strName).trim() !='' )
        {
            query= ' Select name ,id, Account.Name, Cargo__c from Contact where Name Like \''+strName+'%\'' ;
        }
        else
        {
            query = 'Select name ,id, Account.Name, Cargo__c from Contact limit 1000';
        }
    System.debug('Query ---->'+ query );

  System.debug('strName---->'+ strName);
  List<Contact> lstContct =  Database.query(query) ; 
  for(Contact cont : lstContct)
  {
   wrapContactList.add(new wrapContact(cont));
  }
  
  return null;
    }
   
 public class wrapContact 
 {
        public Contact cont {get; set;}
        public Boolean selected {get; set;}
 
        public wrapContact(Contact c) 
  {
            cont = c;
            selected = false;
        }
    }   
}
 
<apex:page controller="TargetControllerNew" sidebar="false">

    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!strName}" label="Company"/> 
            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="resultId" rendered="{!if(wrapContactList != null && wrapContactList.size > 0, true,false )}">
        
                <apex:pageBlockTable value="{!wrapContactList}" var="contWrap" id="table" title="All Contacts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!contWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!contWrap.cont.name}" />
                    <apex:column value="{!contWrap.cont.Account.Name}" />
                    <apex:column value="{!contWrap.cont.Cargo__c}" />
                </apex:pageBlockTable>
                
                <apex:commandButton value="Add Target" action="{!addTarget}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks
Amit Chaudhary

All Answers

KaranrajKaranraj
In your controller class you haven't declared the variable with the Name selectedlstAccounts. Declare the variable with the correct data type.
Vincent van Drunen LittelVincent van Drunen Littel
Hi Karanraj, 
Sorry but I'm new to apex, and not sure what you mean, would you be able to show me how its done?
Shrikant BagalShrikant Bagal
Hello just add 
----------------------------------------------------------------------------------
public String selectedlstAccounts { set; get; }
---------------------------------------------------------------------------------------------
in your Controller.

 
Shrikant BagalShrikant Bagal
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks! 
Vincent van Drunen LittelVincent van Drunen Littel
unfortunately it did not Shirkant... im still working on the resolution, once resolved i will most certainly post it here.

Thank you 
Shrikant BagalShrikant Bagal
Use following code as VF:
 
<apex:page controller="CustomPaginationController" sidebar="false">

//I added this
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
<script type="text/javascript">
    $ = jQuery.noConflict();
    function Select1stAccounts(chk, contactId, Account.Name)
        {
            alert( '1' );
            var lstAccounts = $('input[id$="lstAccountsIds"]');
            var selectedlstAccounts = lstAccounts.val();
            
            if (chk.checked == true)
            {
                alert( '2A' );
                if (selectedlstAccounts == '')
                {
                    alert( '2A1' );
                    selectedlstAccounts = lstAccounts;
                } 
                else
                {
                    alert( '2A2' );
                    selectedlstAccounts = selectedlstAccounts + ',' + lstAccounts;
                }
            }
            else 
            {
                alert( '2B' );
                var accountIndex = selectedlstAccounts.indexOf(contactId);
                var accountToRemove = '';

                if (selectedlstAccounts.length == lstAccounts.length) {
                    accountToRemove = lstAccounts;
                }
                else {
                    if (accountIndex == 0){
                        accountToRemove = lstAccounts+ ',';
                    }
                    else{
                        accountToRemove = ',' + lstAccounts;
                    }
                }
            
                selectedlstAccounts = selectedlstAccounts.replace(accountToRemove, '');
            }
            alert( 'B' );
            lstAccounts.val(selectedlstAccounts);
            alert (lstAccounts.val());
    }
</script>
//Orignal Code
    <apex:form >
        <apex:inputHidden value="{!selectedlstAccounts}" id="lstAccountsIds"/>
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!acc.Name}" label="Name"/> 
                
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock id="resultId" rendered="{!if(lstAccount != null && lstAccount.size > 0, true,false )}">
            <apex:pageBlockButtons >
                <div style="text-align:right"> 
                  Total Records Found: {!Con.resultSize}  
      <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
      <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
      <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
      <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
      <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>           
      <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
      <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
      <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/> 
      <img src="/s.gif" title="Last Page" alt="Last Page" class="last"/>         
                </div>
            </apex:pageBlockButtons>                
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!lstAccount}" var="acc" >
                <apex:column headerValue="Select" width="100px;">
                    <apex:inputCheckbox onclick="javascript:SelectAccounts(this, '{!acc.Id}');"/>
                    </apex:column>
                    <apex:column value="{!acc.Name}"/>
                    <apex:column value="{!acc.Account.Name}"/>
                    <apex:column value="{!acc.Cargo__c}"/>
                </apex:PageblockTable>
                <apex:commandButton value="Add Target" action="{!addTargets}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Shrikant BagalShrikant Bagal
In your controller you have to add 


public String selectedlstAccounts { set; get; }
 as Property.


 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
public class TargetControllerNew
{
     public Account acc {get;set;}
     public String strName {get;set;}
     public List<wrapContact> wrapContactList {get; set;}    
     public TargetControllerNew() // CHANGE THIS (SelectConcorrenteController TO ConcorrenteController)
    {
  wrapContactList = new List<wrapContact>();
  
        String parentAccountId = ApexPages.currentPage().getParameters().get('accId');
  
  if(parentAccountId != null)
  {
   acc = [select Id, Name from Account where id = :parentAccountId ];
        }
    }
    public PageReference addTarget() 
 {
        List<Target__c> Targets= new List<Target__c>();
        
        for (wrapContact selCont : wrapContactList)
        {
   if(selCont.selected == true)
   {
    Target__c Target= new Target__c(Target__c= selCont.cont.id, Name = selCont.cont.Name , Target_del__c = acc.id); 
    Targets.add(target);
   } 
        }
        if (Targets.size() > 0)
        {
            insert Targets;
        }
        string strPageRef = '"javascript:window.close();"';
        return new PageReference(strPageRef);
    }

 public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(strName != null && (strName).trim() !='' )
        {
            query= ' Select name ,id, Account.Name, Cargo__c from Contact where Name Like \''+strName+'%\'' ;
        }
        else
        {
            query = 'Select name ,id, Account.Name, Cargo__c from Contact limit 1000';
        }
    System.debug('Query ---->'+ query );

  System.debug('strName---->'+ strName);
  List<Contact> lstContct =  Database.query(query) ; 
  for(Contact cont : lstContct)
  {
   wrapContactList.add(new wrapContact(cont));
  }
  
  return null;
    }
   
 public class wrapContact 
 {
        public Contact cont {get; set;}
        public Boolean selected {get; set;}
 
        public wrapContact(Contact c) 
  {
            cont = c;
            selected = false;
        }
    }   
}
 
<apex:page controller="TargetControllerNew" sidebar="false">

    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!Search}" value="Search" />
            </apex:pageBlockButtons>
            
            <apex:pageblockSection >
                <apex:inputText value="{!strName}" label="Company"/> 
            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="resultId" rendered="{!if(wrapContactList != null && wrapContactList.size > 0, true,false )}">
        
                <apex:pageBlockTable value="{!wrapContactList}" var="contWrap" id="table" title="All Contacts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!contWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!contWrap.cont.name}" />
                    <apex:column value="{!contWrap.cont.Account.Name}" />
                    <apex:column value="{!contWrap.cont.Cargo__c}" />
                </apex:pageBlockTable>
                
                <apex:commandButton value="Add Target" action="{!addTarget}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks
Amit Chaudhary
This was selected as the best answer