• Nathan Wylder 6
• NEWBIE
• 0 Points
• Member since 2020

• Chatter
Feed
• 0
• 0
• 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){

//CALCULATE NEW calculateNewDateTime()
////Making sure that New Date Time and Working Date Time are not null
//For BH method we assign (BH record id, start time field, end time field)
//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

//calculates difference between New Date Time and Nurturing Date Time
Decimal resultingHours = result/(60*60*1000);

//Calculates difference between New Date Time and Qualified
Decimal resultingHours = result/(60*60*1000);

//if New Date Time is not null but all the other Date Times are Null - then have the calculation be made from NOW()
Decimal resultingHours = result/(60*60*1000);
} //else //if (myLead.New_Date_Time__c == NULL)
//}
}

//CALCULATE WORKING
//calculates difference between working and nurturing if both are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between working and Qualified if Nurturing is null but Working and Qualified are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between working and Now() if working is not null but nurturing and qualified are
Decimal resultingHours = result/(60*60*1000);
} //else // (myLead.Working_Date_Time__c == Null)
//}
}

//CALCULATE NURTURING
//calculates difference between nurturing and qualified if both are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between nurturing and Now() if qualified is null
Decimal resultingHours = result/(60*60*1000);
//}
}
//if new, working or nurturing date times are null, there is no calculation of business hours for it
}
}
}

}
}
}```

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){

//CALCULATE NEW calculateNewDateTime()
////Making sure that New Date Time and Working Date Time are not null
//For BH method we assign (BH record id, start time field, end time field)
//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

//calculates difference between New Date Time and Nurturing Date Time
Decimal resultingHours = result/(60*60*1000);

//Calculates difference between New Date Time and Qualified
Decimal resultingHours = result/(60*60*1000);

//if New Date Time is not null but all the other Date Times are Null - then have the calculation be made from NOW()
Decimal resultingHours = result/(60*60*1000);
} //else //if (myLead.New_Date_Time__c == NULL)
//}
}

//CALCULATE WORKING
//calculates difference between working and nurturing if both are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between working and Qualified if Nurturing is null but Working and Qualified are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between working and Now() if working is not null but nurturing and qualified are
Decimal resultingHours = result/(60*60*1000);
} //else // (myLead.Working_Date_Time__c == Null)
//}
}

//CALCULATE NURTURING
//calculates difference between nurturing and qualified if both are not null
Decimal resultingHours = result/(60*60*1000);
//calculates difference between nurturing and Now() if qualified is null
Decimal resultingHours = result/(60*60*1000);
//}
}
//if new, working or nurturing date times are null, there is no calculation of business hours for it
}