+ Start a Discussion
Martha VMartha V 

how to call a method in a class that is not static in production

Hi, I wrote a class that has no static methods (due to some public variables that I needed to use accross different methods). I tested it by making an instance of the class. My question is once I get it in production how do I call it?

Here is my code
public class ImportAZGives {
        public List<AZGives_Donor__c> donors;
        public map<String, AZGives_Donor__c> map_donors;
        public List<Opportunity> oppys;
        public List<Task> taskList;
        public Campaign theCampaign;
        public List<Contact> newContacts;
        public List<AZGives_Donor__c> annon;        

    
    public ImportAZGives(){
        annon = new List<AZGives_Donor__c>();
        theCampaign = [SELECT id, Name FROM Campaign 
                       WHERE Name = 'AZGives Main Campaign' limit 1];
        oppys = new List<Opportunity>();
        taskList = new List<Task>();
        for (Integer x=0 ; x<30; x++)
        {
            boolean done = checkWithContacts();    
            if (done) x = 31;
        }
    }
    
    public Boolean checkWithContacts() 
    {
        donors = [SELECT id, Amount__c, Transaction_Date_Time__c, Anonymous__c,
                 FirstName__c, LastName__c, Email__c, Phone_Number__c,
                 Address_1__c, Address_2__c, City__c, State__c, Country__c,
                 Zip_Code__c, Cover_Fees__c, In_Memory__c, Comments__c, Get_Involved__c,
                 Campaign_Name__c, Processed__c, Matched_Email__c, Not_Found__c 
                 FROM AZGives_Donor__c WHERE 
                 (Not_Found__c = false AND Processed__c = false AND 
                  Matched_Email__c = false) Limit 200];
        system.debug('donors.size() = '+ donors.size());
        if (donors.size()>0)
        {
            map_donors = new map<String, AZGives_Donor__c>();
            system.debug('map_donors.size() = '+ map_donors.size());
            for (AZGives_Donor__c donor : donors)
            {
                system.debug('donor = ' + donor);
                if ( ! map_donors.containsKey(donor.Email__c))
                    if (donor.Anonymous__c.contains('Fully')) annon.add(donor); 
                    else
                        map_donors.put(donor.Email__c, donor);
                }
            system.debug('map_donors.size() = '+ map_donors.size());
            SearchContacts();
            return false;
        }
        else return true;
    }
    
    public void SearchContacts(){    
        system.debug('in SearchContacts');
        List<Contact> conts = [SELECT id, FirstName, LastName, Email from Contact
                              WHERE Email IN :map_donors.keySet()];
        system.debug('conts size = '+ conts.size());
        List<AZGives_Donor__c> updatedDonors = new List<AZGives_Donor__c>();
        for (Contact c : conts){
            AZGives_Donor__c donor = map_Donors.get(c.Email);
            system.debug('contact name '+ c.FirstName +' ' + c.LastName);
            system.debug('donor First Name '+ donor.FirstName__c +
                        ' donor Last Name ' + donor.LastName__c);
            if (c.FirstName == donor.FirstName__c && c.LastName == donor.LastName__c)
            {
                oppys.add(newOppy(c, donor));
                donor.Processed__c = true;
            }
            Else
            {
                donor.Processed__c = false;
                donor.Matched_Email__c = true;
            }
            updatedDonors.add(donor);
            map_Donors.put(c.Email, donor);
        }
        If (map_Donors.size() > conts.size())
        {
            addNewContacts();        
            if (newContacts.size() > 0){
                insert newContacts;
                for (Contact newC : newContacts){  
                    system.debug('newC.id = '+ newC.id);
                    AZGives_Donor__c donor = map_Donors.get(newC.Email);

                    oppys.add(newOppy(newC, donor));
                }
            }
        }
        if (annon.size()>0) ProcessAnnonymous();        
        donors = map_Donors.values();        
        for (AZGives_Donor__c d : donors)
            if (!d.Processed__c && d.Matched_Email__c)
                d.Not_Found__c = true;
        update donors;
        insert oppys;
        insert taskList;
        
    }
    
