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
ishchopraishchopra 

Custom button on Lead List View

I am facing a small problem with custom button and VF Page.

Scenario

We would like to replace the standard button called Change Status on lead list view to add more fields. This is done but when i click on the new button it doesnt check if we have selected any record on the list view therefore a validation is required, if atleast one record is selected. I placed a java script which works fine with one record but fails when multiple records are selected.

here is the java code
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var ids = {!GETRECORDIDS($ObjectType.Lead)};

if(ids.length > 0) {
    window.location='../apex/Lead_Status_Change?ids='+ids;
} else {
    alert("No Row Selected");
}



java.lang.IllegalArgumentException: id 00Q550000022tMl,00Q550000022tS9 must be 15 characters

Can somebody please help?

A
Best Answer chosen by ishchopra
Rajiv Penagonda 12Rajiv Penagonda 12
See if this works:
<apex:page controller="MyTestApexClass1" tabStyle="Lead">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>

    <img src="../s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><br />
    <h1 class="pageType noSecondHeader">Select New Status</h1>

    <script>
        function handleColumnPicklistChangeEvent(argSelectEle) {
            var jnc = jQuery.noConflict();

            jnc('.StatusChildList').each(function(argEle) {
                this.selectedIndex = argSelectEle.selectedIndex;
            });
        }
    </script>
    <apex:form >
        <apex:pageBlock title="Change Status">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!Save}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>
            <apex:variable value="{!1}" var="ind" />
            <apex:pageBlockTable value="{!mLeadRecs}" var="leadRec">
                <apex:column headerValue="#">
                    <apex:outputText value="{!ind}" />
                </apex:column>
                <apex:column>
                    <apex:facet name="header">
                        <apex:outputPanel>
                            <apex:outputLabel value="Status: " for="StatusMainList" />
                            <apex:selectList id="StatusMainList" size="1" multiselect="false" 
                                             onchange="handleColumnPicklistChangeEvent(this);">
                                <apex:selectOptions value="{!StatusPicklist}" />    
                            </apex:selectList>
                        </apex:outputPanel>
                    </apex:facet>
                    <apex:inputField value="{!leadRec.status}" styleClass="StatusChildList" /> 
                </apex:column>
                <apex:column headerValue="Recycled Reason">
                    <!-- <apex:inputField value="{!leadRec.Recycled_Reason__c}"/> -->
                </apex:column>
                <apex:column headerValue="Unqualified Reason">
                    <!-- <apex:inputField value="{!leadRec.Unqualified_Reason__c}"/> -->
                </apex:column>
                <apex:variable value="{!ind + 1}" var="ind" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class MyTestApexClass1 {
	public List<Lead> mLeadRecs {get;set;}

	public MyTestApexClass1() {
		String lStrLeadID = ApexPages.currentPage().getParameters().get('ids');
		mLeadRecs = [SELECT id, status /*Recycled_Reason__c, Unqualified_Reason__c*/ FROM Lead WHERE id IN:lStrLeadID.split(',')];
	}

	public PageReference save() {
		update mLeadRecs;
		return new PageReference('/00Q/o');
	}

	public PageReference cancel() {
		return new PageReference('/00Q/o');
	}

	public List<SelectOption> getStatusPicklist() {
		List<SelectOption> lOptions = new List<SelectOption>();
		
		for(Schema.PicklistEntry lFieldMeta : Lead.Status.getDescribe().getPicklistValues()) {
		    lOptions.add(new SelectOption(lFieldMeta.getValue(), lFieldMeta.getLabel()));
		}

		return lOptions;
	}
}


 

All Answers

Rajiv Penagonda 12Rajiv Penagonda 12
Your button is fine. I think it is doing what it is expected to. You need to fix the code in your Lead_Status_Change VF page/controller.

The control is reaching your Lead_Status_Change VF page from the button. Once the control is there, I am thinking, you are tying to create an insteance of ID object in your controller. That is failing because as part of your URL parameters, your have "00Q550000022tMl,00Q550000022tS9" as id.

What you need to do is, in controller split the string "00Q550000022tMl,00Q550000022tS9" using comma as delimited and try creating multiple ID objects for each of the splits, instead of one ID record for all of them

Hope this helps.
ishchopraishchopra
Hello Rajiv,

