• Krishnan Mishra
  • NEWBIE
  • 45 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 19
    Questions
  • 16
    Replies
I am iterating  a map using repeat on a vf page. I want to send the selected folder's id in the apex. I tried to use param but it is giving null values.Following is my code:
Page:
<apex:pageBlockSection id="FolderName" collapsible="true" title="Folder">
				<apex:repeat value="{!foMapNames}" var="fokey">
					<apex:commandLink value="{!foMapNames[fokey]}" action="{!folderIn}" reRender="none">
						<apex:param value="{!fokey}" assignTo="{!foId}"/>
					</apex:commandLink>
					<br/>
				</apex:repeat>
			</apex:pageBlockSection>

here fokey is null.
 
Can anyone please explain this statement :
global class ContactBatch implements Database.Batchable<sObject>{
}

As far as I know Database is a class in apex and batchable is an interface, So why can't we write as: 
global class ContactBatch implements Batchable{
}

Also, explain the syntax for <sObject> here
I am trying to change the "status" field(custom) of the contact object by using a batch. If I update the fields in the execute method then it gets updated but if I try to update it in the finish method then my list is empty. I am not getting this!
 
global class BatchNSchedulable implements Database.Batchable<sObject>{
	global list<contact> con=new list<contact>();
	String query = 'SELECT id,name,status__c FROM contact WHERE CreatedDate= Yesterday';
	global Database.QueryLocator start(Database.BatchableContext bc){
		System.debug('In the start');
		 return (Database.getQueryLocator(query));
	}
	global void execute(Database.BatchableContext bc,list<sObject> li){
		System.debug('In the execute');
		con = (list<contact>)li;
		System.debug(con.size());	
		for(contact ct:con){
			ct.put('status__c','None');
		}
		System.debug('con in execute is '+con);
	}
	global void finish(Database.BatchableContext bc){
		System.debug('con is '+con);
		update con;
		System.debug('In the finish');
	}

}

 
id i = 0037F00000M6MCHQA3;
String fieldNames='name,id';
String objName = 'contact';
String queryStr = 'SELECT ' + fieldNames + ' FROM ' + objName + ' WHERE id IN :(\'i\')';
System.debug(queryStr);

 
i encountered the following 
Schema.sObjectType.Account.fields.getmap()

Here what is getMap() method? In which class this method is defined, and what does it do? Also if you can give me more details about this method like can this method be over loaded,what is the return type of this etc etc
I have created a visualforce component and would like to use $Action.object name.action name($Action.Contact.Del) within it but i am getting the above error. Following is my controller and component code:
Component code
<apex:component controller ="PaginationForComponent">
    <apex:attribute name= "objectName" description = "The object's name" type = "String" required = "true" assignTo="{!objName}"/>
    <apex:attribute name= "fieldName" description = "Fields to be displayed" type = "String" required ="true" assignTo="{!fieldNames}"/>
<apex:outputLink value="{!URLFOR($Action."{!objName}".Edit,contacts.con.id)}"> 
                        Edit |
                    </apex:outputlink>
                    <apex:outputLink value="{!URLFOR($Action."{!objName}".Delete,contacts.con.id)}"> 
                        Del |
                    </apex:outputlink>

Controller code:
public class PaginationForComponent {
public String objName{get;set;}
	public String fieldNames{get;set;}
}

