0

I have requirement to create an alert everyday displaying list of calls submitted by users sharing same set of accounts over last 7 days. But the text field should display selective values for user based on its accountshare.My code is in raw state written below. I am stuck in selectively displaying user the alert record field value Say if result set is:

C1 created by U1 on A1
C2 created by U2 on A1
C3 created by U3 on A2
C4 created by U4 on A3

if U1 has A2, A1 in account share, he should see single record with text field showing as:

C1 created by U1 on A1
C2 created by U2 on A1
C3 created by U3 on A2 

if U2 has A3, A1 in account share, he should see single record with text field showing as:

C1 created by U1 on A1
C2 created by U2 on A1
C4 created by U4 on A3

Please find my code below:

global class BatchCreateAlertforCalls implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {  

        String query = 'SELECT Id,CMS_Submitted_DateTime__c,Account_vod__r.name,createdby.name FROM Call2_vod__c where Status_vod__c = \'Submitted_vod\' AND CMS_Submitted_DateTime__c >= N_DAYS_AGO:7 AND OwnerId!=\'005U0000001vQ0a\'';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject>scope) {
        List<Call2_vod__c> callList = (List<Call2_vod__c>) scope;
        system.debug('call list>>>>'+calllist);
        List<Alert_vod__c> alertsubmittedcalllist = new List<Alert_vod__c>();
        List<Alert_vod__Share> alsharelist = new List<Alert_vod__Share>();
        String concatenatedText='';
        for(Call2_vod__c c: callList){
       concatenatedText+=c.Account_vod__r.name+' seen by '+c.createdby.name + ' on '+c.CMS_Submitted_DateTime__c.format()+'\n';            
        }
        concatenatedText = concatenatedText.removeEnd('\n');
        system.debug('concatenatedtext>>>>'+concatenatedText);

        if(callList.size()!=null && callList.size()>0){
            Alert_vod__c alert = new Alert_vod__c();
            alert.Name = 'Calls Submitted in last 7 days';
            alert.Activation_Date_vod__c = system.today();
            alert.Alert_Text_vod__c = concatenatedText;
            alert.Expiration_Date_vod__c = system.today()+1;
            alert.Created_by_batch__c = true;
            alertsubmittedcalllist.add(alert);
        }
        if(alertsubmittedcalllist.size() >0)
        {
            insert alertsubmittedcalllist;
            system.debug('alert list>>>>'+alertsubmittedcalllist);
        }

        List<Alert_vod__c> deleteAlerts = new List<Alert_vod__c>();
        deleteAlerts = [Select Id from Alert_vod__c where Expiration_Date_vod__c <= today and Created_by_batch__c =true ];
        if(deleteAlerts.size()>0){
            delete deleteAlerts;
        }
    }
    }
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
    }
}

Your Answer

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

Browse other questions tagged or ask your own question.