• [Lindsey Kiken]
  • NEWBIE
  • 45 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 2
    Likes Given
  • 8
    Questions
  • 17
    Replies
I have trigger on the standard Case Comment object that performs the following actions:

When a Case Comment is created, it meets our criteria for an internal user, and the First Response fields on the Case Comment's Parent Case are null, stamp the Case Comment's Created By user into the First Response By field on the Case and the Case Comment's Created Date date/time info into the First Response On field on the Case.

The current working code is as follows:
 
trigger FirstResponse on CaseComment (after insert) 
{
    Set<Id> parentIds = new Set<ID>();
    Set<Id> CreatorIds = new Set<ID>();
    Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById(); 
    List<Case> cases = new List<Case>();
    map<id,case> mapIdTocase = new map<id,case>();
    list<Case> lstCaseTobeUpdate = new list<Case>();
    //Get all the Parent Ids (cases) in the Set
    for(CaseComment cc : Trigger.new)
    {
        parentIds.add(cc.ParentId);
        CreatorIds.add(cc.CreatedByID);
    }
    map<id,user> mapCommentUsr = new map<id,user>([select Id,ProfileId,profile.name FROM User where id IN:CreatorIds]); 
    cases = [Select Id, First_Response_On__c, First_Response_By__c, Internal_vs_External__c, RecordTypeID, Origin, of_Comments__c, Status from Case where Id in :parentIds limit 1];
    for(case objcase : cases)
    {
        mapIdTocase.put(objcase.id,objcase);
    }
    for(CaseComment cc : Trigger.new)
    {
//Fire if the Status is New
        if(
            cc.IsPublished == TRUE && 
            mapIdTocase.get(cc.parentid).First_Response_On__c == null &&
            mapIdTocase.get(cc.parentid).Status == 'New' &&
            rt_map.get(mapIdTocase.get(cc.parentid).recordTypeID).getName().containsIgnoreCase('Support Case') &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Customer Community Login User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Customer Community User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Elemental Community Login User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Elemental Community User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Partner Community User' &&
            cc.createdbyid != '00570000003UrOPAA0'
        ){
                mapIdTocase.get(cc.parentid).First_Response_On__c = system.now();
                mapIdTocase.get(cc.parentid).First_Response_By__c = cc.CreatedById;             
                mapIdTocase.get(cc.parentid).Status = 'Open'; 
                lstCaseTobeUpdate.add(mapIdTocase.get(cc.parentid));
        }

        else if(
            cc.IsPublished == TRUE && 
            mapIdTocase.get(cc.parentid).First_Response_On__c == null &&
            mapIdTocase.get(cc.parentid).Status != 'New' &&
            rt_map.get(mapIdTocase.get(cc.parentid).recordTypeID).getName().containsIgnoreCase('Support Case') &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Customer Community Login User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Customer Community User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Elemental Community Login User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Elemental Community User' &&
            mapCommentUsr.get(cc.createdbyid).profile.name != 'Partner Community User' &&
            cc.createdbyid != '00570000003UrOPAA0'
        ){
                mapIdTocase.get(cc.parentid).First_Response_On__c = system.now();
                mapIdTocase.get(cc.parentid).First_Response_By__c = cc.CreatedById;             
                lstCaseTobeUpdate.add(mapIdTocase.get(cc.parentid));
        }
        
            if(lstCaseTobeUpdate != null && lstCaseTobeUpdate.size()>0)
            {
                update lstCaseTobeUpdate;
            }
    }
}

We received a batching error when trying to bulk load case comments, but I unfortunately do not know where to start regarding more efficiently batching this code.

Help is much appreciated!
I am new to apex coding, especially code that involves mult-select picklists, so please bear with me!

I have a multi-select picklist on the Contact object called Reason_Under_Review__c that I am working on coding to automatically populate with values that are depented on specific values within the Contact record to == TRUE. My current code is as follows:
 
