Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
2) Write a batch Apex program to phone number in the contact object withthe phone number in the corresponding account object where contact is
child of account.

Batch Class:
------------

			public class UpdateContactPhoneBatch implements Database.Batchable <Sobject>
			{
				Public Database.QueryLocator start(Database.BatchableContext bContext)
				{
					string query = 'Select id,AccountID,Phone from Contact where AccountID != Null';
					return Database.getQueryLocator(query);
				}
				
				Public void Execute(Database.BatchableContext bContext,list<Contact> lstcon)
				{
					set<id> lstset = new set<id>();
					for(Contact con : lstcon)
					{
						lstset.add(con.AccountID);
					}
					
					map<id,Account> lstMap = new map<id,Account>();
					for(Account acc : [Select id ,name,phone from Account where ID IN :lstset ])
					{
						lstMap.put(acc.id,acc);            
					}
					
					list<Contact> lstlist = new list<Contact>();
					for(Contact con : lstcon)
					{
						Account acc = lstMap.get(con.AccountId);
						con.Phone = acc.Phone;
						lstlist.add(con);
					}
					if(!lstlist.isEmpty())
					{
						Database.update(lstlist,false);
					}
					
					
				}
				
				Public void Finish(Database.BatchableContext bContext)
				{
					system.debug('Update Contact Phone ....'+bContext.getJobId());
					
					AsyncApexJob jobID = [Select id, status, totalJobItems,jobItemsProcessed,NumberofErrors,CreatedBy.email
										 from AsyncApexJob where id =: bContext.getJobId()];
					
					EmailUtility.sendEmailNotificationContactUpdate(jobID, 'UpdateContactPhoneBatch');
						
				}
			}

Eamil Class :
--------------
				
				public class EmailUtility 
				{
					Public static void sendEmailNotificationContactUpdate(AsyncApexJob jobID,string jobName)
					{
						if(jobID.Id != Null)
						{
							Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
							
							string[] toAddress = new string[]{jobID.CreatedBy.email,'mahendranimje24@gmail.com'};
								email.setToAddresses(toAddress);
							
							email.setSenderDisplayName('Batch Job Alert for Contact Object');
							email.setReplyTo('intellect@cloud.com');
							
							string emailSubject = 'Batch Job Status Notification for Contact Phone Update';
							email.setSubject(emailSubject);
							
							string emailContent = 'Dear Customer <br/><br/>'+
								'We Pleased to infrom you that, Your Batch Job status has been Completed <br/><br/>'+
								'Below are the Job status Details'+
								'<br/> Job Name is .....'+jobName+
								'<br/> Job Status ID is .....'+jobID.ID+
								'<br/> Job Status is....'+jobID.Status+
								'<br/> Total Job Items....'+jobID.TotalJobItems+
								'<br/> Total Batch are Processed...'+jobID.JobItemsProcessed+
								'<br/> Number of Bactes are Failed...'+jobID.NumberOfErrors+
								'<br/> Batch Job Owner Email....'+jobID.CreatedBy.email+
								'<br/><br/> Please Contact on the below Address , if any Querries'+
								'<br/><br/> ****This is System Generated Email.Please DO NOT Replay.'+
								'<br/><br/> Thanks & Regards <br/> Support Team,<br/> Cloud Intellect,Nagpur';
							
							email.setHtmlBody(emailContent);
							
							Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email}); 
						}
						
					}
				}
				
Execution:
----------
			UpdateContactPhoneBatch ph = new UpdateContactPhoneBatch();
			Database.executeBatch(ph,8);