thanks for your reply, here is my VF Page Code
<apex:page standardController="Lead" recordSetVar="leads" tabStyle="Lead">
    <img src="../s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/>
    <br />
    <h1 class="pageType noSecondHeader">Select New Status</h1>
    <apex:form >
        <apex:pageBlock title="Change Status">
            <apex:pageBlockSection >
                <apex:inputField value="{!lead.status}"/>
                <apex:inputField value="{!lead.Recycled_Reason__c}"/>
                <apex:inputField value="{!lead.Unqualified_Reason__c}"/>
            </apex:pageBlockSection>
            
               
            
            <apex:pageBlockSection >
     
             <apex:pageBlockSectionItem >

<apex:commandButton action="{!Save}" 
style="left:0px;position:relative;"
value="Save" />

<apex:commandButton action="{!cancel}" 
style="left:-20px;position:relative;"
value="Cancel" />

</apex:pageBlockSectionItem>
   
            </apex:pageBlockSection>    
        
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Rajiv Penagonda 12Rajiv Penagonda 12
I see that you are using Standard Controller. This is causing the issue because Standard Controller expects only one id in the "id" URL parameter.

In this case, you can consider one of the two possible solutions:
1. Make sure you send only one id to the VF page from your button.
2. Create a custom controller to handle multiple ids which are part of the "id" URL parameter.

I suggest option 2 as option 1 may not meet your business needs.

I have updated your VF page with a new controller. See if it helps
 
<apex:page controller="MyTestApexClass1" recordSetVar="leads" tabStyle="Lead">
    <img src="../s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><br />
    <h1 class="pageType noSecondHeader">Select New Status</h1>
    <apex:form >
        <apex:pageBlock title="Change Status">
			<apex:pageBlockButtons>
				<apex:commandButton action="{!Save}" style="left:0px;position:relative;" value="Save" />
				<apex:commandButton action="{!cancel}" style="left:-20px;position:relative;" value="Cancel" />
			</apex:pageBlock>
			<apex:pageBlockTable value="{!mLeadRecs}" var="leadRec">
				<apex:column headerValue="Status">
					<apex:inputField value="{!leadRec.status}"/> 
				</apex:column>
				<apex:column headerValue="Recycled Reason">
					<apex:inputField value="{!leadRec.Recycled_Reason__c}"/> 
				</apex:column>
				<apex:column headerValue="Unqualified Reason">
					<apex:inputField value="{!leadRec.Unqualified_Reason__c}"/> 
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlock>
    </apex:form>
</apex:page>

custom controller should look like:
 
public with sharing class MyTestApexClass1 {
	public List<Lead> mLeadRecs {get;set;}

	public MyTestApexClass1() {
		String lStrLeadID = ApexPages.currentPage().getParameters().get('id');
		mLeadRecs = [SELECT id, status /*Recycled_Reason__c, Unqualified_Reason__c*/ FROM Lead WHERE id IN:lStrLeadID.split(',')];
	}

	public PageReference save() {
		update mLeadRecs;
		return new PageReference('/00Q/o');
	}

	public PageReference cancel() {
		return new PageReference('/00Q/o');
	}
}


 
ishchopraishchopra
Rajiv,

I believe i am very near to the solution but this is the error i am getting on VF Page

Error: No standard controller was specified - recordSetVar cannot be used.

 
ishchopraishchopra
Rajiv,

I removed recordSetVar="leads" and was able to save the VF page but now i am getting this erro

System.NullPointerException: Attempt to de-reference a null object
Class.MyTestApexClass1.<init>: line 6, column 1
Rajiv Penagonda 12Rajiv Penagonda 12
Try this in the line 5 of the controller:
 
String lStrLeadID = ApexPages.currentPage().getParameters().get('ids');

 
ishchopraishchopra
same error
Visualforce Error

System.NullPointerException: Attempt to de-reference a null object

Class.LeadStatusChange.<init>: line 8, column 1

 
Rajiv Penagonda 12Rajiv Penagonda 12
What do you have at Class.LeadStatusChange.<init>: line 8? Can you paste the class here?
ishchopraishchopra
public with sharing class LeadStatusChange {
    public List<Lead> mLeadRecs {get;set;}

    public LeadStatusChange () {
        String lStrLeadID = ApexPages.currentPage().getParameters().get('ids');
        
        
        mLeadRecs = [SELECT id, status /*Recycled_Reason__c, Unqualified_Reason__c*/ FROM Lead WHERE id IN:lStrLeadID.split(',')];
    
    }

    public PageReference save() {
        update mLeadRecs;
        return new PageReference('/00Q/o');
    }

    public PageReference cancel() {
        return new PageReference('/00Q/o');
    }
}

 
Rajiv Penagonda 12Rajiv Penagonda 12
In your javascript button update the following line:
 
var ids = {!GETRECORDIDS($ObjectType.Lead)};

to
 
