I am trying to look at all tasks for a specific time frame, then create a note and set the LinkedEntityId to the whatid or whoid. This code creates the content note, 1 for each task. But then created a contentdocumentlink for each whatid or whoid for every note.
For instance, the query returns 8 tasks, 8 contentnotes are created and then each note is associated to each whatid(opps). So every opp has 8 notes. I have reworked this a million times and I am just not sure how to get it to know that I just want each contentnote associated to the task it "copied".
At this point I think I am over complicating it and the answer is so easy that I am not seeing it. How can I get this to create one note per task, and then associate it to the opp that the task is related? (whatid)
global class archiveTasksBatch implements Database.Batchable<SObject>, Schedulable{
public final string query;
date mydate = date.today().addDays(-369);
public archiveTasksBatch() {
query = 'Select WhoId, WhatId, Subject, Status, OwnerId, Id, Description, CreatedDate, ActivityDate From Task where ActivityDate = :mydate' ;
}
public void execute(SchedulableContext sc){
Database.executeBatch(this, 100);
}
public Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext bc, List<sObject> scope){
list<ContentNote > newObjects = new list<ContentNote >();
list<ContentDocumentLink > newCDL = new list<ContentDocumentLink >();
Map<ID, String> taskIdMap = new Map<ID, String>();
for(Sobject s : scope){
Task obj = (Task) s;
String myString = obj.Description + obj.ActivityDate;
Blob myBlob = Blob.valueof(myString.escapeHtml4());
newObjects.add(new ContentNote (
Title = obj.Subject,
Content = myBlob
));
}
system.debug('*********************************newObjects' +newObjects.size());
system.debug('*********************************scope' +scope.size());
if(!newObjects.isEmpty()){
Database.SaveResult[] srList = Database.insert(newObjects, false);
for (Database.SaveResult sr : srList) {
for (Sobject sc : scope){
Task t = (Task) sc;
string tid = t.WhatId;
if(tid == null) {
tid = t.WhoId;}
taskIdMap.put(sr.Id, tid);
if(sr.isSuccess()) {
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = sr.getId();
cdl.LinkedEntityId = taskIdMap.get(sr.id);
cdl.Visibility = 'AllUsers';
cdl.ShareType = 'I';
newCDL.add(cdl);
system.debug('*********************************srList' +srList.size());
system.debug('*********************************newCDL' +newCDL.size());
system.debug('*********************************LinkedEntityId' +cdl.LinkedEntityId);
system.debug('*********************************ContentDocumentId' +cdl.ContentDocumentId);
}
}
}
}
insert newCDL;
}
public void finish(Database.BatchableContext bc){
system.debug('JOB IS FINISHED');
}
}