• Nathan Wylder 6
  • NEWBIE
  • 0 Points
  • Member since 2020
  • Salesforce Admin
  • Advanced Drainage Systems Inc.

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Help! I'm new to coding and am probably doing this all wrong. How could I optimize the code block below.

What I need to acheive is calculating the difference in business hours that a lead spends in each lead stage from New,  Working, Nurturing, and lastly, Qualified. I wrote the trigger which works but is incomplete (needs to prevent negative values)

I want the trigger (before update) to calls a class & method that does all of this whenever a lead record is updated, but I don't know how. How do I package all of the logic into an apex class that I can call with this trigger?
 
trigger LeadTracking on Lead (before update) {

    //Selecting default business hours (BH) record
    BusinessHours defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true Limit 1];
	//Making sure BH record exists
    if(defaultBH != NULL){
        for(Lead myLead : trigger.new ){
            
            //CALCULATE NEW calculateNewDateTime()
            ////Making sure that New Date Time and Working Date Time are not null
            if(myLead.New_Date_Time__c != NULL ) {
             	if(myLead.Working_Date_Time__c != NULL) {
				//For BH method we assign (BH record id, start time field, end time field)
				decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Working_Date_Time__c );
				//Result from the method is divided by 60*60*100 (milliseconds to be then converted into hours)
				Decimal resultingHours = result/(60*60*1000);
				//Populating result into our custom field & setting number of decimals
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2); 
            
            //calculates difference between New Date Time and Nurturing Date Time
            	} else if (myLead.New_Date_Time__c != NULL && myLead.Working_Date_Time__c == NULL && myLead.Nurturing_Date_Time__c != NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Nurturing_Date_Time__c );
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);  
            
            //Calculates difference between New Date Time and Qualified
            	} else if (myLead.New_Date_Time__c != NULL && myLead.Nurturing_Date_Time__c == NULL && myLead.Working_Date_Time__c == NULL && myLead.Qualified_Date_Time__c != NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Qualified_Date_Time__c );
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);
            
            //if New Date Time is not null but all the other Date Times are Null - then have the calculation be made from NOW()
            	} else if (myLead.New_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == NULL && myLead.Working_Date_Time__c == NULL && myLead.Qualified_Date_Time__c == NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);
            	} //else //if (myLead.New_Date_Time__c == NULL) 
            	//{ myLead.New_Business_Hours_2__c = NULL;
				//}
			}
                
            
            //CALCULATE WORKING
            //calculates difference between working and nurturing if both are not null
            if (myLead.Working_Date_Time__c != Null ){
                if(myLead.Nurturing_Date_Time__c != Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, myLead.Nurturing_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between working and Qualified if Nurturing is null but Working and Qualified are not null   
            } else if (myLead.Working_Date_Time__c != Null && myLead.Qualified_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == NULL) {
				decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, myLead.Qualified_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between working and Now() if working is not null but nurturing and qualified are 
            } else if (myLead.Working_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == Null && myLead.Qualified_Date_Time__c == Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            } //else // (myLead.Working_Date_Time__c == Null) 
            	//{myLead.Working_Business_Hours_2__c = Null; 
                //}
            }
            
            //CALCULATE NURTURING    
            //calculates difference between nurturing and qualified if both are not null
            if (myLead.Nurturing_Date_Time__c != Null) {
                if(myLead.Qualified_Date_Time__c != Null) {
            	decimal result = BusinessHours.diff(defaultBH.Id, myLead.Nurturing_Date_Time__c, myLead.Qualified_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Nurturing_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between nurturing and Now() if qualified is null
            	} else if (myLead.Nurturing_Date_Time__c != Null && myLead.Qualified_Date_Time__c == Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Nurturing_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.Nurturing_Business_Hours_2__c = resultingHours.setScale(2);
           		} //else //(myLead.Nurturing_Date_Time__c == NULL) 
                //{myLead.Nurturing_Business_Hours_2__c = 0.00;
            	//}  
        	}
            //if new, working or nurturing date times are null, there is no calculation of business hours for it
            if (myLead.New_Date_Time__c == NULL) {
                myLead.New_Business_Hours_2__c = NULL;
            }
            if (myLead.Working_Date_Time__c == NULL) {
                myLead.Working_Business_Hours_2__c = NULL;
            }
            if (myLead.Nurturing_Date_Time__c == NULL){
                myLead.Nurturing_Business_Hours_2__c = NULL;
            }
            
    	}    
	}
}



 
 
Help! I'm new to coding and am probably doing this all wrong. How could I optimize the code block below.

What I need to acheive is calculating the difference in business hours that a lead spends in each lead stage from New,  Working, Nurturing, and lastly, Qualified. I wrote the trigger which works but is incomplete (needs to prevent negative values)