    public void addNewContacts()
    {
        List<AZGives_Donor__c> not_Found = map_Donors.values();
        newContacts = new List<Contact>();
        For (AZGives_Donor__c d : not_Found){
            if (!d.Processed__c && !d.Matched_Email__c) {
                Contact c = new Contact(FirstName = d.FirstName__c,
                                   LastName = d.LastName__c,
                                   Email = d.Email__c,
                                           MailingStreet = d.Address_1__c,
                                           MailingCity = d.City__c,
                                           MailingState = d.State__c,
                                           MailingPostalCode = d.Zip_Code__c,
                                           Phone = d.Phone_Number__c);
                newContacts.add(c);
                d.Processed__c = true;
                map_Donors.put(d.Email__c, d);
            }
        }
    }

    public Opportunity newOppy(Contact c, AZGives_Donor__c donor){
        String stName = 'Donation '+ c.FirstName + ' ' +
                                c.LastName + '4/3/17';
        String annon1 = '';
        If (donor.Anonymous__c.contains('Partially')) 
            annon1 = 'Donor wishes to remain annonymous'; 
        Opportunity oppy = new Opportunity (Name = stName,
                                        RecordTypeId = '012o0000000xvXl',
                                        npsp__Primary_Contact__c = c.id,
                                        Amount = donor.Amount__c,
                                        CloseDate = donor.Transaction_Date_Time__c.date(),
                                        StageName = 'Closed Won',
                                        Description = annon1,
                                        CampaignId = theCampaign.Id);
        if (donor.Get_Involved__c)
            taskList.add(makeTask(c));
        return oppy;
    }
    
    public Task makeTask (Contact c)
    {
        Task contTask = new Task(ActivityDate = Date.Today() + 3,
             Description = c.FirstName + ' ' + c.LastName + ' wants to be involved with DA.',
                     Priority = 'High', Status = 'Not Started', 
                    Subject = 'Need to make CONTACT',
                    WhatId = theCampaign.Id, 
                    WhoId = c.id);
        return contTask;
    }
    
    public void ProcessAnnonymous(){
        Account acc = [SELECT id FROM Account WHERE Name = 'Anonymous' limit 1];
        if (acc == null){
            acc = new Account(Name = 'Anonymous');
            insert acc;
        }
        integer x = 1;
        for(AZGives_Donor__c d : annon){
            String str = 'Donation AZGives Anonymous ' + x;
            Opportunity oppy = new Opportunity (Name = str,
                                   AccountId = acc.id,
                                   RecordTypeId = '012o0000000xvXl',
                                   Amount = d.Amount__c,
                                   CloseDate = d.Transaction_Date_Time__c.date(),
                                   StageName = 'Closed Won',
                                   Description = 'Anonymous Donation',
                                   CampaignId = theCampaign.Id); 
            x++;
            d.Processed__c = true;
            map_Donors.put(d.Email__c, d);
            oppys.add(oppy);
        }
    }  
}
Best Answer chosen by Martha V
Martha VMartha V
Thanks Ashish for the answer. I was confused in how to instantiate the class from a production org, because you can't run Anonymous apex in there. You have to call it from a process or flow. However, this morning it came to me that I can make a VF page and have my class be an extension of the page and that is how it will get executed.

All Answers

Ashish DevAshish Dev
May be I am not getting what your actual question is. 
1) If a property (class level variable) is public you can access it from instance of the class.
2) You can directly access property of the class in its class methods. 
3) If you want to access a property of class 'A' into another class 'B' you first need to instantiate class 'A' and pass the property of class 'A' like instance_classA.public_classA_property to method of instance of class 'B' as a argument to that method.

Let me know if this helps you.
Martha VMartha V
Thanks Ashish for the answer. I was confused in how to instantiate the class from a production org, because you can't run Anonymous apex in there. You have to call it from a process or flow. However, this morning it came to me that I can make a VF page and have my class be an extension of the page and that is how it will get executed.
This was selected as the best answer