Ps: Only relevant code is provided here​
String objName = 'Contact';
string alphaSearchConct = 'A';
String fieldNames = 'name,id';
list<sObject> ct = Database.query('SELECT '+ fieldNames +'FROM '+ objName +'WHERE lastname Like:'+alphaSearchConct +'%');

 
In th following code the action support for my input text field is not working as it should work(Line number 69-70 in vf code).I want to navigate to the page number entered by the user.Following is my controller and VF code:
Code for controller
public class ContactListViewController {
    public ContactlistViewController(){
        system.debug('constructor first called');
        RecordsPerPageslist=10;
        allContactList = new list<wrapper>();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c));
        System.debug('constructor called');
    }
    Map<id,Boolean> m = new Map<id,boolean>();  	 // To store boolean values of checkboxes corrosponding to every contact id
    list<contact> con = [SELECT Name,id,Account.name,Title,Phone,Email FROM Contact];
    public list<wrapper> allContactList;			//Wrapper class object
    
    public list<wrapper> getWrapperContacts(){  	//List of wrapper class to display in table
        return allContactList;
    }
    
    public void getSelectedListContacts(){  		// Select contacts and save them in a map
        for(wrapper wc:allContactList){
            m.put(wc.con.id,wc.isSelected);
        }
        
        System.debug('getSelectedListContacts = '+m);
        
    }
    public void getSelectedAllContacts(){			//To select all contacts in a page
        for(wrapper wc:allContactList){
            m.put(wc.con.id,wc.isSelected=true);
        }
    }
    public void next(){
        System.debug('next');
		allContactList.clear();        
        this.stdSetController.next();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
        }
    }
    public void previous(){
		allContactList.clear();        
        this.stdSetController.previous();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
    }
    }
    public void last(){
        allContactList.clear();        
        this.stdSetController.last();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
    }
    }
    public void first(){
        allContactList.clear();        
        this.stdSetController.first();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
        for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
    }
    }
    public boolean getHasNext(){
        return stdSetController.getHasNext();
    }
    public boolean getHasPrevious(){
        return stdSetController.getHasPrevious();
    }
    public list<String> alphabet{
        get{															//To display a list of alphabets on vf page 
            alphabet = new list<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Others','All'};
       		return alphabet;	 
                }
        set;
    }
    public String alphaSearchConct{get;set;}							// To get commandlink parameter for alphabet selected
    public Pagereference getalphaSearch(){								//To update contact list as per the alphabet selected by the user
        allContactList.clear();
        if (alphaSearchConct=='All'){
            con = [SELECT name,Account.name,Title,Phone,Email FROM contact];
        }
        else{
            	con = [SELECT name,Account.name,Title,Phone,Email FROM contact WHERE lastName Like:alphaSearchConct+'%'];
        }
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(con);
        stdSetController= ssc;
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c));
        system.debug('alphaSearchconct called');
        return null;
    }      
    public Integer PageNumber{
        get{                                                            //To get current page number
  		    System.debug('get of pageNumber called '+ PageNumber);
            this.PageNumber=stdSetController.getPageNumber();   
            return this.PageNumber;
        }
        set{  
            System.debug('set of pageNumber called');
           	this.pageNumber=value;         
    }
        }
    
    public PageReference NavigateByText(){
        
        System.debug('getNavigateByText '+ stdSetController.getPageNumber());
        allContactList.clear();
        this.stdSetController.setPageNumber(PageNumber);
            for(contact c:(list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c));
            //System.debug(stdSetController.getRecords());
           for(wrapper wc:allContactList){
            wc.isSelected=m.get(wc.con.id);
               System.debug(wc.isSelected);
               System.debug('pageNumber called');
               
    }
        return null;
    }
    public Integer TotalPages{                                            // Total number of pages as per user selection of Records per page
        get{
              if(stdSetController.getResultSize() <=10)
                   this.TotalPages=1;
              if(Math.Mod(stdSetController.getResultSize() ,stdSetController.getPageSize()) == 0)
                  this.TotalPages =(stdSetController.getResultSize()/stdSetController.getPageSize());
              else
                this.TotalPages = (stdSetController.getResultSize()/stdSetController.getPageSize())+1;
              //System.Debug(this.TotalPages);
                return totalpages;
        }
        set;
    }
    public Integer MaxNumberOfRecords{                                    //Maximum number of records in a query list
        get{
             return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){              //To display a drop down list on vf page  
            list<SelectOption>  options = new list<SelectOption>();
            options.add(new selectOption('10','10'));
            options.add(new selectOption('25','25'));
            options.add(new selectOption('50','50'));
            options.add(new selectOption('100','100'));
            options.add(new selectOption('200','200'));
            return options;
    }
    public Integer RecordsPerPageslist{ 
        get;
        set{                                                          //To select number of records per page
            if(value!=null){
                this.RecordsPerPagesList=value;
                System.debug('RecordsPerPageList called');
            }
        }       
    }
    public Pagereference getChangeNumberOfRecordsPerPage(){
        allContactList.clear();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
         for(wrapper wc:allContactList)
            wc.isSelected=m.get(wc.con.id);
        return null;
    } 
    public ApexPages.StandardSetController stdSetController{            //Instantiating a standard set controller
        get{
            if(stdSetController==null){
             	 stdSetController = new ApexPages.StandardSetController(con);
            }
              stdSetController.setPageSize(RecordsPerPageslist);        //Limiting Number of records to be displayed per page 
            	System.debug('stdSetController called '+ stdSetController.getPageNumber());
            return stdSetController;   
        }
        set;
    }
    public class wrapper{
      public boolean isSelected{get;set;}
      public Contact con{get;set;}
       
         wrapper(contact con){
            isSelected = false;
            this.con = con;
        }
    }
}
Code for VF page:
<apex:page controller="ContactListViewController" sidebar="false">
    <apex:form >
        <!-- For alphabetic search-->
        <div align="right">
            <apex:panelGrid >
                <apex:repeat value="{!alphabet}" var="alph">
                    <apex:commandLink value="{!alph} | " action="{!getalphaSearch}" reRender="table">
                        <apex:param name="a" value="{!alph}" assignTo="{!alphaSearchConct}"/>
                    </apex:commandLink>
                </apex:repeat>
            </apex:panelGrid>
        </div>
        <apex:PageBlock id="table">
            <apex:pageMessages />
            <apex:commandButton action="{!getSelectedAllContacts}" value="Select All" reRender="table"/>
            <apex:PageBlockTable value="{!WrapperContacts}" var="contacts" >
                <!-- To display and select Checkboxes-->
                <apex:column >
                    <apex:facet name="header">
                    	<apex:inputCheckbox value="{!contacts.isSelected}">
                        	<apex:actionSupport event="onclick" action="{!getSelectedAllContacts}" reRender="table"/>
                        </apex:inputCheckbox>	
                        </apex:facet>
                    <apex:inputCheckbox value="{!contacts.isSelected}">
                        <apex:actionSupport event="onclick" action="{!getSelectedListContacts}" reRender="table"/>
                    </apex:inputCheckbox>  
                </apex:column>
                <!-- To Edit and Delete a record -->
                <apex:column headerValue="Action">
                    <apex:outputLink value="{!URLFOR($Action.Contact.Edit,contacts.con.id)}"> 
                        Edit |
                    </apex:outputlink>
                    <apex:outputLink value="{!URLFOR($Action.Contact.Delete,contacts.con.id)}"> 
                        Del |
                    </apex:outputlink>
                </apex:column>
                <apex:column headerValue="Name">
                    <apex:outputLink value="/{!contacts.con.id}">
                        {!contacts.con.name}
                    </apex:outputLink>
                </apex:column>
                <apex:column headerValue="Account Name">
                    <apex:outputLink value="/{!contacts.con.account.id}">
                        {!contacts.con.account.name}
                    </apex:outputLink>
                </apex:column>
                <apex:column value="{!contacts.con.Title}"/>
                <apex:column value="{!contacts.con.Phone}"/>
                <apex:column value="{!contacts.con.email}"/>
                <apex:inlineEditSupport />
            </apex:PageBlockTable>
      		</apex:PageBlock> 
        <!-- below code for pagination -->
        <apex:outputPanel id="button"> 
        <div align = "center" >
            <!-- To return to first page of records-->
            <apex:commandButton action="{!first}" value="<<" title="First Page" disabled="{!!HasPrevious}" reRender="table,button"/>
            <!-- To return to Previous page of records-->
            <apex:commandButton action="{!previous}" value="Previous" disabled="{!!HasPrevious}" reRender="table,button"/>
            <!-- To return to next page of records-->
            <apex:commandButton action="{!next}" value="Next >" disabled = "{!!HasNext}" reRender="table,button"/>
            <!-- To return to last page of records-->
            <apex:commandButton action="{!last}" value=">>" title="Last Page" disabled="{!!HasNext}" reRender="table,button"/>
            <!-- InputText to display current page and to navigate to any page number, At righmost side of page-->
            <span style="float:right">
                <apex:outputLabel value="Page ">
                </apex:outputLabel>
                 <!-- To navigate to the page--> 
                <apex:InputText value="{!PageNumber}" maxLength="4" size="1"/>
                <apex:actionSupport event="onchange" action="{!NavigateByText}" reRender="table,button"/>
                <!-- The above action support is not working-->         
                <apex:outputLabel value=" of {!TotalPages}">
                </apex:outputLabel>
            </span>
            <!-- To display a list for number of records to be selected per page-->
            <span style = "float:left">
                <apex:SelectList value="{!RecordsPerPageslist}" size="1" >
                    <apex:selectOptions value="{!RecordsPerPageOptionList}">    
                    </apex:selectOptions>
                    <apex:actionSupport event="onchange" action="{!getChangeNumberOfRecordsPerPage}" reRender="table,button"/>
                </apex:SelectList>
            </span>
        </div>
    </apex:outputPanel>      
    </apex:form>