trigger ReasonUnderReview on Contact (after insert, after update) {

//(1) define variable for list of all UnderReviewIDs
List<Id> UnderReviewIDs = new List<ID>();
      for(Contact c: trigger.new){
      if(c.Reason_Under_Review__c == null){
          UnderReviewIDs.add(c.id);
          }
}  
//(2) get ids and fields on the Contact record
List<Contact> underreviewreasons = new List<Contact>();
for(Contact c: [SELECT Id,FirstName,LastName,Email,Reason_Under_Review__c
                                                FROM Contact
                                                WHERE Id in: UnderReviewIDs]) {
                                                
//(3) select Under Review Reasons based on fields on the Contact record                                                                                          
        if(c.FirstName==null){c.Reason_Under_Review__c += ';Missing First Name ';}
        if(c.LastName==null){c.Reason_Under_Review__c +=';Missing Last Name ';}
        if(c.Email==null){c.Reason_Under_Review__c +=';Missing Email Address ';}
        c.Reason_Under_Review__c = c.Reason_Under_Review__c.replaceAll(';?null;?','');
        
        underreviewreasons.add(c);
        
//(4) update Reason_Under_Review__c field
}
update underreviewreasons;
}

So far, I see two glaring issues with the code above:
  1. I can only get this code to fire when the Reason_Under_Review__c field == null on Line 6. This means that if the trigger has ever previously fired and populated the field with a value, the trigger will no longer fire any additional updates.
    • If I change Line 6 to say RecordTypeID = [18digit record type ID in my test org] or Reason_Under_Review__c != null, I get a large "ReasonUnderReview: maximum trigger depth exceeded" error when trying to save my test Contact record.
    • I believe that this issue might be due to recursive errors, but am a bit lost on how to fix the issue. How do I resolve this error in my code?
  2. This code is very obviously not setup right now to conditionally delete multi-select picklist values, if the criteria does not meet the condition for the value to exist within the field.
    • A quick example would be that the Contact record previously did not have a FirstName value, and has since updated the record. Per Line 17, the field would have been stamped with a "Missing First Name" value, but I have nothing in place to conditional remove said value when it no longer rings true.
    • How do I conditionally delete values within a multi-select picklist field?
Please let me know if you have any questions and thank you so much for your help regarding this matter!

Cheers,
Lindsey
I have a case apex trigger that is supposed to add a new case team member when the OwnerId is changed. The goal is to have the new OwnerId be entered into the case team and to not alter the case team outside of the OwnerId change. Instead, the new OwnerID is being inserted into the Case Team and is deleting any Case Team Members that were previously in the Case Team, that should remain in the team during the insert, are being deleted.

Please see below for the code:
trigger CaseTeamMember_Owner_Update on Case (after update) {
    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team']; 
    Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById();  
    List<CaseTeamMember> members = new List<CaseTeamMember>();
   
    for (Case c: Trigger.new) {
        List<CaseTeamMember> mem=[Select id from CaseTeamMember where CaseTeamMember.MemberId=:trigger.new[0].OwnerId AND CaseTeamMember.ParentId=:trigger.new[0].Id];
            if(rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case') && mem.size()>0) {
                }
            
        else if(c.CreatedDate != System.now() && c.OwnerId != Trigger.oldMap.get(c.Id).OwnerId && rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case')) {
                members.add(new CaseTeamMember(
                    ParentID = c.Id,
                    MemberID = c.OwnerId,
                    TeamRoleID = role.Id            
                ));           
            }     
        }
    for (Case cas: Trigger.new) {
        List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId];
        delete memdelete;
    } 
          
    if (!members.isEmpty()) {
        insert members;
    }  
}
Removing these line of codes:
for (Case cas: Trigger.new) {
        List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId];
        delete memdelete;
    }
I am fairly certain the following line is causing this issue, but I am not knowledge enough to fix the code:
insert members;
Thoughts?
I have a need to allow Community Portald users to add in comma delimited values to a CC_List__c long text field on the Case object, and then have the following occur:
  • Text string in the CC_List__c is split by the comma symbol
  • Newly added or updated delimited values found in the CC_List__c field are then entered into a child object called CC_List_Object__c as new related records
  • Values found in the related CC_List_Object__c's child list that are no longer listed in the CC_List__c  are deleted
I know that my starting point is listed below, but I am admittedly lost on where to take the trigger from there:
String[]  strs =  valstring.spilt(',');
Thoughts?
I have a Case trigger that is built to automatically add the ContactId on the Case into the Case Team after insert. Unfortunately, the trigger is firing, not generating an error, and not adding the ContactId to the Team.

Please see the code below:
 
trigger CaseTeamMember_Requester_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Requester' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = c.ContactId
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}

