Pages

Tuesday 15 November 2011

How to create a visual force page to submit the Opportunity for mass approval or rejection

Hello friends,

Some time it may happen that you need a custom functionality for mass submission of Opportunity for approval and rejection. Here is the apex and visual force code for this functionality.

I have used wrapper class in this functionality.

Apex Controller :


    public class wrapperClassController {
    
        //Our collection of the class/wrapper objects cContact 
        public List<cContact> contactList {get; set;}
    
        //This method uses a simple SOQL query to return a List of Contacts
        public List<cContact> getContacts() {
            if(contactList == null) {
                contactList = new List<cContact>();
                for(Opportunity c: [select Id, Name from Opportunity ]) {
                    // As each contact is processed we create a new cContact object and add it to the contactList
                    contactList.add(new cContact(c));
                }
            }
            return contactList;
        }
    
    
        public PageReference processSelected() 
        {
            try
            {
    
                    //We create a new list of Contacts that we be populated only with Contacts if they are selected
            List<Opportunity > selectedContacts = new List<Opportunity >();
    
            //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
            for(cContact cCon: getContacts()) {
                if(cCon.selected == true) {
                    selectedContacts.add(cCon.con);
                }
            }
    
            // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
            
     
            for(Opportunity con: selectedContacts) 
            {
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(con.Id);
                // submit the approval request for processing
                Approval.ProcessResult result = Approval.process(req);
                // display if the reqeust was successful
                System.debug('Submitted for approval successfully: '+result.isSuccess());
            }
    
    
            }
            catch(Exception ex)
            {}
                    PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
                    return pageRef;
        }
    
    
        // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
        public class cContact {
            public Opportunity con {get; set;}
            public Boolean selected {get; set;}
    
            //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
            public cContact(Opportunity c) {
                con = c;
                selected = false;
            }
        }
    }
     
     
     
Visual Force code :
    
    
    <apex:page controller="wrapperClassController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" reRender="table"/>
            </apex:pageBlockButtons>    
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <apex:column value="{!c.con.name}"/>
                
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    </apex:page> 

6 comments:

  1. Hi Niket! This is a very helpful post. Thank you!

    ReplyDelete
  2. Salesforce Admin Online Training - 21st Century Software ...
    www.21cssindia.com/courses/salesforce-admin-online-training-143.html
    Salesforce admin online training by 21cssindia the largest institute in providing online trainings in all technologies. Salesforce admin training, salesforce ...
    salesforce developer online training| salesforce developer ...
    www.21cssindia.com/.../salesforce-developer-online-training-144.html
    21cssindia provides Salesforce developer online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce ...
    salesforce crm online training| salesforce crm training| call ...
    www.21cssindia.com/courses/salesforce-crm-online-training-212.html
    21cssindia provides Salesforce crm online training by real time Experts. Call us +91 9000444287 for online training and demo. Online Salesforce crm training ...

    ReplyDelete
  3. Well,
    I am too very late to reply on this. Maybe, what I am going to say is totally irrelevant but is definitely linked with the approval process.

    Q: Is it possible to create an approval process through a custom VF page? I am been trying to find out some information on it on the web but unable to get some good results.

    ReplyDelete
  4. Nice post :)there is always scope for improvement. please follow proper name convention for example wraper class and that list names contactList????..rather then that everything is simple superb

    ReplyDelete
  5. Learn Salesforce.com developer courses online/classroom in Delhi from top training institutes and get Salesforcedeveloper certification. Get details on course fees @91-931OO96831!!

    ReplyDelete