Monday, January 5, 2015

Salesforce code for Mass Update from List View

 Sample code for Opportunity

List view  Button Code

{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}

var records = {!GETRECORDIDS( $ObjectType.Opportunity)};

if (records[0] == null ) {
    alert("Please select at least one record.")
} else {
    window.location = '/apex/OpportunityChangeStatus?Id=' + records;
}

Page

<apex:page standardController="Opportunity" extensions="OpportunityChangeStatus" recordSetVar="opp">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel > Select a stage:</apex:outputLabel>
                <apex:inputField value="{!oppRec.StageName}"/>
            </apex:pageBlockSectionItem>    
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public class OpportunityChangeStatus {
    
    public Opportunity oppRec{get;set;}
    List<Opportunity> oppList = new List<Opportunity>();
    List<String> oppIdList = new List<String>();
    List<Opportunity> updatedOppList = new List<Opportunity>();
    
    public OpportunityChangeStatus(ApexPages.StandardController controller) {
        
    }
    
    private ApexPages.StandardSetController standardController;
    public OpportunityChangeStatus(ApexPages.StandardSetController standardController){
        oppRec = new Opportunity();        
        String oppId = ApexPages.currentPage().getParameters().get('id');
        oppIdList = oppId.split(',');       
    }
    public Pagereference save(){    
        
        for(String oppId : oppIdList){
            Opportunity opp = new Opportunity();
            opp.Id = oppId;
            opp.StageName = oppRec.StageName;
            updatedOppList.add(opp);
        }        
        if(updatedOppList.size() > 0){
            update updatedOppList;
        }        
        return new PageReference('/006');
    }
}

No comments:

Post a Comment

Salesforce - Generate dynamic inner query to fetch parent and related child records

Use Case: In many scenarios, we need to clone the records with related child records. Issue / Limitation: We may simply use the "cl...