</apex:page>


 
In the followin code as soon as my page gets refreshed my record gets unchecked again,How can i avoid that?

Controller code:
public class t1 {
    list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact];
    public list<wrapper> allContactList;
    public list<contact> SelectedContact = new list<contact> ();
   
    public list<wrapper> getWrapperContacts(){  
        allContactList = new list<wrapper>();
        for(contact c: (list<contact>)stdSetController.getRecords())
                allContactList.add(new wrapper(c)); 
        return allContactList;
    }

    public void getSelectedListContacts(){
        for(wrapper wc:allContactList){
            System.debug(wc.isSelected+' '+wc.con);
            if(wc.isSelected==true){
                SelectedContact.add(wc.con);
            }
        }
        System.debug(selectedContact);
    }
    public void getSelectedAllContacts(){			//To select all contacts
        for(wrapper wc:allContactList){
            SelectedContact.add(wc.con);
        	//new t1.wrapper().isSelected=true;
        }
    }
     public ApexPages.StandardSetController stdSetController{            //Instantiating a standard set controller
        get{
            if(stdSetController==null){
             	 stdSetController = new ApexPages.StandardSetController(con);
            }
              stdSetController.setPageSize(10);        //Limiting Number of records to be displayed per page 
               return stdSetController;   
        }
        set;
    }
    
    public class wrapper{
      public boolean isSelected{
          get{
              if(this.isSelected==null)
                this.isSelected = false;
            else
                this.isSelected = true;
              return isSelected;
          }
          set;
          }
      public Contact con{get;set;}
       
         wrapper(contact con){
           //isSelected = false;
            this.con = con;
        }
    }
}