The ContactId is a required value within the Support Case record type and a value is supplied during each test. I ran a debug test on Apex Code (Debug), but no errors were provided.

Thoughts on what I am missing?
I am attemping to insert a Queue into a Case Team (after insert) via apex. I have the logice to successfully insert a new Case Team Member, but continue to hit errors when attemting to swap the memberid for a Queue value.

My working member (user) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = c.OwnerId
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
My non-working member (queue) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Group g = [select Name from Group where Type = 'Queue' AND Name = 'Support Unassigned' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = g.Id
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
The error that I am receiving when creating a new case with the member (queue) code in place:
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTeamMember_Owner_Insert caused an unexpected exception, contact your administrator: CaseTeamMember_Owner_Insert: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Member ID: id value of incorrect type: 00G19000000ZNltEAG: [MemberId]: ()

Any thoughts?
 
I have a trigger that successully fires upon the system receiving an inbound email related to a Case record. This trigger's purpose is to stamp the inbound email's body into a text field on the Case object. The working code is as follows:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailBodyMap.get(c.Id);
        
        update c;
        }
}
My users just asked for a new requirement in that the values stamped into the text field "[FromAddres] + BR() + [FromName] + BR() + [Subject] + BR() + BR() + [Body]". Sounded simple enough, but I ran into a silly issue: 

I received the error "Error: Compile Error: unexpected token: '<' at line 34 column 67". I have tried to play around with different ways of adding in <BR/>'s into this field, but continue to run into this same error code. Please see below for the updated work in progress code:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
    Map<Id, String> emailSubjectMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailSubjectMap.put(so.ParentId, so.Subject);
            } 
        }
    Map<Id, String> emailFromAddressMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromAddressMap.put(so.ParentId, so.FromAddress);
            } 
        }       
    Map<Id, String> emailFromNameMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromNameMap.put(so.ParentId, so.FromName);
            } 
        }  
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailFromNameMap.get(c.Id) + <br /> + emailFromAddressMap.get(c.Id) + <br /> + <br /> + emailSubjectMap.get(c.Id) + <br /> + <br /> + emailBodyMap.get(c.Id);
        
        update c;
        }
}
Thoughts?  
I am fairly new to Apex and am having a very difficult time trying to pass my trigger beyond 27%.

My trigger is as follows:
 
trigger LicenseDownloads on Account (after update) {

    for(Account acc:Trigger.new){
        Account oldAcc=Trigger.oldMap.get(acc.Id);
        if(oldAcc.License_Downloads__c!=acc.License_Downloads__c)
            {
                List<Contact>children=[SELECT Id,AccountId,Licensing_Downloads__c from Contact where AccountId = :acc.Id];
                List<Contact>newids=new List<Contact>();
                
                for(Contact con:children){
                    if(con.Licensing_Downloads__c!=acc.License_Downloads__c){
                        con.Licensing_Downloads__c=acc.License_Downloads__c;
                    newids.add(con);
                    }
                }
                if(newids.isEmpty()==false){
                    update newids;
                }
            }
    }
}

My test class is as follows:
 
@IsTest
public class RelationshipTypesInsert_Test {

    public testMethod static void contactInsertion() {
        Account acct = new Account(name='Apex Test Account',BillingState='Oregon',BillingCountry='United States');
        insert acct;

        // Consider setting some address fields as required by copyAddressFields
        Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
        insert c;

        //Make some assertions based on the results of the copyAddressFields call
        
        List<Account> accountsToUpdate = new List<Account>();
        for(Account a : [SELECT Id,Name,Technology_Partner_Tier__c,Account_Status__c,Channel_Partner__c FROM Account WHERE Name = 'Apex test Account']){
            if(a.Account_Status__c == 'Inactive'){
                   a.Technology_Partner_Tier__c = 'Ecosystem';
                   a.Account_Status__c= 'Active';
                   a.Channel_Partner__c= 'Access';
                /** Here put update actions **/
                accountsToUpdate.add(a); 
            }
        }
        update accountsToUpdate;
           
    }

    public testMethod static void contactInsertionFails() {
        Account acct = new Account(name='test account');
        insert acct;

        // Set some fields that will cause an Exception in copyAddressFields
        Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex', email='apex.testing@elementaltechonlogies.com');
        insert c;

        //Make some assertions that errors were added to the Contact

    }

