You need to sign in to do that
Don't have an account?

Schedule Apex class to query Assignments and add Assignment to Contact record.
I am trying to create an Apex class that will run every night and update a field on Contact based on specidic criteria.
Basically what I need to happen is for the system to query all assignemnts (pse__assignment__c) in the system with an end date (pse__End_Date__c) greater than TODAY (today__c)
I then need to add that assignemnt to a custom field I have created on Contact if the assignment passes the criteria. If there are no assignments for the specific contact then I need the assignment field to be NULL.
There is a lookup relationship between Assignemtn and Contact. In my code I think I am querying the assignments correctly but I am strugling with figuring out how to then associate that specific assignment with the correct contact. I am also struggling with how to make this Class a schedulable class that I am able to schedule to run every night.
Here is my code so far.
Any help would be GREATLY APPRECIATED
Basically what I need to happen is for the system to query all assignemnts (pse__assignment__c) in the system with an end date (pse__End_Date__c) greater than TODAY (today__c)
I then need to add that assignemnt to a custom field I have created on Contact if the assignment passes the criteria. If there are no assignments for the specific contact then I need the assignment field to be NULL.
There is a lookup relationship between Assignemtn and Contact. In my code I think I am querying the assignments correctly but I am strugling with figuring out how to then associate that specific assignment with the correct contact. I am also struggling with how to make this Class a schedulable class that I am able to schedule to run every night.
Here is my code so far.
Any help would be GREATLY APPRECIATED
public class AssignmentToContact implements Database.Batchable<sObject> { global Database.queryLocator start(Database.BatchableContext ctx ) { list<pse__Assignment__c> aid=New list<pse__Assignment__c>(); list<Contact> updatecontact = new list<Contact>(); for(pse__Assignment__c ass: Trigger.new){ if(ass.pse__End_Date__c >= today__c && ass.pse__Is_Billable__c = TRUE){ aid.add(ass.id); } list<Contact> contactlist=[SELECT Id,Assignment__c FROM Contact WHERE id = :aid]; for(Contact con: contactlist){ con.Assignment__c=ass.Id; updatecontact.add(con); } } update updatecontact; } } //IF THE list is empty then the field on CONTACT should be NULL
For a class to be schedulable it should implement the Schedulable Interface.
Since only part of the code is available I assume that you are aware of the Database.Batchable Interface. The interface has three methods which needs to be implemented.
- Start
- execute
- finish
The main purpose of the start method is to fetch the records which are needed for the batch to process. All sorts of logic should be carried out in execute method.>> line no 14 : list<Contact> contactlist=[SELECT Id,Assignment__c FROM Contact WHERE id =:aid];
Always you wont get any values in contactlist as you are checking two different SF ID
To assign the assignments to a contact you need to have some fields or parameters to find the matching contact. Please be bit more clear about what you need.
Regards
Karthik
So once we have the list of Assignemnts that meet the criteria in line 11 I then want to insert each specific assignemnt to the associated contact record. So on the Assignment record the Contact_c field is a lookup field to the contact record, so I want the assignment associated with each specifric contact that passes the criteria to then be inserted in the Assignment__c field on the Contact record.
So I guess for line 14 it needs to be something like
list<Contact> contactlist=[SELECT Id,Assignment__c FROM Contact WHERE id =:aid];
Is there a way to query the ID of the assignment as well as the ID of the contact (Contact__c) associacted with the Assignemnt? I figure once we have both those IDs we can then use the Contact__c ID to associate the assignments with the correct contacts.
Thanks !
From your above comment I think this is what you wanted to achieve.
Regards
Karthik
I also changed the code in Line 31 to get(assign.Id) because that is what I hope to insert into the Assignment__c field on the contact.
Here is my class thus far.
Please let me know if that looks okay ??
I am now working on the TEST class and am trying to figure out how to get it working and how to get it to pass coverage..
I have a good start but still need a little direction. Here is what I have so far.
I am first creating a contact record via my TestDataFactory, and then creating an Assignment where the pse__resource__c (contact lookup field on assignment) is equal to the contact that I created in the previous lines, and I made sure the Assignemnt is billable.
I then start the test as you can see and that is where things get hard for me..
Any help would be greatly appreciated!!
Thanks.