VisualForce code:
<apex:page controller="t1" >
<apex:form >
<apex:pageBlock id="table">
<apex:commandButton action="{!getSelectedAllContacts}" value="Select All" reRender="table"/>
<apex:pageBlockTable value="{!WrapperContacts}" var="c">
<apex:column >
<apex:inputCheckbox value="{!c.isSelected}">
<apex:actionSupport event="onclick" action="{!getSelectedListContacts}" reRender="table"/>
</apex:inputCheckbox>
</apex:column>
<apex:column value="{!c.con.name}"/>
<apex:column value="{!c.con.title}"/>
</apex:pageBlockTable>
    <!-- To return to first page of records-->
           <apex:commandButton action="{!stdSetController.first}" value="<<" title="First Page" disabled="{!!stdSetController.HasPrevious}" reRender="table,button"/>
           <!-- To return to Previous page of records-->
           <apex:commandButton action="{!stdSetController.previous}" value="Previous" disabled="{!!stdSetController.HasPrevious}" reRender="table,button"/>
           <!-- To return to next page of records-->
           <apex:commandButton action="{!stdSetController.next}" value="Next >" disabled = "{!!stdSetController.HasNext}" reRender="table,button"/>
           <!-- To return to last page of records-->
            <apex:commandButton action="{!stdSetController.last}" value=">>" title="Last Page" disabled="{!!stdSetController.HasNext}" reRender="table,button"/>
</apex:pageBlock>
</apex:form>
</apex:page>

Here on this visualforce page i want to select number of records from the picklist given in the left bottom of the page and then display number of records as per it but, I am not able to fetch the value from picklist to my controller.Following is my visualforce and controller code:
VisualForce code:
<apex:page controller="ContactListViewController">
<apex:form >
<apex:PageBlock >
    <apex:PageBlockTable value="{!ContactList}" var="contacts">
    <apex:column headerValue="Name">
        <apex:outputLink value="/{!contacts.id}">
        {!contacts.name}
        </apex:outputLink>
    </apex:column>
    <apex:column headerValue="Account Name">
        <apex:outputLink value="/{!contacts.account.id}">
            {!contacts.account.name}
        </apex:outputLink>
    </apex:column>
    <apex:column value="{!contacts.Title}"/>
    <apex:column value="{!contacts.Phone}"/>
    <apex:column value="{!contacts.email}"/>
    <apex:inlineEditSupport />
    </apex:PageBlockTable>
    <!-- below code for pagination -->
    <div align = "center">
           <!-- To return to first page of records-->
           <apex:commandButton action="{!stdSetController.first}" value="<<" title="First Page" disabled="{!!stdSetController.HasPrevious}"/>
           <!-- To return to Previous page of records-->
           <apex:commandButton action="{!stdSetController.previous}" value="Previous" disabled="{!!stdSetController.HasPrevious}"/>
           <!-- To return to next page of records-->
           <apex:commandButton action="{!stdSetController.next}" value="Next >" disabled = "{!!stdSetController.HasNext}"/>
           <!-- To return to last page of records-->
            <apex:commandButton action="{!stdSetController.last}" value=">>" title="Last Page" disabled="{!!stdSetController.HasNext}"/>
           <!-- InputText to display current page and to navigate to any page number, At righmost side of page-->
           <span style="float:right">
                <apex:outputLabel value="Page ">
                </apex:outputLabel>
                <apex:InputText value="{!PageNumber}" maxLength="4" size="1"/>
                <!-- To navigate to the page-->
                
                <apex:outputLabel value=" of {!stdSetController.PageNumber}">
                </apex:outputLabel>
            </span>
            <!-- To display a list for number of records to be selected per page-->
            <span style = "float:left">
                <apex:SelectList value="{!RecordsPerPageslist}" size="1">
                    <apex:selectOptions value="{!RecordsPerPageOptionList}">
                    </apex:selectOptions>
                </apex:SelectList>
            </span>
            </div>

