• Hall Anthony
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 0
    Replies
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!!

User-added image
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});
  }
}
 
Hi All,
We have a trigger on our case object that summarizes a number field on each related activity when that case closes.  Problem is the trigger only works for some employees for some reason. I have a debug file for a user when the trigger fails but the file its too long to post on this forum (I can only paste 32,000 characters in a post. What should i do to go about solving this issue? I have a feeling the issue is a very simple fix for someone that uderstands apex code well.

Thanks!





 
Do standard fields have 15 digit reference id's? I want to reference the Case "account name" field in a URL button but the field address is 

https://cs45.salesforce.com/_ui/common/config/field/StandardFieldAttributes/d?id=Account&type=Case&retURL=%2Fp%2Fsetup%2Flayout%2FLayoutFieldList%3Ftype%3DCase%26retURL%3D%252Fui%252Fsetup%252FSetup%253Fsetupid%253DCase%26setupid%3DCaseFields&setupid=CaseFields

Thanks!
Hi All,
I recently tried to save a case in our sandbox to test a new case object field and an error message popped up. I have been working in this sandbox since October and never had an error before. Recently I refreshed our sandbox for the Summer 16 release and I'm not sure if that possibly had an effect. Below is a screen shot of the error that mentioned "line 66" and a screenshot of the casebefore line 66. I'm at a lose of how to handle this because i dont have apex code experience. Thanks for the help, I really appreciate it!
User-added image
User-added image