You need to sign in to do that
Don't have an account?
Hall Anthony
CaseBefore Trigger needs to work for all profiles, not just one.
Hi All,
We have an Apex Trigger called CaseBefore which has a trigger directed at one profile "Member services" and i need the trigger to work for everyone. I am not to familiar with apex code and this trigger was created when salesforce was implemented at our company before I was working here. I'm sure this is a simple change to the coding
Line 33 describes the function: We calculate the time it takes to complete tasks related to a case. "Made by Member Services Profile User attached to the case" needs to be changed to "Made by anyone"
Again I'm not familiar with Apex code but I'm hoping this isnt to difficult of a correction. I copy/pasted the code below the file
Thanks a lot for the help!!
trigger CaseBefore on Case (before insert, before update) {
if (Trigger.isInsert) {
setAccountId();
manageWebToCase();
}
if (Trigger.isUpdate) {
setAccountId();
setRequestTime();
}
/* PRIVATE METHODS */
/*Sets accountId field using the property's management company id*/
private void setAccountId() {
Set<Id> propIds = new Set<Id>();
for (Case c : Trigger.New) {
if (c.AccountId == null && c.Property__c != null) {
propIds.add(c.Property__c);
}
}
Map<Id, Property__c> propMap = new Map<Id, Property__c>([Select Id, Management_Company__c from Property__c where Id in :propIds]);
for (Case c : Trigger.New) {
if (c.AccountId == null && c.Property__c != null) {
Property__c prop = propMap.get(c.Property__c);
if (prop != null) {
c.AccountId = prop.Management_Company__c;
}
}
}
}
//Populates request time field on case object by summing up all Task.Time_To_Complete__c made by a
//"Member Services" Profile user attached to the case.
private void setRequestTime() {
System.debug('start');
Map<Id, Decimal > timeMap = new Map<Id, Decimal >();
for (Task t : [Select Id, WhatId, Time_To_Complete__c from Task where WhatId in :Trigger.NewMap.keySet()
and (Owner.Profile.Name='Member Services' or Owner.Profile.Name='Member Services Manager') and IsDeleted=false ALL ROWS]) {
System.debug('t: ' + t);
Decimal cnt = timeMap.get(t.WhatId);
if (cnt == null) cnt = 0;
if (t.Time_To_Complete__c != null) {
cnt += t.Time_To_Complete__c;
}
System.debug('cnt: ' + cnt);
timeMap.put(t.WhatId, cnt);
}
System.debug('timeMap: ' + timeMap);
for (Case c : Trigger.New) {
Case oldCase = Trigger.OldMap.get(c.Id);
System.debug('c.status: ' + c.status + ', oldCase status: ' + oldCase.status);
if (c.status == 'Closed' && oldCase.status != 'Closed') {
System.debug('condition met');
Decimal cnt = timeMap.get(c.Id);
System.debug('cnt: ' + cnt);
c.Request_Time_Min__c = cnt;
}
}
}
private void manageWebToCase() {
//getting the default User which it will be the Owner for the inserted Case
String defaultCaseUser = KeyValueStore__c.getAll().get('WebToCaseOwnerUsername').TextValue__c;
User defaultUser = [SELECT Id FROM User WHERE Username = :defaultCaseUser limit 1];
//getting the Usernames of the inserted Cases into a Set
Set<String> caseUserNames = new Set<String>();
for(Case c : trigger.new) {
if (c.Created_By_Web_to_Case__c && c.Contact_Username__c != null) {
caseUserNames.add(c.Contact_Username__c);
}
}
//getting the information from the Contact for the associated Usernames
List<Contact> contactRecordList = [SELECT Id, AccountId, User_Name__c, Primary_Location__c FROM Contact
WHERE User_Name__c IN :caseUserNames];
Map<String,Contact> contactMap = new Map<String, Contact>(); //key->contact username
for (Contact contactRecord : contactRecordList) {
contactMap.put(contactRecord.User_Name__c, contactRecord);
}
//getting the default RecordType which it will be the RecordType for the inserted Case
String defaultRT = KeyValueStore__c.getAll().get('WebToCaseRecordType').TextValue__c;
RecordType rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Case' AND DeveloperName = :defaultRT];
//getting the default Email to which it will be sent an email if inserted Case doesn't have a Username
System.debug('xxx:::' + KeyValueStore__c.getAll().get('WebToCaseErrorNotifyEmail').TextValue__c);
String defaultEmailCase = KeyValueStore__c.getAll().get('WebToCaseErrorNotifyEmail').TextValue__c;
for(Case c : trigger.new) {
if (c.Created_By_Web_to_Case__c == true) {
c.OwnerId = defaultUser.Id;
c.Origin = 'Web';
c.RecordTypeId = rt.Id;
//if a Case with username is inserted -> the Account's field from Case should be populated with the values from Contact with specified username
if (c.Contact_Username__c != null) {
Contact cont = contactMap.get(c.Contact_Username__c);
if (cont != null) {
c.AccountId = cont.AccountId;
c.ContactId = cont.Id;
c.Property__c = cont.Primary_Location__c;
}
} else {
sendEmail(defaultEmailCase, c);
}
}
}
}
private void sendEmail(String emailAddress, Case c) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[1];
toAddresses[0] = emailAddress;
email.setSubject( 'The Case you are trying to insert does not have a Username' );
email.setToAddresses( toAddresses );
email.setPlainTextBody( 'The Contact_Username__c field for the Case you are trying to insert is empty. \n\n Created_By_Web_to_Case__c = true \n OwnerId = ' + c.OwnerId + '\n Origin = ' + c.Origin + '\n RecordTypeId = ' + c.RecordTypeId);
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
We have an Apex Trigger called CaseBefore which has a trigger directed at one profile "Member services" and i need the trigger to work for everyone. I am not to familiar with apex code and this trigger was created when salesforce was implemented at our company before I was working here. I'm sure this is a simple change to the coding
Line 33 describes the function: We calculate the time it takes to complete tasks related to a case. "Made by Member Services Profile User attached to the case" needs to be changed to "Made by anyone"
Again I'm not familiar with Apex code but I'm hoping this isnt to difficult of a correction. I copy/pasted the code below the file
Thanks a lot for the help!!
trigger CaseBefore on Case (before insert, before update) {
if (Trigger.isInsert) {
setAccountId();
manageWebToCase();
}
if (Trigger.isUpdate) {
setAccountId();
setRequestTime();
}
/* PRIVATE METHODS */
/*Sets accountId field using the property's management company id*/
private void setAccountId() {
Set<Id> propIds = new Set<Id>();
for (Case c : Trigger.New) {
if (c.AccountId == null && c.Property__c != null) {
propIds.add(c.Property__c);
}
}
Map<Id, Property__c> propMap = new Map<Id, Property__c>([Select Id, Management_Company__c from Property__c where Id in :propIds]);
for (Case c : Trigger.New) {
if (c.AccountId == null && c.Property__c != null) {
Property__c prop = propMap.get(c.Property__c);
if (prop != null) {
c.AccountId = prop.Management_Company__c;
}
}
}
}
//Populates request time field on case object by summing up all Task.Time_To_Complete__c made by a
//"Member Services" Profile user attached to the case.
private void setRequestTime() {
System.debug('start');
Map<Id, Decimal > timeMap = new Map<Id, Decimal >();
for (Task t : [Select Id, WhatId, Time_To_Complete__c from Task where WhatId in :Trigger.NewMap.keySet()
and (Owner.Profile.Name='Member Services' or Owner.Profile.Name='Member Services Manager') and IsDeleted=false ALL ROWS]) {
System.debug('t: ' + t);
Decimal cnt = timeMap.get(t.WhatId);
if (cnt == null) cnt = 0;
if (t.Time_To_Complete__c != null) {
cnt += t.Time_To_Complete__c;
}
System.debug('cnt: ' + cnt);
timeMap.put(t.WhatId, cnt);
}
System.debug('timeMap: ' + timeMap);
for (Case c : Trigger.New) {
Case oldCase = Trigger.OldMap.get(c.Id);
System.debug('c.status: ' + c.status + ', oldCase status: ' + oldCase.status);
if (c.status == 'Closed' && oldCase.status != 'Closed') {
System.debug('condition met');
Decimal cnt = timeMap.get(c.Id);
System.debug('cnt: ' + cnt);
c.Request_Time_Min__c = cnt;
}
}
}
private void manageWebToCase() {
//getting the default User which it will be the Owner for the inserted Case
String defaultCaseUser = KeyValueStore__c.getAll().get('WebToCaseOwnerUsername').TextValue__c;
User defaultUser = [SELECT Id FROM User WHERE Username = :defaultCaseUser limit 1];
//getting the Usernames of the inserted Cases into a Set
Set<String> caseUserNames = new Set<String>();
for(Case c : trigger.new) {
if (c.Created_By_Web_to_Case__c && c.Contact_Username__c != null) {
caseUserNames.add(c.Contact_Username__c);
}
}
//getting the information from the Contact for the associated Usernames
List<Contact> contactRecordList = [SELECT Id, AccountId, User_Name__c, Primary_Location__c FROM Contact
WHERE User_Name__c IN :caseUserNames];
Map<String,Contact> contactMap = new Map<String, Contact>(); //key->contact username
for (Contact contactRecord : contactRecordList) {
contactMap.put(contactRecord.User_Name__c, contactRecord);
}
//getting the default RecordType which it will be the RecordType for the inserted Case
String defaultRT = KeyValueStore__c.getAll().get('WebToCaseRecordType').TextValue__c;
RecordType rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Case' AND DeveloperName = :defaultRT];
//getting the default Email to which it will be sent an email if inserted Case doesn't have a Username
System.debug('xxx:::' + KeyValueStore__c.getAll().get('WebToCaseErrorNotifyEmail').TextValue__c);
String defaultEmailCase = KeyValueStore__c.getAll().get('WebToCaseErrorNotifyEmail').TextValue__c;
for(Case c : trigger.new) {
if (c.Created_By_Web_to_Case__c == true) {
c.OwnerId = defaultUser.Id;
c.Origin = 'Web';
c.RecordTypeId = rt.Id;
//if a Case with username is inserted -> the Account's field from Case should be populated with the values from Contact with specified username
if (c.Contact_Username__c != null) {
Contact cont = contactMap.get(c.Contact_Username__c);
if (cont != null) {
c.AccountId = cont.AccountId;
c.ContactId = cont.Id;
c.Property__c = cont.Primary_Location__c;
}
} else {
sendEmail(defaultEmailCase, c);
}
}
}
}
private void sendEmail(String emailAddress, Case c) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[1];
toAddresses[0] = emailAddress;
email.setSubject( 'The Case you are trying to insert does not have a Username' );
email.setToAddresses( toAddresses );
email.setPlainTextBody( 'The Contact_Username__c field for the Case you are trying to insert is empty. \n\n Created_By_Web_to_Case__c = true \n OwnerId = ' + c.OwnerId + '\n Origin = ' + c.Origin + '\n RecordTypeId = ' + c.RecordTypeId);
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Waqar Hussain SF
Use below code
Waqar Hussain SF
Please don't forget to mark best answer, If the issue is solved.