• Soubhagya Ranjan 2
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 23
    Questions
  • 9
    Replies
I want to delete records having status as rejected through batch class . 

please check the below code and let me know the correct code 

global class batchdeleteloan implements Database.batchable<sobject>{
global final String Query;
global batchdeleteloan (String q)
{
Query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<SObject> scope)
{
List <List<Laon_Application__c> > loanList = new list<List<Laon_Application__c> >();
for(List<Laon_Application__c> l : scope)
{
List<Laon_Application__c> la = (List<Laon_Application__c>)l;
If(l.Status__c = 'Rejected'){
LoanList.add(la);
}
}
Delete LoanList;
}

global void finish(Database.BatchableContext BC){

}
}
Hi ,

my requirement is to merge account records with same name . after merge i will store the deleted record value in another object  . the deleted record which will store in new object will have the id of the master record of account to which it is merged . 
I hav a batch apex in account object . it will update account records . but when one single record failed to update then batch apex failed . please help me on this .. below is my code


global class batchAccount2 implements Database.Batchable<sobject>, Database.stateful {
  
    global Database.QueryLocator start(Database.BatchableContext bc){
      
        String query = 'SELECT Id, Name FROM Account';
        return Database.getQueryLocator(query);
    }
      
    global void execute(Database.BatchableContext bc, List<account> scope) {
      
        for(Account a : scope) {
            a.Name = a.Name + 'Updated';
        }
        // update scope;
        Database.update(scope, false);
    } 
      
    global void finish(Database.BatchableContext bc) {
       // Get the AsyncApexJob that represents the Batch job using the Id from the BatchableContext
 AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
  TotalJobItems, CreatedBy.Email, ExtendedStatus
  from AsyncApexJob where Id = :BC.getJobId()];
  
 // Email the Batch Job's submitter that the Job is finished.
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 String[] toAddresses = new String[] {a.CreatedBy.Email};
 mail.setToAddresses(toAddresses);
 mail.setSubject('BatchJob batchAccount2  Status: ' + a.Status);
 mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
  ' batches with '+ a.NumberOfErrors + ' failures. Please modify the error . ExtendedStatus: ' + a.ExtendedStatus);
   
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
Hi I am having this trigger . when my account name is already present status will updated as Y . and when address is already present then its not updating to Y .. why ?

trigger accountupdatestatus1 on Account (before insert) {
  list<account> acc = new list<account>(); 
  list<account> acc1 = new list<account>();
 for(account a: trigger.new)
 {
acc=[select id,name from account where name=:a.name];
  if(acc.size()>0)
  {
   a.Status__c = 'Y' ;
  }
 }
 for(account a1: trigger.new)
 {
acc1=[select id,name,BillingStreet from account where name=:a1.BillingStreet];
  if(acc1.size()>0)
  {
   a1.Status__c = 'Y' ;
  }
 }
}
i have a trigger which shows error while trying to delete a record . it is working fine but after that the requirement is to send email to owner of that record . it is not working . i am posting my code . please check and provide the solution .

Trigger : 
Trigger ErrorDelete on Course__c(before delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Course__c c: Trigger.old) {
        c.adderror ('can not delete');
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'ranjan.soubhagya2015@gmail.com'});
        email.setSubject('Deleted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account  has been deleted.');
        emails.add(email);
    Messaging.sendEmail(emails);
        
    }
    
}




whenever i am commenting the adderror line then mail is sending but at a time error showing and email sending not working 
email not sent to user .

Trigger : 

trigger checkDuplicateValue on Account (after insert)
{
    if(Trigger.isAfter)
    {   
        //Get list of Users
        Map<Id,User> userMap = new Map<Id,User>([SELECT Name, Email FROM User]);
        
        //Get all Ids in order to do a query
        //and bulkify the trigger
        Set<Id> accIdSet = new Set<Id>();
        for(Account a : Trigger.new)
        {
            accIdSet.add(a.Id);
        }
        
        List<Account> accList = [Select ID, owner.Id, Name, fax, phone 
                                 From Account 
                                 Where Id in :accIdSet];
        
        if(!accList.isEmpty())
        {
            //Create a map with the Name and the Owner Id
            //With the Owner Id I can look for the User in the first Map
            Map<String, Id> accMap = new Map<String, Id>();
            for(Account acc : accList)
            {
                accMap.put(acc.Name, acc.owner.Id);
            }
            
            for(Account acc : Trigger.new)
            {
                String accName = acc.Name;
                if(accMap.containsKey(accName))
                {
                    //Double check the name for the new account
                    Id userId = accMap.get(accName);
                    acc.adderror('Account already exists in your Organization with name ' + acc.Name);
                    
                    //Retrieve the owner's email
                    String theEmail = userMap.get(userId).Email;
                    EmailHelper.sendEmail(theEmail);
                }
            }
        }
    }
}



Class :

public with sharing class EmailHelper
{
    public static void sendEmail(String theEmail)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {theEmail});
        mail.setSenderDisplayName('My Email');
        mail.setSubject('My email subject');
        mail.setPlainTextBody('Process fails duplicate value');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

 
while inserting records into account if the phone number is duplicate then it will show an error and send email to account owner to modify the filed value .
how to write trigger for this .

Thanks,
Soubhagya