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
neshnesh 

How to call apex class from List View Custom Button .. my sample code is here

How to call  apex class from List View Custom Button  ..my inside apex class code is here- how to use it...Please help me

INSIDE CLASS:
List <training__c> t1 = new list <training__c>();
       list <training_survey__c> ts2 = new list<training_survey__c>();
       t1 = [ select day1surveyflag__c, day3surveyflag__c, day5surveyflag__c from training__c where day1surveyflag__c = true or day3surveyflag__c = true or day5surveyflag__c = true ];
       for(training__c tx : t1)
          {
             training_survey__c ts = new training_survey__c();
             ts.training__C = tx.id;
             if(tx.day1surveyflag__c == true )
               {
                 ts.Survey_Type__c = 'Day1';
               }else
             if(tx.day3surveyflag__c == true )
               {
                 ts.Survey_Type__c = 'Day3';
               }else
             if(tx.day5surveyflag__c == true )
               {
                 ts.Survey_Type__c = 'Day5';
               }else
               {
                 ts.Survey_Type__c = 'Dropped';
               } 
             RecordType r = [Select id from Recordtype where sobjecttype = 'training_survey__c' and name= :ts.Survey_Type__c];
             ts.RecordTypeid = r.id;
             ts2.add(ts); 
          } 
        if(ts2 != null )
         {
           insert ts2;  
         }     

 

 

Best Answer chosen by Admin (Salesforce Developers) 
GlynAGlynA

Hi Nesh,

 

To do this you need a VisualForce page and a controller extension.

 

The controller extension, let's call it "CreateTrainingSurveys.cls", will contain your code.  I've optimized the code a bit to move the record type query out of the loop and to make the code easier to get code coverage:

 

public class CreateTrainingSurveys
{
    public CreateTrainingSurveys( ApexPages.StandardController controller ) {}

    public void createSurveys()
    {
        Set<String> set_SurveyTypes = new Set<String>{ 'Day1', 'Day3', 'Day5', 'Dropped' };

        Map<String,Id> map_RecordTypeIDs = new Map<String,Id>();

        for ( RecordType recordType :
            [   SELECT  Id
                FROM    RecordType
                WHERE   (   sObjectType = 'Training_Survey__c'
                        AND Name IN :set_SurveyTypes
                        )
            ]
            )
        {
            map_RecordTypeIDs.put( recordType.Name, recordType.Id );
        }

        List<Training_Survey__c> list_TrainingSurveysToCreate = new List<Training_Survey__c>();

        for ( Training__c training :
            [   SELECT  Day1SurveyFlag__c, Day3SurveyFlag__c, Day5SurveyFlag__c
                FROM    Training__c
                WHERE   (   Day1SurveyFlag__c = true
                        OR  Day3SurveyFlag__c = true
                        OR  Day5SurveyFlag__c = true
                        )
            ]
            )
        {
            String surveyType   = training.Day1SurveyFlag__c    ?   'Day1'
                                : training.Day3SurveyFlag__c    ?   'Day3'
                                : training.Day5SurveyFlag__c    ?   'Day5'
                                :                                   'Dropped';

            list_TrainingSurveysToCreate.add
            (   new Training_Survey__c
                (   Training__c     = training.Id,
                    Survey_Type__c  = surveyType,
                    RecordTypeId    = map_RecordTypeIDs.get( surveyType )
                )
            );
        }
        insert list_TrainingSurveysToCreate;
    }
}

 

The VF page, let's call it "CreateTrainingSurveys.page", should be something like this:

 

<apex:page standardController="Training__c" extensions="CreateTrainingSurveys" action="{!createSurveys}">
<script>
    window.history.back();
</script>
</apex:page>

That's all you need.  The button will launch this page, which will display for a moment and then return to the page that you came from.  If you want, you could put some content on the page, like "Creating Training Surveys - Please Wait..."

 

Note that you must use the standard controller in order for the page to be available as a list view VF button for the object.

 

Then, you need to go to Setup | Create | Objects | Training__c and create your custom list view button.

 

Finally, add the custom button to the appropriate layout.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

 

 

All Answers

GlynAGlynA

Hi Nesh,

 

To do this you need a VisualForce page and a controller extension.

 

