+ Start a Discussion
kirsankirsan 

Assigning leads to user by using Javaremoating

Hello,
I am assigning a lead to one user by using radio button, for this I am using javascript remote functionality.
I am passing userId to static variable in controller. Whenever I clicked on Assign button the static variable is getting null.
Below is my code, any one can help me to fix this issue.

<apex:page controller="LeadDealerAssignmentProcess">
    <script type="text/javascript">
        function fnbuttoncheck(id)  
            {  
                var radiobutton = document.getElementById(id);
                 if  (radiobutton.checked == true )       
                    {  
                       alert('------->'+id);
                         Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.LeadDealerAssignmentProcess.changeIdvalue}', id,
                                                                        function(result, event){},{escape: true});
                        
                     }  
            }  

    </script>
    <apex:form >
       
     <apex:pageblock >
         
        <apex:pageBlockTable value="{!DealerAssignment}" var="dealer" id="myTable">
             <apex:column ><input type="radio" name="selectRadio" onclick="fnbuttoncheck(this.id)" checked="{!dealer.isSelected }" id="{!dealer.usr.id}" /></apex:column>
            <apex:column value="{!dealer.usr.Name}"/>
            <apex:column value="{!dealer.usr.Title}"/>
            <apex:column value="{!dealer.usr.Dealer_Title__c}"/>
            <apex:column value="{!dealer.usr.Dealer_Outlet_Name__c}"/>
            <apex:column value="{!dealer.usr.CompanyName}"/>
           </apex:pageBlockTable>
           <apex:pageBlockButtons >
           <apex:commandButton value="Assign" action="{!AssignLeadtoDealer}"/>
           <apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/>
           </apex:pageBlockButtons>
           </apex:pageblock>
        <!--<input type="text" name="userid" value="{!userStr}"/>-->
       
   </apex:form>
</apex:page>

public class LeadDealerAssignmentProcess{
    Public static String userStr;
    
    public list<Wrapperuser> getDealerAssignment(){
       List<Wrapperuser> listwrapperuser= new list<Wrapperuser>();
        Lead__c leadslist = [select id, Name, Assigned_Dealer__c, Assigned_Dealer__r.Dealer_Sales_Manager__c from Lead__c WHERE id = : ApexPages.currentPage().getParameters().get('id') LIMIT 1];
        List<User> UserDealers = [select Id, Name, Title, Dealer_Title__c, Dealer_Outlet_Name__c, CompanyName, Email from User LIMIT 10];//leadslist.Assigned_Dealer__r.Dealer_Sales_Manager__c LIMIT 1];
 
  for(user usr: UserDealers){
  Wrapperuser wuser= new Wrapperuser ();
  wuser.usr=usr;
  wuser.isSelected = true;
  listwrapperuser.add(wuser);
  }
        return listwrapperuser;
        
        
    }
    public pageReference doCancel(){
        return null;        
    }
     
    @RemoteAction
    public static void changeIdvalue(String struserId)
    {
      userStr = struserId;
       System.debug('Id from remote action-->'+struserId);
       System.debug('UserID--->'+userStr );
        //userStr = struserId;
        //Pagereference pageRef = Apexpages.currentPage();
        //pageref.getParameters().put('UserId',struserId);
       // return userStr ;
       
        
    }
    public PageReference AssignLeadtoDealer(){
     
         string leadid= apexpages.currentpage().getparameters().get('id');
         
         Lead__c lead = new Lead__c(id=leadid, ownerid=userStr);
         System.debug('Id >>>>>>>>' + userStr+ ' Lead Id >>>>' + leadid);
         update lead;
         
         System.debug(' User ID >>>>>>>>> ' + userStr);
        return null;        
    }
    
    public class Wrapperuser{
    
    public boolean isSelected {get;set;}
    public user usr {get;set;}
    
    public Wrapperuser()
    {
        isSelected = false;
        usr=new user();
    }
    }
    
}
Alain CabonAlain Cabon
Hello kirsan,

Visualforce Developer Guide: Version 38.0, Winter ’17: page 311: Using JavaScript in Visualforce Pages

var accountName = document.getElementById('acctSearch').value;
<input id="acctSearch" type="text"/>

therefore:

onclick="fnbuttoncheck(this.id)"   replace by:   onclick="fnbuttoncheck('{!dealer.usr.id}')"

this.is should not be resolved (I don't test your code)

Regards
Alain