I want the trigger (before update) to calls a class & method that does all of this whenever a lead record is updated, but I don't know how. How do I package all of the logic into an apex class that I can call with this trigger?
 
trigger LeadTracking on Lead (before update) {

    //Selecting default business hours (BH) record
    BusinessHours defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true Limit 1];
	//Making sure BH record exists
    if(defaultBH != NULL){
        for(Lead myLead : trigger.new ){
            
            //CALCULATE NEW calculateNewDateTime()
            ////Making sure that New Date Time and Working Date Time are not null
            if(myLead.New_Date_Time__c != NULL ) {
             	if(myLead.Working_Date_Time__c != NULL) {
				//For BH method we assign (BH record id, start time field, end time field)
				decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Working_Date_Time__c );
				//Result from the method is divided by 60*60*100 (milliseconds to be then converted into hours)
				Decimal resultingHours = result/(60*60*1000);
				//Populating result into our custom field & setting number of decimals
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2); 
            
            //calculates difference between New Date Time and Nurturing Date Time
            	} else if (myLead.New_Date_Time__c != NULL && myLead.Working_Date_Time__c == NULL && myLead.Nurturing_Date_Time__c != NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Nurturing_Date_Time__c );
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);  
            
            //Calculates difference between New Date Time and Qualified
            	} else if (myLead.New_Date_Time__c != NULL && myLead.Nurturing_Date_Time__c == NULL && myLead.Working_Date_Time__c == NULL && myLead.Qualified_Date_Time__c != NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, myLead.Qualified_Date_Time__c );
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);
            
            //if New Date Time is not null but all the other Date Times are Null - then have the calculation be made from NOW()
            	} else if (myLead.New_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == NULL && myLead.Working_Date_Time__c == NULL && myLead.Qualified_Date_Time__c == NULL) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.New_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.New_Business_Hours_2__c = resultingHours.setScale(2);
            	} //else //if (myLead.New_Date_Time__c == NULL) 
            	//{ myLead.New_Business_Hours_2__c = NULL;
				//}
			}
                
            
            //CALCULATE WORKING
            //calculates difference between working and nurturing if both are not null
            if (myLead.Working_Date_Time__c != Null ){
                if(myLead.Nurturing_Date_Time__c != Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, myLead.Nurturing_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between working and Qualified if Nurturing is null but Working and Qualified are not null   
            } else if (myLead.Working_Date_Time__c != Null && myLead.Qualified_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == NULL) {
				decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, myLead.Qualified_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between working and Now() if working is not null but nurturing and qualified are 
            } else if (myLead.Working_Date_Time__c != Null && myLead.Nurturing_Date_Time__c == Null && myLead.Qualified_Date_Time__c == Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Working_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.Working_Business_Hours_2__c = resultingHours.setScale(2);
            } //else // (myLead.Working_Date_Time__c == Null) 
            	//{myLead.Working_Business_Hours_2__c = Null; 
                //}
            }
            
            //CALCULATE NURTURING    
            //calculates difference between nurturing and qualified if both are not null
            if (myLead.Nurturing_Date_Time__c != Null) {
                if(myLead.Qualified_Date_Time__c != Null) {
            	decimal result = BusinessHours.diff(defaultBH.Id, myLead.Nurturing_Date_Time__c, myLead.Qualified_Date_Time__c);
				Decimal resultingHours = result/(60*60*1000);
				myLead.Nurturing_Business_Hours_2__c = resultingHours.setScale(2);
            //calculates difference between nurturing and Now() if qualified is null
            	} else if (myLead.Nurturing_Date_Time__c != Null && myLead.Qualified_Date_Time__c == Null) {
                decimal result = BusinessHours.diff(defaultBH.Id, myLead.Nurturing_Date_Time__c, datetime.now());
				Decimal resultingHours = result/(60*60*1000);
				myLead.Nurturing_Business_Hours_2__c = resultingHours.setScale(2);
           		} //else //(myLead.Nurturing_Date_Time__c == NULL) 
                //{myLead.Nurturing_Business_Hours_2__c = 0.00;
            	//}  
        	}
            //if new, working or nurturing date times are null, there is no calculation of business hours for it
            if (myLead.New_Date_Time__c == NULL) {
                myLead.New_Business_Hours_2__c = NULL;
            }
            if (myLead.Working_Date_Time__c == NULL) {
                myLead.Working_Business_Hours_2__c = NULL;
            }
            if (myLead.Nurturing_Date_Time__c == NULL){
                myLead.Nurturing_Business_Hours_2__c = NULL;
            }
            
    	}    
	}
}