var ids = '{!GETRECORDIDS($ObjectType.Lead)}';

Notice how the new code has the GETRECORDIDS dereferencing inside two single quotes.
ishchopraishchopra
Rajiv,

this doesnt make any difference but code is throwing this error at the time of updating status

Visualforce Error
Help for this Page
System.DmlException: Update failed. First exception on row 0 with id 00Q550000022tMlEAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter a value for Industry Verified by field.: []
Error is in expression '{!Save}' in component <apex:commandButton> in page lead_status_change: Class.LeadStatusChange.save: line 13, column 1
Class.LeadStatusChange.save: line 13, column 1
ishchopraishchopra
This happens when i am trying to update more than one records, if i update one record it works fine
Rajiv Penagonda 12Rajiv Penagonda 12
The error "System.DmlException: Update failed. First exception on row 0 with id 00Q550000022tMlEAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter a value for Industry Verified by field.: []" which you are facing is not related to the code that I provided you earlier. That code was for reference only and looks like it worked for you.

Now you are having your application specific issue and not related to technology. You have to check with your business how to proceed and if required you will have to capture the "Industry Verified By" field in your VF page for taking in the data from user.

You may mark the my earlier response as a solution to the problem you were facing. tc.
ishchopraishchopra
Rajiv,

I found that there are some validations throwing errors - i will take care of that, my last thing form you is when i land on the VF page if shows the rows for each selected lead on the list view, is there any way we can get only on row to change status of all the leads? otherwise, what's the point of selecting multiple leads?
Rajiv Penagonda 12Rajiv Penagonda 12
See if this works:
<apex:page controller="MyTestApexClass1" tabStyle="Lead">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>

    <img src="../s.gif" alt="Lead"  class="pageTitleIcon" title="Lead"/><br />
    <h1 class="pageType noSecondHeader">Select New Status</h1>

    <script>
        function handleColumnPicklistChangeEvent(argSelectEle) {
            var jnc = jQuery.noConflict();

            jnc('.StatusChildList').each(function(argEle) {
                this.selectedIndex = argSelectEle.selectedIndex;
            });
        }
    </script>
    <apex:form >
        <apex:pageBlock title="Change Status">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!Save}" value="Save" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>
            <apex:variable value="{!1}" var="ind" />
            <apex:pageBlockTable value="{!mLeadRecs}" var="leadRec">
                <apex:column headerValue="#">
                    <apex:outputText value="{!ind}" />
                </apex:column>
                <apex:column>
                    <apex:facet name="header">
                        <apex:outputPanel>
                            <apex:outputLabel value="Status: " for="StatusMainList" />
                            <apex:selectList id="StatusMainList" size="1" multiselect="false" 
                                             onchange="handleColumnPicklistChangeEvent(this);">
                                <apex:selectOptions value="{!StatusPicklist}" />    
                            </apex:selectList>
                        </apex:outputPanel>
                    </apex:facet>
                    <apex:inputField value="{!leadRec.status}" styleClass="StatusChildList" /> 
                </apex:column>
                <apex:column headerValue="Recycled Reason">
                    <!-- <apex:inputField value="{!leadRec.Recycled_Reason__c}"/> -->
                </apex:column>
                <apex:column headerValue="Unqualified Reason">
                    <!-- <apex:inputField value="{!leadRec.Unqualified_Reason__c}"/> -->
                </apex:column>
                <apex:variable value="{!ind + 1}" var="ind" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class MyTestApexClass1 {
	public List<Lead> mLeadRecs {get;set;}

	public MyTestApexClass1() {
		String lStrLeadID = ApexPages.currentPage().getParameters().get('ids');
		mLeadRecs = [SELECT id, status /*Recycled_Reason__c, Unqualified_Reason__c*/ FROM Lead WHERE id IN:lStrLeadID.split(',')];
	}

	public PageReference save() {
		update mLeadRecs;
		return new PageReference('/00Q/o');
	}

	public PageReference cancel() {
		return new PageReference('/00Q/o');
	}

	public List<SelectOption> getStatusPicklist() {
		List<SelectOption> lOptions = new List<SelectOption>();
		
		for(Schema.PicklistEntry lFieldMeta : Lead.Status.getDescribe().getPicklistValues()) {
		    lOptions.add(new SelectOption(lFieldMeta.getValue(), lFieldMeta.getLabel()));
		}

		return lOptions;
	}
}


 
This was selected as the best answer
ishchopraishchopra
Rajiv,

thanks for all the efforts and help, everything works fine excepts the redirection after clicking save, how can i redirect to the same list view?

thankyou very much once again