- Bharat S
- NEWBIE
- 0 Points
- Member since 2018
- Senior Salesforce Developer
-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
4Replies
Can anyone please help me write logic for trigger to populate title on event whenever event is updated. I have code written to populate title when event is created which is working perfectly.
I am attaching the trigger and triggerhandler class below. Please help creating trigger handler method when event is updated.
Event Trigger:-
Event Trigger:-
trigger EventTrigger on Event (before insert, before update, after insert, after update) {
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
if (Trigger.isUpdate) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
EventTriggerHandler:-
public class EventTriggerHandler {
public static String comma = ',' ;
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
if (Trigger.isUpdate) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
EventTriggerHandler:-
public class EventTriggerHandler {
public static String comma = ',' ;
public static void AfterInsert(Map<id, Event> EventMap)
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
}
catch(exception e)
{
throw e;
}
}
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
}
catch(exception e)
{
throw e;
}
}
-
- Bharat S
- June 20, 2019
- Like
- 0
- Continue reading or reply
Can anyone tell me what is wrong with the test method I have written in the following test class
Trigger is working fine and updating event whenever Title is getting updated on Contact. Just my test method is not covering any line in my apex class when i run the test class. Please help regarding this asap.
Trigger Handler Method in Apex Class for AfterUpdate trigger on Contact
public static void AfterUpdate(List<Contact> conList, Map<Id,Contact> oldContactMap){
Set<Id> UpdatedContactID = new Set<Id>();
for( Contact con : conList)
{
if(con.Title!=oldContactMap.get(con.Id).Title){
UpdatedContactId.add(con.Id);}
}
// system.debug('UpdatedContactId*****'+UpdatedContactId);
List<Event> eventList = new List<Event>();
try
{
for(Event e : [SELECT Id,Title__c FROM Event where StartDateTime > Today and Whoid in : UpdatedContactId])
{
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
//System.debug('eventList' + eventList);
}
catch(exception e)
{
throw e;
}
}
Test Method in Test Class for above Apex method
@isTest static void method1() {
Profile prof = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User user = new User(firstName = 'test1',lastName = 'test2',profileId = prof.id,
username = 'test@test.com'+Math.random(),email = 'test@test.com',
Alias = 'ts',TimeZoneSidKey = 'Asia/Kolkata',LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',LanguageLocaleKey = 'en_US');
insert user;
System.runAs(user){
List<Account>TestDataAccountList=New List<Account>(TestDataUtility.createAccounts(1));
List<Contact>ContactList=New List<Contact>();
Contact Cons = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry', Title= TestDataUtility.generateRandomString(5),
MailingCity='City67', isPrimary__c=True));
ContactList.add(Cons);
insert ContactList;
List<Event> EventListTest = New List<Event>();
Event Eve = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2020, 12, 9),
EndDateTime =Date.newInstance(2020, 12, 9), Location__c = 'Client Office', WhoId = Cons.Id, OwnerId= User.Id);
EventListTest.add(Eve);
insert EventListTest;
Test.startTest();
List<Contact>UpdatedContactList=New List<Contact>();
Contact Cons1 = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry1', Title= 'Title9',
MailingCity='City67', isPrimary__c=True));
UpdatedContactList.add(Cons1);
update UpdatedContactList;
List<Event> UpdatedEventListTest = New List<Event>();
Event Eve1 = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2016, 12, 9),
EndDateTime =Date.newInstance(2016, 12, 9), Location__c = 'Client Office', WhoId = Cons1.Id, OwnerId= User.Id);
UpdatedEventListTest.add(Eve1);
update UpdatedEventListTest;
Test.stopTest();
}
}
Trigger Handler Method in Apex Class for AfterUpdate trigger on Contact
public static void AfterUpdate(List<Contact> conList, Map<Id,Contact> oldContactMap){
Set<Id> UpdatedContactID = new Set<Id>();
for( Contact con : conList)
{
if(con.Title!=oldContactMap.get(con.Id).Title){
UpdatedContactId.add(con.Id);}
}
// system.debug('UpdatedContactId*****'+UpdatedContactId);
List<Event> eventList = new List<Event>();
try
{
for(Event e : [SELECT Id,Title__c FROM Event where StartDateTime > Today and Whoid in : UpdatedContactId])
{
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
//System.debug('eventList' + eventList);
}
catch(exception e)
{
throw e;
}
}
Test Method in Test Class for above Apex method
@isTest static void method1() {
Profile prof = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User user = new User(firstName = 'test1',lastName = 'test2',profileId = prof.id,
username = 'test@test.com'+Math.random(),email = 'test@test.com',
Alias = 'ts',TimeZoneSidKey = 'Asia/Kolkata',LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',LanguageLocaleKey = 'en_US');
insert user;
System.runAs(user){
List<Account>TestDataAccountList=New List<Account>(TestDataUtility.createAccounts(1));
List<Contact>ContactList=New List<Contact>();
Contact Cons = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry', Title= TestDataUtility.generateRandomString(5),
MailingCity='City67', isPrimary__c=True));
ContactList.add(Cons);
insert ContactList;
List<Event> EventListTest = New List<Event>();
Event Eve = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2020, 12, 9),
EndDateTime =Date.newInstance(2020, 12, 9), Location__c = 'Client Office', WhoId = Cons.Id, OwnerId= User.Id);
EventListTest.add(Eve);
insert EventListTest;
Test.startTest();
List<Contact>UpdatedContactList=New List<Contact>();
Contact Cons1 = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry1', Title= 'Title9',
MailingCity='City67', isPrimary__c=True));
UpdatedContactList.add(Cons1);
update UpdatedContactList;
List<Event> UpdatedEventListTest = New List<Event>();
Event Eve1 = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2016, 12, 9),
EndDateTime =Date.newInstance(2016, 12, 9), Location__c = 'Client Office', WhoId = Cons1.Id, OwnerId= User.Id);
UpdatedEventListTest.add(Eve1);
update UpdatedEventListTest;
Test.stopTest();
}
}
-
- Bharat S
- June 20, 2019
- Like
- 0
- Continue reading or reply
Can anyone help me write Test Class for mentioned Apex Class
I am a beginner in Salesforce and Apex. Can anyone help me write TestClass for this Apex Class.
Event Trigger Handler
public class EventTriggerHandler {
public static String comma = ',';
public static void AfterInsert(Map<id, Event> EventMap)
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
update eventList;
}
catch(exception e)
{
throw e;
}
}
}
Event Trigger
trigger EventTrigger on Event (before insert, before update, after insert) {
if (Trigger.IsBefore) {
if (Trigger.isInsert) {
}
if (Trigger.isUpdate) {
}
}
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
Event Trigger Handler
public class EventTriggerHandler {
public static String comma = ',';
public static void AfterInsert(Map<id, Event> EventMap)
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
update eventList;
}
catch(exception e)
{
throw e;
}
}
}
Event Trigger
trigger EventTrigger on Event (before insert, before update, after insert) {
if (Trigger.IsBefore) {
if (Trigger.isInsert) {
}
if (Trigger.isUpdate) {
}
}
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
-
- Bharat S
- June 17, 2019
- Like
- 0
- Continue reading or reply
Write a Apex trigger on Event to autopopulate Title of Contacts.
Write a Apex trigger on Event to autopopulate Title of Contacts in Relatedto field of Event and display them separated by commas. Can anyone help me wiriting the trigger for this.
-
- Bharat S
- June 06, 2019
- Like
- 0
- Continue reading or reply
Can anyone please help me write logic for trigger to populate title on event whenever event is updated. I have code written to populate title when event is created which is working perfectly.
I am attaching the trigger and triggerhandler class below. Please help creating trigger handler method when event is updated.
Event Trigger:-
Event Trigger:-
trigger EventTrigger on Event (before insert, before update, after insert, after update) {
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
if (Trigger.isUpdate) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
EventTriggerHandler:-
public class EventTriggerHandler {
public static String comma = ',' ;
if(Trigger.IsAfter)
{
if (Trigger.isInsert) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
if (Trigger.isUpdate) {
EventTriggerHandler.AfterInsert(trigger.newMap);
}
}
}
EventTriggerHandler:-
public class EventTriggerHandler {
public static String comma = ',' ;
public static void AfterInsert(Map<id, Event> EventMap)
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
}
catch(exception e)
{
throw e;
}
}
{
try
{
String NameTitle ='';
String Names;
List<EventRelation>EveRelationList=New List<EventRelation>();
EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
system.debug('AfterInsert count_____'+EveRelationList.size());
Set<Id> WhoIds = New set<Id>();
for(EventRelation eveRel : EveRelationList){
WhoIds.add(eveRel.RelationId);
system.debug('WhoIds after insert_____'+WhoIds);
}
List<Contact> ConList = New List<Contact>();
ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
for(Contact c : ConList){
if(c.Title!= null)
{
NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
}
}
Names = NameTitle.removeEnd(comma);
System.debug('Names' + Names);
List<Event> eventList = new List<Event>();
for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
{
e.Title__c = Names;
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
}
catch(exception e)
{
throw e;
}
}
- Bharat S
- June 20, 2019
- Like
- 0
- Continue reading or reply
Can anyone tell me what is wrong with the test method I have written in the following test class
Trigger is working fine and updating event whenever Title is getting updated on Contact. Just my test method is not covering any line in my apex class when i run the test class. Please help regarding this asap.
Trigger Handler Method in Apex Class for AfterUpdate trigger on Contact
public static void AfterUpdate(List<Contact> conList, Map<Id,Contact> oldContactMap){
Set<Id> UpdatedContactID = new Set<Id>();
for( Contact con : conList)
{
if(con.Title!=oldContactMap.get(con.Id).Title){
UpdatedContactId.add(con.Id);}
}
// system.debug('UpdatedContactId*****'+UpdatedContactId);
List<Event> eventList = new List<Event>();
try
{
for(Event e : [SELECT Id,Title__c FROM Event where StartDateTime > Today and Whoid in : UpdatedContactId])
{
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
//System.debug('eventList' + eventList);
}
catch(exception e)
{
throw e;
}
}
Test Method in Test Class for above Apex method
@isTest static void method1() {
Profile prof = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User user = new User(firstName = 'test1',lastName = 'test2',profileId = prof.id,
username = 'test@test.com'+Math.random(),email = 'test@test.com',
Alias = 'ts',TimeZoneSidKey = 'Asia/Kolkata',LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',LanguageLocaleKey = 'en_US');
insert user;
System.runAs(user){
List<Account>TestDataAccountList=New List<Account>(TestDataUtility.createAccounts(1));
List<Contact>ContactList=New List<Contact>();
Contact Cons = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry', Title= TestDataUtility.generateRandomString(5),
MailingCity='City67', isPrimary__c=True));
ContactList.add(Cons);
insert ContactList;
List<Event> EventListTest = New List<Event>();
Event Eve = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2020, 12, 9),
EndDateTime =Date.newInstance(2020, 12, 9), Location__c = 'Client Office', WhoId = Cons.Id, OwnerId= User.Id);
EventListTest.add(Eve);
insert EventListTest;
Test.startTest();
List<Contact>UpdatedContactList=New List<Contact>();
Contact Cons1 = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry1', Title= 'Title9',
MailingCity='City67', isPrimary__c=True));
UpdatedContactList.add(Cons1);
update UpdatedContactList;
List<Event> UpdatedEventListTest = New List<Event>();
Event Eve1 = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2016, 12, 9),
EndDateTime =Date.newInstance(2016, 12, 9), Location__c = 'Client Office', WhoId = Cons1.Id, OwnerId= User.Id);
UpdatedEventListTest.add(Eve1);
update UpdatedEventListTest;
Test.stopTest();
}
}
Trigger Handler Method in Apex Class for AfterUpdate trigger on Contact
public static void AfterUpdate(List<Contact> conList, Map<Id,Contact> oldContactMap){
Set<Id> UpdatedContactID = new Set<Id>();
for( Contact con : conList)
{
if(con.Title!=oldContactMap.get(con.Id).Title){
UpdatedContactId.add(con.Id);}
}
// system.debug('UpdatedContactId*****'+UpdatedContactId);
List<Event> eventList = new List<Event>();
try
{
for(Event e : [SELECT Id,Title__c FROM Event where StartDateTime > Today and Whoid in : UpdatedContactId])
{
eventList.add(e);
}
if(eventList.size()>0){
update eventList;
}
//System.debug('eventList' + eventList);
}
catch(exception e)
{
throw e;
}
}
Test Method in Test Class for above Apex method
@isTest static void method1() {
Profile prof = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User user = new User(firstName = 'test1',lastName = 'test2',profileId = prof.id,
username = 'test@test.com'+Math.random(),email = 'test@test.com',
Alias = 'ts',TimeZoneSidKey = 'Asia/Kolkata',LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',LanguageLocaleKey = 'en_US');
insert user;
System.runAs(user){
List<Account>TestDataAccountList=New List<Account>(TestDataUtility.createAccounts(1));
List<Contact>ContactList=New List<Contact>();
Contact Cons = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry', Title= TestDataUtility.generateRandomString(5),
MailingCity='City67', isPrimary__c=True));
ContactList.add(Cons);
insert ContactList;
List<Event> EventListTest = New List<Event>();
Event Eve = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2020, 12, 9),
EndDateTime =Date.newInstance(2020, 12, 9), Location__c = 'Client Office', WhoId = Cons.Id, OwnerId= User.Id);
EventListTest.add(Eve);
insert EventListTest;
Test.startTest();
List<Contact>UpdatedContactList=New List<Contact>();
Contact Cons1 = (new Contact(firstname=TestDataUtility.generateRandomString(5),lastname='Test',AccountId=TestDataAccountList[0].Id,MailingPostalCode='999999',
MailingStreet= 'Contact1Steet',MailingState='State45', MailingCountry='TestCountry1', Title= 'Title9',
MailingCity='City67', isPrimary__c=True));
UpdatedContactList.add(Cons1);
update UpdatedContactList;
List<Event> UpdatedEventListTest = New List<Event>();
Event Eve1 = new Event(Subject =TestDataUtility.generateRandomString(4) + 'Call', StartDateTime = Date.newInstance(2016, 12, 9),
EndDateTime =Date.newInstance(2016, 12, 9), Location__c = 'Client Office', WhoId = Cons1.Id, OwnerId= User.Id);
UpdatedEventListTest.add(Eve1);
update UpdatedEventListTest;
Test.stopTest();
}
}
- Bharat S
- June 20, 2019
- Like
- 0
- Continue reading or reply