1
 public static void updateAdminContact(List<Accreditation__c> newApplicationList) {

    Set<ID> accSet = new Set<ID>();
    for (Accreditation__c ad : newApplicationList) {
        accSet.add(ad.Account__c);
    } 

    List<Account> accList = [select Primary_Admin_Contact__c from Account where id in:accSet];
    Map<Id, Id> accMap = new Map<Id, Id>();

    for (Account a : accList) {
        accMap.put(a.id, a.Primary_Admin_Contact__c);
    }

    for (Accreditation__c ad : newApplicationList) {
        ad.Primary_Contact__c = accMap.get(ad.Account__c);
    }
}

In above class List newApplicationList has passed. Is that list will fetch all records in Accreditation__c object and check the condition

ad.Primary_Contact__c = accMap.get(ad.Account__c);

for each record. We didn't write any query for the Accreditation then what records will it fetch and process. Now i want to fetch records with some condition. What parameters need to pass

2
  • newApplicationList will only have all record which you have query or assign another list. If you don't assign any value it will be blank. Mar 28, 2016 at 12:29
  • We didn't query any records in that class and how can assign list to that. can we assign list which used in another class? Can you please give me one example class
    – Minion
    Mar 28, 2016 at 12:33

2 Answers 2

2

You have to pass a list of records into your class. Someplace in some other code, perhaps in another class or trigger, you will have code that looks something like this:

list<Accreditation__c>MyAccreditationLst = new list<Accreditation__c>();
// you'll populate the above list by some means

for(Accreditation__c ac : trigger.new)
{
   MyAccreditationLst.add(ac);
}

updateAdminContact uac = new updateAdminContact();
uac(MyAccreditationLst);

That's how you'd see your class called from say a trigger that fired on Accreditation__c.

0

This appears to be a class that is called by a Trigger more than likely before insert but could also be before update. If you look at your Accreditation__c custom object you should see the corresponding trigger and in there you will see how it is calling this method.
Assuming this is before insert because it appears that it is assuming the account primary contact is not already set on the Accreditation__c record. Also assuming it is before and not after because there are no dml statements in the method. Lists are basically passed by reference in that the list sent in is being updated by the method so you do not need to return anything. Assuming my assumptions are correct the following is occurring:
1. Records are being inserted
2. The records being inserted cause the before insert trigger to run
3. The trigger checks to see if it is before insert and creates an instance of this class
4. Then the method on this instance is called and passed in the list of records that caused the trigger to run

1
  • 1
    @Minion - perhaps a clarification to Jenny's excellent response; records passed into the method that are references to a before insert/update Trigger.new will implicitly be updated with any field changes made by the method when the trigger completes.
    – cropredy
    Mar 28, 2016 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.