    public testMethod static void bulkifedInsertaion() {
        Account acct = new Account(name='test account',BillingState='Oregon',BillingCountry='United States',Technology_Partner_Tier__c='Ecosystem',Account_Status__c='Active',Channel_Partner__c='Access');
        insert acct;

        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(AccountId=acct.Id,lastname='testing',firstname='apex',email='apex.testing'+x+'@elementaltechnologies.com');
            contactsToCreate.add(ct);
        }

        Test.startTest();
        insert contactsToCreate;
        Test.stopTest();   
    }
}

Anyone have any ideas?
I am new to apex coding, especially code that involves mult-select picklists, so please bear with me!

I have a multi-select picklist on the Contact object called Reason_Under_Review__c that I am working on coding to automatically populate with values that are depented on specific values within the Contact record to == TRUE. My current code is as follows:
 
trigger ReasonUnderReview on Contact (after insert, after update) {

//(1) define variable for list of all UnderReviewIDs
List<Id> UnderReviewIDs = new List<ID>();
      for(Contact c: trigger.new){
      if(c.Reason_Under_Review__c == null){
          UnderReviewIDs.add(c.id);
          }
}  
//(2) get ids and fields on the Contact record
List<Contact> underreviewreasons = new List<Contact>();
for(Contact c: [SELECT Id,FirstName,LastName,Email,Reason_Under_Review__c
                                                FROM Contact
                                                WHERE Id in: UnderReviewIDs]) {
                                                
//(3) select Under Review Reasons based on fields on the Contact record                                                                                          
        if(c.FirstName==null){c.Reason_Under_Review__c += ';Missing First Name ';}
        if(c.LastName==null){c.Reason_Under_Review__c +=';Missing Last Name ';}
        if(c.Email==null){c.Reason_Under_Review__c +=';Missing Email Address ';}
        c.Reason_Under_Review__c = c.Reason_Under_Review__c.replaceAll(';?null;?','');
        
        underreviewreasons.add(c);
        
//(4) update Reason_Under_Review__c field
}
update underreviewreasons;
}

So far, I see two glaring issues with the code above:
  1. I can only get this code to fire when the Reason_Under_Review__c field == null on Line 6. This means that if the trigger has ever previously fired and populated the field with a value, the trigger will no longer fire any additional updates.
    • If I change Line 6 to say RecordTypeID = [18digit record type ID in my test org] or Reason_Under_Review__c != null, I get a large "ReasonUnderReview: maximum trigger depth exceeded" error when trying to save my test Contact record.
    • I believe that this issue might be due to recursive errors, but am a bit lost on how to fix the issue. How do I resolve this error in my code?
  2. This code is very obviously not setup right now to conditionally delete multi-select picklist values, if the criteria does not meet the condition for the value to exist within the field.
    • A quick example would be that the Contact record previously did not have a FirstName value, and has since updated the record. Per Line 17, the field would have been stamped with a "Missing First Name" value, but I have nothing in place to conditional remove said value when it no longer rings true.
    • How do I conditionally delete values within a multi-select picklist field?
Please let me know if you have any questions and thank you so much for your help regarding this matter!

Cheers,
Lindsey
I have a case apex trigger that is supposed to add a new case team member when the OwnerId is changed. The goal is to have the new OwnerId be entered into the case team and to not alter the case team outside of the OwnerId change. Instead, the new OwnerID is being inserted into the Case Team and is deleting any Case Team Members that were previously in the Case Team, that should remain in the team during the insert, are being deleted.

Please see below for the code:
trigger CaseTeamMember_Owner_Update on Case (after update) {
    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team']; 
    Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById();  
    List<CaseTeamMember> members = new List<CaseTeamMember>();
   
    for (Case c: Trigger.new) {
        List<CaseTeamMember> mem=[Select id from CaseTeamMember where CaseTeamMember.MemberId=:trigger.new[0].OwnerId AND CaseTeamMember.ParentId=:trigger.new[0].Id];
            if(rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case') && mem.size()>0) {
                }
            
        else if(c.CreatedDate != System.now() && c.OwnerId != Trigger.oldMap.get(c.Id).OwnerId && rt_map.get(c.recordTypeID).getName().containsIgnoreCase('Support Case')) {
                members.add(new CaseTeamMember(
                    ParentID = c.Id,
                    MemberID = c.OwnerId,
                    TeamRoleID = role.Id            
                ));           
            }     
        }
    for (Case cas: Trigger.new) {
        List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId];
        delete memdelete;
    } 
          
    if (!members.isEmpty()) {
        insert members;
    }  
}
Removing these line of codes:
for (Case cas: Trigger.new) {
        List<CaseTeamMember> memdelete = [Select Id from CaseTeamMember where MemberId=:Trigger.oldMap.get(cas.Id).OwnerId];
        delete memdelete;
    }
