function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
p sonwanep sonwane 

Create Custom Object named "Training" with Fields Name, Email, Phone Number, Age, Account and Status. Name, Account are required fields and Account should be lookup with Account Object. Create a Batch class to verify the records created in last 24 hours


Create Custom Object  named "Training" with Fields Name, Email, Phone Number, Age, Account and Status. 
Name, Account are required fields and Account should be lookup with Account Object.
Create a Batch class to verify the records created in last 24 hours in training object and if any, create a respective contact record and send out an email to Account Owner saying that related contact is created.
Best Answer chosen by p sonwane
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pooja,

The query will result Object Object only as we are referencing the parent object field. It works in Apex. 

We are passing Training Account to contact Account. as we need Account while creating a Contact. If you dont want it you can remove that particular line but the contact will get created and you cannot see that Contact under the Account.

Let me know if you face any issues.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pooja,

Can you try the below batch class.

You can match other fields from Training to Contact. I have only matched Lastname with Training Name.
 
public class SendEmailtoAccountOwner implements Database.Batchable<sObject>{

     public Database.QueryLocator start(Database.BatchableContext BC){
         String query = 'SELECT Id,Name,Account__r.id,Account__r.name,Account__r.Owner.email FROM Training__c where CreatedDate = Today';
      return Database.getQueryLocator(query);
   }

   public void execute(Database.BatchableContext BC, List<Training__c> scope){
       List<Contact> conlist= new List<Contact>();
         List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
         List<String> sendTo = new List<String>();
       For(Training__c tr:scope){
                 String s ='';
            sendTO.add(tr.Account__r.Owner.email);
         Contact con = new Contact();
           con.lastname= tr.name;
           con.AccountId= tr.Account__c;
           conlist.add(con);
               s+=Con.lastname +' has been created for'+ tr.Account__r.name;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Contact Created  for Account Reated to Training');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
           
       }
     
insert conlist;
 Messaging.SendEmail(mails);       
    }

   public void finish(Database.BatchableContext BC){
   }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
p sonwanep sonwane
Hi Praveen, Thank you for your response. I tried your solution yesterday but it's not working. I ran query Account__r I am getting 'object object' results. Second contact creation con.accountId = tr.Account__c . Why you passed account__c Please reply
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pooja,

The query will result Object Object only as we are referencing the parent object field. It works in Apex. 

We are passing Training Account to contact Account. as we need Account while creating a Contact. If you dont want it you can remove that particular line but the contact will get created and you cannot see that Contact under the Account.

Let me know if you face any issues.

Thanks,
 
This was selected as the best answer
p sonwanep sonwane
Hi Praveen, Good morning. I can see the contact has been created.but mail is not sending to owner. What's the problem here? Thank you Pooja
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pooja,

The email should be sent to email of the owner of the Account. Can you check in junck or some other folders because this got worked in my org.

If you still face issue we can debug and check .

Thanks,
 
p sonwanep sonwane
Hi Praveen, Thank you so much for quick reply. I was facing problem because trigger was active on account object due to this contact was not generated on account object. But after deactivating trigger it's working fine and also received mail to account owner Regards, Pooja jagtap
p sonwanep sonwane
Hi Praveen, Sure will mark. Thanks, Pooja
p sonwanep sonwane
Hi Praveen can you provide test class for this scenario 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

As this would be seperate question can you post it as new question so it avoids confusion.

Thanks,