</apex:PageBlock>
</apex:form>
</apex:page>

Controller code:
public class ContactListViewController {
    list<contact> con = [SELECT Name,Account.name,Title,Phone,Email FROM Contact];
        public ApexPages.StandardSetController stdSetController{
        get{
        stdSetController = new ApexPages.StandardSetController(con);
                return stdSetController;            
        }
        set;//what is use of this set, if we remove this then error is displayed!. 
        //The value is already set in the getter we just need to use that value not setting it
    }
    public Integer PageNumber{
        get{
            this.PageNumber=stdSetController.getPageNumber();
            return this.PageNumber;
        }
        set{
            stdSetController.setPageNumber(value);
        }
    }
    public Integer MaxNumberOfRecords{
        get{
        return stdSetController.getRecords().size();
        }
        set;
    }
    public list<SelectOption> getRecordsPerPageOptionList(){
        list<selectOption> options = new list<SelectOption>();
            options.add(new selectOption('10','10'));
            options.add(new selectOption('25','25'));
            options.add(new selectOption('50','50'));
            options.add(new selectOption('100','100'));
            options.add(new selectOption('200','200'));
            return options;
    }
    public Integer RecordsPerPageslist{
        get;set{
            if(value==null) 
                this.RecordsPerPageslist=10;
             else
                 this.RecordsPerPageslist=value;
        }    
    }
    public list<contact> getcontactList(){ 
       stdSetController.setPageSize(RecordsPerPageslist);
        return (list<contact>)stdSetController.getRecords();
    }
}

The error displayed on my page is:
Visualforce Error
Help for this Page
System.NullPointerException: Argument 1 cannot be null 
Class.ContactListViewController.getcontactList: line 41, column 1
 query = 'Select ' + CorrespondingFields + ' from ' + AllObjectsList;

Here,  object and fields of that object both are dynamic.
pls help how to display records in table on visualforce page.
Hi,
What is Schema.sObjectType in this satatement ?
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

I am unable to know exactly..
Thanks in Advance,
Anji
I was solving this challenge and my VF code is:

<apex:page standardController="Account" recordSetVar="Accounts" >
    <apex:pageblock>
        <apex:repeat var="a" value="{!Accounts}" rendered="true"  id="account_list">
            <li>
                <apex:outputLink value="https://ap1.salesforce.com/{!a.ID}" >
                    <apex:outputText value="{!a.Name}"/>
                </apex:outputLink>
            </li>
        </apex:repeat>
    </apex:pageblock>
</apex:page>

I am getting the list of accounts as required and on clicking on any of the accouts, it redirects to that accounts detail page.
Still I am getting following error from trailhead:

"The page does not bind to the record ID value (in order to link to the record detail page)"
Hello developer heroes!

I'm working through the Apex modules on Trailhead and can't seem to get past this one: https://developer.salesforce.com/en/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk.

Hopefully this doesn't read like a 'please complete the course for me' kinda post, but I have written a trigger that I believe meets the criteria but it isn't passing the check, so I wanted to seek the guidance of the experts.

The challenge is to do this:

Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'.

To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.The Apex trigger must be called 'ClosedOpportunityTrigger'

- With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
- To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
- This challenge specifically tests 200 records in one operation.


And here is the trigger I have come up with, which compiles OK and stands up to a manual (though admittedly unbulkified) test:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List<Task>();
    
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new]){
                    
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                  WhatId = opp.Id));
       
    }

    if(taskList.size()>0){
        
        insert taskList;
        
    }
    
}
I have tried replacing the SOQL with a straightforward 'for (Opportunity opp : Trigger.new)' and having the taskList.add inside an IF that checks for Closed Won - no luck. I also thought about checking to see if the stage was being changed to Closed Won, rather than the trigger firing on every edit, but I don't think this is what the module is asking for.

Where do you think I'm going wrong?

Huge thanks in advance!