• Claire Sunderland 1
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 6
    Replies
I have a flow that is assigning a territory based on the billing address information on the account. I have the 'when no records are returned, set specified variables to null' however, when the critera meets a condition in which no record exists it just leaves the existing value and doesn't change it to null.

User-added image
I've built a flow on Opportunities to date stamp a date field every time the stage updates so we can more easily report on time in each stage without using stage history reporting.

I want the date field to stamp only the first time the Opportunity moved to this stage, not to update it it happens to go to a stage multiple times.

Even though in the 'Get Records' component I'm only requesting records that have a null date. When it moves to assign the value it's overwriting date fields so it's populating on Opportunities where the date field is not null.

This is the first flow I've written so any help is appreciated. Thank you!

Below is the flow components:
User-added image
User-added image
User-added image
User-added image
User-added image
 
We need to build a date/time field that calculates what the date and time would be 4 hours after a Case is opened

i.e.
  • Case is opened 10/19/2020 4:00pm
  • Business hours end at 5:00pm
  • Date/time field of 4 business hours later should be 10/20/2020 11:00am 
How can we build a formula to give us that date/time
 
Hi - I am an admin that is trying to code something into our instance to calculate business hours between 2 date/time fields based on owner location to account for time zone changes. I was able to find and tweak the code from this website (https://www.tech-vision.us/salesforce-the-number-of-business-hours-between-two-date-time-fields/). I then wrote a Test Class but am only able to get to 55% code coverage and cannot figure out why. 

Trigger:
trigger Demo_Response_Biz_Hours_Time_Zones on Lead (before insert, before update) {
   
    //Selecting all active BusinessHours records
    List<BusinessHours> BusinessHours_List = [SELECT Id, Name FROM BusinessHours WHERE IsActive = true];
   
    //Adding map where BusinessHours Name will be the key
    Map<String, BusinessHours> LocationName_BusinessHours_Map = new Map<String, BusinessHours> ();
   
    //Filling the map | Location Name, BusinessHours Object
    for (BusinessHours b : BusinessHours_List) {
        LocationName_BusinessHours_Map.put(b.Name, b);
    }
    for (Lead leadObj : Trigger.new) {
        
        //This part of a trigger only works when we add new records
        if (Trigger.isInsert) {
            
            //We check if both Start Time & Completed Time fields are not empty
            if (leadObj.Demo_Form_Submission_Date__c != NULL && leadObj.First_Activity_Most_Recent_Demo_Request__c != NULL) {
                
                //Then we do another check if current record is tied to a location that has business hours record
                if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                } else{
                    
                    //Just as a safety net, let's use default business hours in case there is no match between Location name and BH name
                    if(LocationName_BusinessHours_Map.get('Default').Id != NULL){
                        decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get('Default').Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                        Decimal resultingHours = result / (60 * 60 * 1000);
                        leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                    }
                }
            }
        } else if (Trigger.isUpdate) {
            
            //This part of a trigger only works when we perform update
           
            //Checking if Start Time or Completed Time has been changed & if they are not empty
            if ((leadObj.Demo_Form_Submission_Date__c != NULL && leadObj.First_Activity_Most_Recent_Demo_Request__c != NULL) && ((leadObj.Demo_Form_Submission_Date__c != Trigger.oldMap.get(leadObj.id).Demo_Form_Submission_Date__c) || (leadObj.First_Activity_Most_Recent_Demo_Request__c != Trigger.oldMap.get(leadObj.id).First_Activity_Most_Recent_Demo_Request__c))) {
                if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                } else{
                    if(LocationName_BusinessHours_Map.get('Default').Id != NULL){
                        decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get('Default').Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                        Decimal resultingHours = result / (60 * 60 * 1000);
                        leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                    }
                }
            } else if (leadObj.Demo_Form_Submission_Date__c == NULL || leadObj.First_Activity_Most_Recent_Demo_Request__c == NULL) {
                
                //Another safety net that will reset Hours spent in case Start Time OR Completed time becomes empty after update.
               
                //That way you won't have old & inaccurate data from past calculations.
                leadObj.APEXTEST_Demo_Response_Time_Zones__c = NULL;
            }
        }
    }
}

Test Class:
@isTest

public class TestDemoResponseTime {

    Static testmethod void LeadTest() {
    
        //create a lead
        Lead leadObj = new Lead();
        leadObj.LastName = 'Test';
        leadObj.Company = 'Test ABC';
        leadObj.LeadSource = 'Sales Prospecting';
        leadObj.Lead_Source_Detail__c = 'Apex Test Class';
        leadObj.OwnerID = '0054G000008UaurQAC';
        leadObj.Demo_Form_Submission_Date__c = datetime.newInstance(2020, 04, 15, 14, 00, 00);
                
        insert leadObj;
        
        //business hours
        BusinessHours b = [SELECT Id FROM BusinessHours WHERE Name = 'Denver'];
        
        //update the lead
        leadObj = [SELECT Id, LastName, Company, LeadSource, Lead_Source_Detail__c, OwnerID, Owner_Location__c, Demo_Form_Submission_Date__c, First_Activity_Most_Recent_Demo_Request__c FROM Lead WHERE Id != NULL];
        leadObj.First_Activity_Most_Recent_Demo_Request__c = datetime.newInstance(2020, 04, 16, 19, 00, 00);
        
        update leadObj;
   }
}

