• Clayton Hilte
  • NEWBIE
  • 25 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 4
    Replies
I am trying to create a trigger that on insert/update of an Account, it will query Events related to the Account and take the Start Date of the next upcoming Event and stick that into a custom field on the Account. I have created the code below, and it looks to fail at line 18. At this line, I am trying to convert "StartDateTime" from a DateTime field to a regular Date field and then use that in the next upcoming event custom field on Account. Any thoughts on this? CODE BELOW!

trigger nextScheduledEvent on Account (after insert, after update) {
    
    List<String> accountIds = new List<String>();
    List<Account> parentAccounts = new List<Account>();
    List<Event> eventDate = new List<Event>();
    
    for ( Account acc : trigger.new ){
        accountIds.add(acc.id);
    }
    
    eventDate = [SELECT StartDateTime
                  FROM Event
                  WHERE StartDateTime >=:system.now()
                  AND AccountId in :accountIds
                  ORDER BY StartDateTime asc
                  LIMIT 1];
    
    Date d = Date.valueof(eventDate.get(0));
    Date nextDate = date.newInstance(d.year(), d.month(), d.day());
    for ( Account a : trigger.new ){
        a.Next_Scheduled_Event__c = nextDate;
    }

}
I am trying to Query to get the COUNT of Accounts that have at least 1 Event related to it that has the Type = 'Meeting - Client'

When I run the below SOQL it still brings back Accounts that DO NOT have any Events related to them. How do I fix this?

SELECT Id, (SELECT Event.Id FROM Account.Events WHERE Event.Type = 'Meeting - Client') FROM Account
WHERE OwnerId = '00530000005eaqR'
I have a snippet of my main code below. When creating/updating a task, trigger updates fields on the related contact and the related contacts parent account (this is different than using the standard WhoId and AccountId and WhatId).

Trigger works fantastic except when users do mass updates, it will only update the 1st record in the batch. I think this is because I have to iterate over my list.

I have a <LIST> that contains Account Ids and a <MAP> that contains the Account Id and the related fields needed for each ID. How can I edit the trigger below to check if an ID from the List matches an ID in the MAP? Without specifying a specific Location in the LIST with [0], it will not pass syntax: "Incompatible key type LIST<String> for MAP<Id,Account>"

How do I change this, or iterate over the Map incase there are multiple tasks being updated in the same batch?

List<String> lstStoreIds = new List<String>();
List<Account> lstStoreFields = new List<Account>([SELECT Id, OwnerId, ParentId, Last_Contacted_By__c, Last_Contact_Owner__c, Last_Contact_All__c FROM Account WHERE Id in :lstStoreIds]);
    
Map<Id, Account> mapStoreIds = new Map<Id, Account>();
        mapStoreIds.putAll(lstStoreFields);

