• ShikhaJain
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
Hi All,

We have a requirement where based on the Last Modified User we need to set the Priority value in a custom Field. However, we are doing this in the Before Update Event and hence are not getting the value for the Last Modified By in the Trigger.new. Can you please help us understand in which user's perspective would the Data.com Clean Job run so that we can Identify that the record was updated by a Data.com Clean Job.

What would be the value that UserInfo.getUserId() return in the before update event in this case as the data.com Clean Job runs in the background?

Thanks in advance.
Hi All,

We have a requirement where based on the Last Modified User we need to set the Priority value in a custom Field. However, we are doing this in the Before Update Event and hence are not getting the value for the Last Modified By in the Trigger.new. Can you please help us understand in which user's perspective would the Data.com Clean Job run so that we can Identify that the record was updated by a Data.com Clean Job.

What would be the value that UserInfo.getUserId() return in the before update event in this case as the data.com Clean Job runs in the background?

Thanks in advance.
Hello Experts,

Below is the batch class that I've written, however, the functionality is all the cases should be cloned on anniversary (date field) of an account but this is not working. Experts, please help me to write test class for the below batch as i'm beginner and need it urgent
 
public class CaseCloneOnAccountAnniversary implements Database.Batchable<SObject>,Schedulable
{
    public static final String BATCH_JOB_TITLE = 'My Batch Job';
    String errors = '';
    
    integer todayday = date.today().day();
    Integer currentmonth = date.today().month();

    public Map<Id, String> errorMap = new Map<Id, String>();
    public Map<Id,Case> IdToSObjectMap = new Map<Id, Case>();
    
    public Database.QueryLocator start(Database.BatchableContext bc)
    {
       return Database.getQueryLocator('SELECT Anniversary__c,Id from Account WHERE Anniversary__c = DAY_IN_MONTH(Anniversary__c) =:todayday AND CALENDAR_MONTH(Anniversary__c ) =: currentmonth') ;
    }
        
    
    public void execute(Database.BatchableContext bc, List<SObject> scope)
    {
        if(scope.isEmpty()) return;
        //List<Account> accts = (List<Account>) scope;
        // Lis<Account> acctsList = new List<Account>();
        List<Case> caseToCreateList = new List<Case>();
        
        
       // for( Account aAccount : (Account)scope )
       for(Account aAccount:[Select Id,(Select Id from cases order by CreatedDate limit 1) from account where Id IN : scope]) //Add all field in case query which you want to copy
        {
            if(aAccount.cases.size() > 0)
            {
                Case objCase = new Case();
                objCase  = aAccount.cases[0].clone(false, true);

                // collecting the Cases to create
                caseToCreateList.add(ObjCase);
            }
            
       }
        try
        {
           if(!caseToCreateList.isEmpty()) 
           { 
                        
             Database.SaveResult[] srList = Database.insert(caseToCreateList, false);
             Integer index = 0;
             system.debug('---srList  first'+ srList );
             
             for(Database.SaveResult result : srList )
             {
                if(!result.isSuccess())
                {
                    String errMsg = result.getErrors()[0].getMessage();
                    errorMap.put(caseToCreateList[index].Id, errMsg);
                    IdToSObjectMap.put(caseToCreateList[index].Id, caseToCreateList[index]);
                } // End Inner If
                index++;
              } // End for
            
            } // End Outer If
        } // End try
        catch( Exception ex )
        {
                this.errors += ex.getMessage();
        }
     }

    public void finish(Database.BatchableContext context)
    {
       //Send an email to the User after your batch completes 
       if(!errorMap.isEmpty()){
       AsyncApexJob a = [SELECT id,ApexClassId,JobItemsProcessed,TotalJobItems,NumberOfErrors, CreatedBy.Email FROM AsyncApexJob WHERE id = :context.getJobId()];
       
       String body = 'Your batch job '
             + 'CaseCloneOnAccountAnniversary'
             + 'has finished. \n' 
             + 'There were '
             + errorMap.size()
             + ' errors. Please find the error list attached to the mail.';
             
       // Creating the CSV file
        String finalstr = 'Id, Subject, Error \n';
        String subject = 'CaseCloneOnAccountAnniversary- Apex Batch Error List';
        String attName = 'CaseCloneOnAccountAnniversary Errors.csv';      
        
        for(Id id  : errorMap.keySet()){
                string err = errorMap.get(id);
                Case objCase = (Case) IdToSObjectMap.get(Id);
                String recordString = '"'+id+'","'+ objCase.Subject +'","'+ err +'"\n';
                finalstr = finalstr +recordString;
            } 
            
            // Define the email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
 
            // Create the email attachment    
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName(attName);
            efa.setBody(Blob.valueOf(finalstr));
 
            // Sets the paramaters of the email
            email.setSubject( subject );
            email.setToAddresses( new String[] {'abc@gmail.com'} );
            email.setPlainTextBody( body );
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
 
            // Sends the email
            Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
       
      }       
    }

    public void execute(SchedulableContext sc)
    {
        CaseCloneOnAccountAnniversary scheduleBatch= new CaseCloneOnAccountAnniversary();
        Database.executeBatch(scheduleBatch);
    }

        
   }

 
Hi All,

We have a requirement where based on the Last Modified User we need to set the Priority value in a custom Field. However, we are doing this in the Before Update Event and hence are not getting the value for the Last Modified By in the Trigger.new. Can you please help us understand in which user's perspective would the Data.com Clean Job run so that we can Identify that the record was updated by a Data.com Clean Job.

What would be the value that UserInfo.getUserId() return in the before update event in this case as the data.com Clean Job runs in the background?

Thanks in advance.