I've checked the developer console and the sections that aren't covered are:
if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);

Does anyone have any ideas on what I'm missing? Thank you!
I am a Salesforce admin working to finish a Trigger in our Sandbox for tesitng. I have an Apex Trigger that is being used to rollup certain activities onto the lead record. When I try to convert the lead I'm hitting an error that I'm interpreting is because it's pushing an update to the activities on the record and then looking for the Lead.Id but this no longer exists. Can someone show me how to trigger only if the Lead is not converted?

Error message:

Error: There was an error converting the lead. Please resolve the following error and try again: ActivityRollups: execution of AfterUpdate caused by: System.SObjectException: Invalid Id for Lead 
I am an admin attempting to modify a prior LREngine class in our Salesforce to rollup activity records onto Leads. I want to only rollup activities that meet certain criteria and am able to do so fine with one criteria but when I add 2 criteria I get a 'Constructor not defined' error so it seems the code doesn't allow more than one field result entry. Does anyone know a way around that so I can add multiple criteria?

Code is below. I'm getting an error on the second critiera where 'RecordType = \'Demo\''
public class ActivityRollups {



    public static void doRollups(Task[] objects) {


        LREngine.Context ctx = new LREngine.Context(Lead.SobjectType, // parent object
                                               Task.SobjectType, // child object
                                               Schema.SObjectType.Task.fields.WhoID // relationship field name
                                               );     

        ctx.add(
               new LREngine.RollupSummaryField(
                                               Schema.SObjectType.Lead.fields.Demos_Attended__c,
                                               Schema.SObjectType.Task.fields.id,
                                               LREngine.RollupOperation.Count 
                                            ));


    
        LREngine.Context ctxDemoAttended = new LREngine.Context(Lead.SobjectType, // parent object
                                               Task.SobjectType, // child object
                                                Schema.SObjectType.Task.fields.WhoID, // relationship field name
                                                'Call_Status__c = \'Attended\'', // filter / field result
                                                'RecordType = \'Demo\'' // filter / field result
                                                );


         Sobject[] masters = LREngine.rollUp(ctx, objects);
         Sobject[] mastersDemoAttended = LREngine.rollUp(ctxDemoAttended, objects);


        updateRecords(masters);
        masters = null;


        updateRecords(mastersDemoAttended);
        mastersDemoAttended = null;

    }


    private static void updateRecords(Sobject[] pMasters) {
        try {
            update pMasters;
        }
        catch (System.DMLException ex) {
            String s = ex.getMessage();
            if (s.containsIgnoreCase('FIELD_CUSTOM_VALIDATION_EXCEPTION') && !Trigger.IsDelete) {
                Trigger.New[0].addError( s.substring( s.indexof('FIELD_CUSTOM_VALIDATION_EXCEPTION')+35,  s.indexof(': [') ) );
                return;
            } else {
                throw ex;
            }
        }
    }


}

 
I have a flow that is assigning a territory based on the billing address information on the account. I have the 'when no records are returned, set specified variables to null' however, when the critera meets a condition in which no record exists it just leaves the existing value and doesn't change it to null.

User-added image
I've built a flow on Opportunities to date stamp a date field every time the stage updates so we can more easily report on time in each stage without using stage history reporting.

I want the date field to stamp only the first time the Opportunity moved to this stage, not to update it it happens to go to a stage multiple times.

Even though in the 'Get Records' component I'm only requesting records that have a null date. When it moves to assign the value it's overwriting date fields so it's populating on Opportunities where the date field is not null.

This is the first flow I've written so any help is appreciated. Thank you!

Below is the flow components:
User-added image
User-added image
User-added image
User-added image
User-added image
 