if( mapStoreIds != null )
                {                    
                    
                    Account store = mapStoreIds.get(lstStoreIds[0]);        //error occurs here            
                    if( store.OwnerId == t.OwnerId )
                    {
                        store.Last_Contacted_By__c = t.OwnerId;
                    }
 
Hi all,

I have a Trigger on the Task object. I am trying to perform a get from a Map using an ID from a previoulsy populated List. The list will only every have 1 ID in it, but I still need to tell it which ID to use. Now I could simply use the WhoId, but without going into detail I can't use that for other reasons based on how the trigger will function. 

Based on the code below, is this correct?
-------------------------------------------------------------------------

trigger ContactAccountLastActivity on Task (after insert, after update) {

    List<String> lstWhoIds = new List<String>();

for( Task t : Trigger.new )
    {
        // Add the "Who" from the task to the lstWhoIds List
        lstWhoIds.add(t.WhoId);    
    }
    
        // Query Contact Object to get Contact fields and create
        // a Map of Contact ID to Contact fields using lstContactFields Query results
    List<Contact> lstContactFields = new List<Contact>([SELECT Id, AccountId, Last_Contacted_Date__c, Last_Contacted_By__c
                                                     FROM Contact
                                                     WHERE Id in :lstWhoIds]);
    Map<String, Contact> mapWhoIds = new Map<String, Contact>(lstContactFields);

for( task t : Trigger.new )
    {
            if( (t.type == 'Telephone In' || t.type == 'Telephone Out' || t.type == 'Visit/Meeting') && t.status == 'Completed' )
            {
                if( Date.valueOf(t.Completed_Date__c) >= mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c )
                {
                    mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
                }
        }
}

mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c  does not provide any errors
mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c  give error: "Incompatible key type LIST<String> for MAP<String, Contact>
 
Hi all -

I have several scenarios in my Test Class, one of which is EXPECTED to fail because of a Validation Rule. I want to test that the validation rule fires and prevents the update, but this also fails the entire Test Class. Is there a way that I can identify in the class that I am expecting there to be a failure for 1 particular TestMethod, but not the rest?

If the validation rule fires - this is good; don't fail the test class.
I am trying to create a trigger that on insert/update of an Account, it will query Events related to the Account and take the Start Date of the next upcoming Event and stick that into a custom field on the Account. I have created the code below, and it looks to fail at line 18. At this line, I am trying to convert "StartDateTime" from a DateTime field to a regular Date field and then use that in the next upcoming event custom field on Account. Any thoughts on this? CODE BELOW!

trigger nextScheduledEvent on Account (after insert, after update) {
    
    List<String> accountIds = new List<String>();
    List<Account> parentAccounts = new List<Account>();
    List<Event> eventDate = new List<Event>();
    
    for ( Account acc : trigger.new ){
        accountIds.add(acc.id);
    }
    
    eventDate = [SELECT StartDateTime
                  FROM Event
                  WHERE StartDateTime >=:system.now()
                  AND AccountId in :accountIds
                  ORDER BY StartDateTime asc
                  LIMIT 1];
    
    Date d = Date.valueof(eventDate.get(0));
    Date nextDate = date.newInstance(d.year(), d.month(), d.day());
    for ( Account a : trigger.new ){
        a.Next_Scheduled_Event__c = nextDate;
    }

}
Hi all,

I have a Trigger on the Task object. I am trying to perform a get from a Map using an ID from a previoulsy populated List. The list will only every have 1 ID in it, but I still need to tell it which ID to use. Now I could simply use the WhoId, but without going into detail I can't use that for other reasons based on how the trigger will function. 

Based on the code below, is this correct?
-------------------------------------------------------------------------

trigger ContactAccountLastActivity on Task (after insert, after update) {

    List<String> lstWhoIds = new List<String>();

for( Task t : Trigger.new )
    {
        // Add the "Who" from the task to the lstWhoIds List
        lstWhoIds.add(t.WhoId);    
    }
    
        // Query Contact Object to get Contact fields and create
        // a Map of Contact ID to Contact fields using lstContactFields Query results
    List<Contact> lstContactFields = new List<Contact>([SELECT Id, AccountId, Last_Contacted_Date__c, Last_Contacted_By__c
                                                     FROM Contact
                                                     WHERE Id in :lstWhoIds]);
    Map<String, Contact> mapWhoIds = new Map<String, Contact>(lstContactFields);

for( task t : Trigger.new )
    {
            if( (t.type == 'Telephone In' || t.type == 'Telephone Out' || t.type == 'Visit/Meeting') && t.status == 'Completed' )
            {
                if( Date.valueOf(t.Completed_Date__c) >= mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c )
                {
                    mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
                }
        }
}

mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c  does not provide any errors
mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c  give error: "Incompatible key type LIST<String> for MAP<String, Contact>
 
Hi all -

I have several scenarios in my Test Class, one of which is EXPECTED to fail because of a Validation Rule. I want to test that the validation rule fires and prevents the update, but this also fails the entire Test Class. Is there a way that I can identify in the class that I am expecting there to be a failure for 1 particular TestMethod, but not the rest?

If the validation rule fires - this is good; don't fail the test class.
Hi,

How to make a standard field on my custom object "Read only" in salesforce


Help me how to achieve this