I am fairly certain the following line is causing this issue, but I am not knowledge enough to fix the code:
insert members;
Thoughts?
I have a need to allow Community Portald users to add in comma delimited values to a CC_List__c long text field on the Case object, and then have the following occur:
  • Text string in the CC_List__c is split by the comma symbol
  • Newly added or updated delimited values found in the CC_List__c field are then entered into a child object called CC_List_Object__c as new related records
  • Values found in the related CC_List_Object__c's child list that are no longer listed in the CC_List__c  are deleted
I know that my starting point is listed below, but I am admittedly lost on where to take the trigger from there:
String[]  strs =  valstring.spilt(',');
Thoughts?
I have a Case trigger that is built to automatically add the ContactId on the Case into the Case Team after insert. Unfortunately, the trigger is firing, not generating an error, and not adding the ContactId to the Team.

Please see the code below:
 
trigger CaseTeamMember_Requester_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Requester' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = c.ContactId
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}

The ContactId is a required value within the Support Case record type and a value is supplied during each test. I ran a debug test on Apex Code (Debug), but no errors were provided.

Thoughts on what I am missing?
I am attemping to insert a Queue into a Case Team (after insert) via apex. I have the logice to successfully insert a new Case Team Member, but continue to hit errors when attemting to swap the memberid for a Queue value.

My working member (user) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = c.OwnerId
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
My non-working member (queue) code is as follows:
trigger CaseTeamMember_Owner_Insert on Case (after insert) {

    CaseTeamRole role = [select Name from CaseTeamRole where Name = 'Support Team' LIMIT 1];
    Group g = [select Name from Group where Type = 'Queue' AND Name = 'Support Unassigned' LIMIT 1];
    Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>();
    List<Case> cases = [Select Id,OwnerId,ContactId,RecordTypeId,RecordType.Name
                  from Case where id IN :Trigger.newMap.keySet()];
    for (Case c : cases) {
    
      if (c.RecordType.Name == 'Support Case') {
           
        membersToAdd.put(c.Id, 
          new CaseTeamMember(                   
            TeamRoleId = role.Id,
            ParentId = c.Id,                        
            MemberId = g.Id
          )
        );
        if (!membersToAdd.isEmpty()) {
          insert membersToAdd.values();
        }        
      }
    } 
}
The error that I am receiving when creating a new case with the member (queue) code in place:
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTeamMember_Owner_Insert caused an unexpected exception, contact your administrator: CaseTeamMember_Owner_Insert: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Member ID: id value of incorrect type: 00G19000000ZNltEAG: [MemberId]: ()

Any thoughts?
 
I have a trigger that successully fires upon the system receiving an inbound email related to a Case record. This trigger's purpose is to stamp the inbound email's body into a text field on the Case object. The working code is as follows:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailBodyMap.get(c.Id);
        
        update c;
        }
}
My users just asked for a new requirement in that the values stamped into the text field "[FromAddres] + BR() + [FromName] + BR() + [Subject] + BR() + BR() + [Body]". Sounded simple enough, but I ran into a silly issue: 

I received the error "Error: Compile Error: unexpected token: '<' at line 34 column 67". I have tried to play around with different ways of adding in <BR/>'s into this field, but continue to run into this same error code. Please see below for the updated work in progress code:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
    Map<Id, String> emailSubjectMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailSubjectMap.put(so.ParentId, so.Subject);
            } 
        }
    Map<Id, String> emailFromAddressMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromAddressMap.put(so.ParentId, so.FromAddress);
            } 
        }       
    Map<Id, String> emailFromNameMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromNameMap.put(so.ParentId, so.FromName);
            } 
        }  
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailFromNameMap.get(c.Id) + <br /> + emailFromAddressMap.get(c.Id) + <br /> + <br /> + emailSubjectMap.get(c.Id) + <br /> + <br /> + emailBodyMap.get(c.Id);
        
        update c;
        }
}
Thoughts?  
I am fairly new to Apex and am having a very difficult time trying to pass my trigger beyond 27%.