Hi - I am an admin that is trying to code something into our instance to calculate business hours between 2 date/time fields based on owner location to account for time zone changes. I was able to find and tweak the code from this website (https://www.tech-vision.us/salesforce-the-number-of-business-hours-between-two-date-time-fields/). I then wrote a Test Class but am only able to get to 55% code coverage and cannot figure out why. 

Trigger:
trigger Demo_Response_Biz_Hours_Time_Zones on Lead (before insert, before update) {
   
    //Selecting all active BusinessHours records
    List<BusinessHours> BusinessHours_List = [SELECT Id, Name FROM BusinessHours WHERE IsActive = true];
   
    //Adding map where BusinessHours Name will be the key
    Map<String, BusinessHours> LocationName_BusinessHours_Map = new Map<String, BusinessHours> ();
   
    //Filling the map | Location Name, BusinessHours Object
    for (BusinessHours b : BusinessHours_List) {
        LocationName_BusinessHours_Map.put(b.Name, b);
    }
    for (Lead leadObj : Trigger.new) {
        
        //This part of a trigger only works when we add new records
        if (Trigger.isInsert) {
            
            //We check if both Start Time & Completed Time fields are not empty
            if (leadObj.Demo_Form_Submission_Date__c != NULL && leadObj.First_Activity_Most_Recent_Demo_Request__c != NULL) {
                
                //Then we do another check if current record is tied to a location that has business hours record
                if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                } else{
                    
                    //Just as a safety net, let's use default business hours in case there is no match between Location name and BH name
                    if(LocationName_BusinessHours_Map.get('Default').Id != NULL){
                        decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get('Default').Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                        Decimal resultingHours = result / (60 * 60 * 1000);
                        leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                    }
                }
            }
        } else if (Trigger.isUpdate) {
            
            //This part of a trigger only works when we perform update
           
            //Checking if Start Time or Completed Time has been changed & if they are not empty
            if ((leadObj.Demo_Form_Submission_Date__c != NULL && leadObj.First_Activity_Most_Recent_Demo_Request__c != NULL) && ((leadObj.Demo_Form_Submission_Date__c != Trigger.oldMap.get(leadObj.id).Demo_Form_Submission_Date__c) || (leadObj.First_Activity_Most_Recent_Demo_Request__c != Trigger.oldMap.get(leadObj.id).First_Activity_Most_Recent_Demo_Request__c))) {
                if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                } else{
                    if(LocationName_BusinessHours_Map.get('Default').Id != NULL){
                        decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get('Default').Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                        Decimal resultingHours = result / (60 * 60 * 1000);
                        leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);
                    }
                }
            } else if (leadObj.Demo_Form_Submission_Date__c == NULL || leadObj.First_Activity_Most_Recent_Demo_Request__c == NULL) {
                
                //Another safety net that will reset Hours spent in case Start Time OR Completed time becomes empty after update.
               
                //That way you won't have old & inaccurate data from past calculations.
                leadObj.APEXTEST_Demo_Response_Time_Zones__c = NULL;
            }
        }
    }
}

Test Class:
@isTest

public class TestDemoResponseTime {

    Static testmethod void LeadTest() {
    
        //create a lead
        Lead leadObj = new Lead();
        leadObj.LastName = 'Test';
        leadObj.Company = 'Test ABC';
        leadObj.LeadSource = 'Sales Prospecting';
        leadObj.Lead_Source_Detail__c = 'Apex Test Class';
        leadObj.OwnerID = '0054G000008UaurQAC';
        leadObj.Demo_Form_Submission_Date__c = datetime.newInstance(2020, 04, 15, 14, 00, 00);
                
        insert leadObj;
        
        //business hours
        BusinessHours b = [SELECT Id FROM BusinessHours WHERE Name = 'Denver'];
        
        //update the lead
        leadObj = [SELECT Id, LastName, Company, LeadSource, Lead_Source_Detail__c, OwnerID, Owner_Location__c, Demo_Form_Submission_Date__c, First_Activity_Most_Recent_Demo_Request__c FROM Lead WHERE Id != NULL];
        leadObj.First_Activity_Most_Recent_Demo_Request__c = datetime.newInstance(2020, 04, 16, 19, 00, 00);
        
        update leadObj;
   }
}

I've checked the developer console and the sections that aren't covered are:
if (LocationName_BusinessHours_Map.containsKey(leadObj.Owner_Location__c)) {
                    decimal result = BusinessHours.diff(LocationName_BusinessHours_Map.get(leadObj.Owner_Location__c).Id, leadObj.Demo_Form_Submission_Date__c, leadObj.First_Activity_Most_Recent_Demo_Request__c);
                    Decimal resultingHours = result / (60 * 60 * 1000);
                    leadObj.APEXTEST_Demo_Response_Time_Zones__c = resultingHours.setScale(1);

Does anyone have any ideas on what I'm missing? Thank you!
I am a Salesforce admin working to finish a Trigger in our Sandbox for tesitng. I have an Apex Trigger that is being used to rollup certain activities onto the lead record. When I try to convert the lead I'm hitting an error that I'm interpreting is because it's pushing an update to the activities on the record and then looking for the Lead.Id but this no longer exists. Can someone show me how to trigger only if the Lead is not converted?

Error message:

Error: There was an error converting the lead. Please resolve the following error and try again: ActivityRollups: execution of AfterUpdate caused by: System.SObjectException: Invalid Id for Lead