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
jadenjaden 

Problem getting value back from from commandlink

Hi,

 

I thought I had this working but I found I do not.  I am trying to return the ID of the record selected in a block table I have on a VF page.   As the code is right now, what I get back is the ID of the first record in my blocktable.  I have 3 records returned to my blocktable and when I select the first or third record I get the same id returned in the Assign routine.

 

Here is the page code:

<apex:page controller="TeleReassign">
<apex:pageMessages />
<apex:form >
    <apex:pageBlock title="ThomasNet - Telemarketers Re-Assign of New Accounts">
        <apex:pageBlockTable value="{!users}" var="pitem" styleClass="data" rowClasses="rowOdd,rowEven">

        <apex:column headerValue="Sel"> 
            <apex:commandLink action="{!assign}" value="Sel"> </apex:commandLink>  
                        <apex:param name="tobeassignid" value="{!pitem.userdata.id}" assignTo="{!AssignID}" />  
                        <input type="hidden" name="tobeassignid" value="{!pitem.userdata.id}"/>
        </apex:column> 
        <apex:column rendered="false" headerValue="id"> <apex:outputText value="{!pitem.userdata.Id}" /> </apex:column>         
        <apex:column headerValue="Name"> <apex:outputText value="{!pitem.userdata.Name}" /> </apex:column> 
        <apex:column headerValue="Email"> <apex:outputText value="{!pitem.userdata.email}" /> </apex:column>
        <apex:column headerValue="Phone"> <apex:outputText value="{!pitem.userdata.phone}" /> </apex:column>
        <apex:column headerValue="Territory"> <apex:outputText value="{!pitem.userdata.Territory__c}" /> </apex:column> 
        </apex:pageBlockTable> 
    </apex:pageBlock>
</apex:form> 
</apex:page>

 

Here is the class code

 

public class TeleReassign {
    public string Acctid;
    public integer AccountOwn;
    public string UserTerr; 
    private String retURL = '/home/home.jsp';

    
   public String AssignID {get; set; }
   public String SelectedID {get; set; }

public TeleReassign() {

     if(ApexPages.currentPage().getParameters().containsKey('id')) {
       Acctid = ApexPages.currentPage().getParameters().get('id');
       retURL = '/' + Acctid;
       system.debug('The acct id ' + Acctid);
       
       try {
             AccountOwn = [SELECT Count() from Account where ownerid = :UserInfo.getUserId()];
             system.debug('the account owner count ' + AccountOwn ); 
             if(AccountOwn == 0) 
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'To Re-Assign an Account you must own it.'));
                 return;  
           }
        catch (Exception ex) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'To Re-Assign an Account you must own it.'));
            return;
        } 
    }
}    
    public Territory_Limit__c tl {get; set;}
    public User us {get; set;}

public PageReference assign() {

    integer lengthOwned; 
    Integer numberOfAccounts;
    SelectedID = ApexPages.currentPage().getParameters().get('tobeassignid');

    system.debug(' the assignid is ' + SelectedID );
   try {
         tl = [Select Id, Account_Limit__c, Number_of_Days_Owned__c from Territory_Limit__c where territory__c = :UserTerr ];
         system.debug('the limit is ' + tl.Account_Limit__c); 
       }
    catch (Exception ex) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The current user has no Territory limit record.'));
        return null;
    } 
 
    try {
        //numberOfAccounts = [Select count() from Account where OwnerId = :UserInfo.getUserId() and Do_Not_Count_towards_Limits__c != true and Type = 'Prospect'];
        //Get Reps number of accounts owned  
        numberOfAccounts = [Select count() from Account where OwnerId = :SelectedID  and Do_Not_Count_towards_Limits__c != true and Type = 'Prospect'];
         system.debug('The number of accounts is' + numberOfAccounts );
        lengthOwned = tl.Number_of_Days_Owned__c.Intvalue();       
        }
    catch (Exception ex) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Problem getting users ownership Quota.'));
            return null;
    }  
     
    Double RepAcctLimit = 0; 
    if(tl.Account_Limit__c <> null)
        RepAcctLimit = tl.Account_Limit__c; 

      if(numberOfAccounts <= RepAcctLimit + 10 ) { 
        system.debug('just before user query');
        User us2 = [Select Id from User where Id = :SelectedID ];
        system.debug('just before acct query');

        Account a = [Select Id, OwnerId from Account where Id = :Acctid];
        system.debug('after acct query');
        //NOTE: Update to owner done seperately from revert Date so it does not fire 
        //the Opportunity creation functinality  JJB Blackiron 8-2011
        a.OwnerId = us2.Id; 
        update a;
        //DO Not combine with above JJB Blackiron 8-2011
        a.Admin_Open_Revert_Date__c=Date.today().addDays(lengthOwned); 
        update a;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'The account has been re-assigned.'));
        return null;
    }
    Else {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This Rep is at or above their limit.'));
        return null;
    }
} 
// Field to hold and list Userlist and class to hold data DisplayReps
public DisplayReps[] userlist {get; set;}

    public class DisplayReps {
    public User userdata {get; set; }
    public DisplayReps(User userrec) {
    this.userdata = userrec;
    }
}
//Method getusers to initialize the userlist
public DisplayReps[] getUsers() {

    if(AccountOwn >= 1) {
          
        if (userlist == null) {
            Territory_Limit__c tl;
            User u;
            //Added try catch code with an error message so accounts with no territory (our account) don't show an soql error. cbarisic
            try {
                system.debug('the user id is ' + UserInfo.getUserId());
                u = [Select Id, Territory__c from User where Id = :UserInfo.getUserId()];
                system.debug('the terr is ' + u.Territory__c);
                UserTerr = u.Territory__c;
    
            }
            catch (Exception ex) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The current user has no territory.'));
                return null;
            }
             system.debug('the territory is ' + u.Territory__c);
            userlist = new DisplayReps[]{};
            for (User userrec: 
                [SELECT id, Name, email, phone, Territory__c, Ownership_quota__c 
                FROM User 
                WHERE Territory__c = :u.Territory__c and IsActive = true and Is_Telemarket_Rep__c = false])  {
                userlist.add(new DisplayReps(userrec));
            }
        }
    }
    return userlist; 
    }
}

 

Any help greatly appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
jadenjaden

This was resolved by using a hidden field and setting the value and then getting that value

 

Thank you