The controller extension, let's call it "CreateTrainingSurveys.cls", will contain your code.  I've optimized the code a bit to move the record type query out of the loop and to make the code easier to get code coverage:

 

public class CreateTrainingSurveys
{
    public CreateTrainingSurveys( ApexPages.StandardController controller ) {}

    public void createSurveys()
    {
        Set<String> set_SurveyTypes = new Set<String>{ 'Day1', 'Day3', 'Day5', 'Dropped' };

        Map<String,Id> map_RecordTypeIDs = new Map<String,Id>();

        for ( RecordType recordType :
            [   SELECT  Id
                FROM    RecordType
                WHERE   (   sObjectType = 'Training_Survey__c'
                        AND Name IN :set_SurveyTypes
                        )
            ]
            )
        {
            map_RecordTypeIDs.put( recordType.Name, recordType.Id );
        }

        List<Training_Survey__c> list_TrainingSurveysToCreate = new List<Training_Survey__c>();

        for ( Training__c training :
            [   SELECT  Day1SurveyFlag__c, Day3SurveyFlag__c, Day5SurveyFlag__c
                FROM    Training__c
                WHERE   (   Day1SurveyFlag__c = true
                        OR  Day3SurveyFlag__c = true
                        OR  Day5SurveyFlag__c = true
                        )
            ]
            )
        {
            String surveyType   = training.Day1SurveyFlag__c    ?   'Day1'
                                : training.Day3SurveyFlag__c    ?   'Day3'
                                : training.Day5SurveyFlag__c    ?   'Day5'
                                :                                   'Dropped';

            list_TrainingSurveysToCreate.add
            (   new Training_Survey__c
                (   Training__c     = training.Id,
                    Survey_Type__c  = surveyType,
                    RecordTypeId    = map_RecordTypeIDs.get( surveyType )
                )
            );
        }
        insert list_TrainingSurveysToCreate;
    }
}

 

The VF page, let's call it "CreateTrainingSurveys.page", should be something like this:

 

<apex:page standardController="Training__c" extensions="CreateTrainingSurveys" action="{!createSurveys}">
<script>
    window.history.back();
</script>
</apex:page>

That's all you need.  The button will launch this page, which will display for a moment and then return to the page that you came from.  If you want, you could put some content on the page, like "Creating Training Surveys - Please Wait..."

 

Note that you must use the standard controller in order for the page to be available as a list view VF button for the object.

 

Then, you need to go to Setup | Create | Objects | Training__c and create your custom list view button.

 

Finally, add the custom button to the appropriate layout.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

 

 

This was selected as the best answer
neshnesh
Thanks
Rajesh SFDCRajesh SFDC

i want to display the all sobejct in visualforce page .. i want to check these sobject deletable,readable,writeable. for eg  i want to display all sobject in dropdown box  and check button in visualforcepage. when i click on check button, check wheather that sobject is deletable,readable,writeable..i done no how to get all sobjects in visualforce page.. plz help me frds

GlynAGlynA

@php tool kit,

 

I might have an answer for you, but you need to post this new question in a new thread so that it can have its own solution.  I'll look for your post and then I (and others) can help you out.

 

-Glyn

Rajesh SFDCRajesh SFDC

ok i ll post the my question in other thread..can u send me code ...

Rajesh SFDCRajesh SFDC

i want to display the all sobejct in visualforce page .. i want to check these sobject deletable,readable,writeable. for eg  i want to display all sobject in dropdown box  and check button in visualforcepage. when i click on check button, check wheather that sobject is deletable,readable,writeable..i done no how to get all sobjects in visualforce page.. plz help me frds

textualtextual
i too am trying to call an apex method with a selection of items from a list view and having issues on how to send over the selected items. 
Ive been mucking around trying different variations of the string to pass as the parameter but the closest ive gotten is the value of the object id

heres a sample of what im doing

myRecords = {!GETRECORDIDS($ObjectType.thing__c)};

sforce.apex.execute("className", "methodName", {ids:"[WHAT GOES IN HERE]"});

i can see it pass in a string on the class, but its never a tring of ids that were selected
i understand the workaround of using a custom page and controller, but i think the users want a warning if no records are selected