My trigger is as follows:
 
trigger LicenseDownloads on Account (after update) {

    for(Account acc:Trigger.new){
        Account oldAcc=Trigger.oldMap.get(acc.Id);
        if(oldAcc.License_Downloads__c!=acc.License_Downloads__c)
            {
                List<Contact>children=[SELECT Id,AccountId,Licensing_Downloads__c from Contact where AccountId = :acc.Id];
                List<Contact>newids=new List<Contact>();
                
                for(Contact con:children){
                    if(con.Licensing_Downloads__c!=acc.License_Downloads__c){
                        con.Licensing_Downloads__c=acc.License_Downloads__c;
                    newids.add(con);
                    }
                }
                if(newids.isEmpty()==false){
                    update newids;
                }
            }
    }
}

My test class is as follows:
 
@IsTest
public class RelationshipTypesInsert_Test {

    public testMethod static void contactInsertion() {
        Account acct = new Account(name='Apex Test Account',BillingState='Oregon',BillingCountry='United States');
        insert acct;

        // Consider setting some address fields as required by copyAddressFields
        Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex');
        insert c;

        //Make some assertions based on the results of the copyAddressFields call
        
        List<Account> accountsToUpdate = new List<Account>();
        for(Account a : [SELECT Id,Name,Technology_Partner_Tier__c,Account_Status__c,Channel_Partner__c FROM Account WHERE Name = 'Apex test Account']){
            if(a.Account_Status__c == 'Inactive'){
                   a.Technology_Partner_Tier__c = 'Ecosystem';
                   a.Account_Status__c= 'Active';
                   a.Channel_Partner__c= 'Access';
                /** Here put update actions **/
                accountsToUpdate.add(a); 
            }
        }
        update accountsToUpdate;
           
    }

    public testMethod static void contactInsertionFails() {
        Account acct = new Account(name='test account');
        insert acct;

        // Set some fields that will cause an Exception in copyAddressFields
        Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex', email='apex.testing@elementaltechonlogies.com');
        insert c;

        //Make some assertions that errors were added to the Contact

    }

    public testMethod static void bulkifedInsertaion() {
        Account acct = new Account(name='test account',BillingState='Oregon',BillingCountry='United States',Technology_Partner_Tier__c='Ecosystem',Account_Status__c='Active',Channel_Partner__c='Access');
        insert acct;

        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(AccountId=acct.Id,lastname='testing',firstname='apex',email='apex.testing'+x+'@elementaltechnologies.com');
            contactsToCreate.add(ct);
        }

        Test.startTest();
        insert contactsToCreate;
        Test.stopTest();   
    }
}

Anyone have any ideas?
Hello Salesforce Community - First post #1!

Business Goal : 15 minutes after a lead is created from a web-to-lead form I'd like to reassign the lead to a queue if the SDR has not updated the lead status from default value of "Not Started".

Problem :  Process builder doesn't allow you to use conditions based on custom formula fields which would display the created time and compare to the time now.

Solution : Write Apex code, have that code populate a field on the lead object indicating the created date/time + 15 minutes. Then process builder can evaluate the created date time to see if Now () created date is greater than the created date + 15 minutes.  This would allow for the conditions to be met and the record owner updated to the queue.  

Can someone please help me write this code and understand how to push it to the field on the lead object which is not going to be a formula.
Hi All.

Could you please help us to find a solution for our issue?

We need to set a custom URL for our SF Community (not a Force.com site). To do that we performed the following steps:
- registered a custom domain (www.mycompany.com) using the GoDaddy service and added a necessary CNAME there to point this URL to our SF community.
- added a domain for this URL in Salesforce.
- created a Custom URL in Salesforce to point this domain to the community.

Now, if we enter a custom URL in a browser (www.mycompany.com), then it correctly redirects us to the Salesforce community. And it's exactly what we want, except one thing: in address bar of a browser we still see old URL: XXX.force.com.

So, Is it possible to configure this custom URL so that we could see "www.mycompany.com" in an address bar? What should we pay attention for to implement this behavior?

Thank you. Gennadiy.

p.s. One of our assumptions is that we should do something more with CA-certificate and SSL-certificate. If anyone knows more whether they affect on the described problem or not, please push us in a right direction.