You need to sign in to do that
Don't have an account?
sam
Test method for this apex method
public static void sendContractExpiryEmail(List<Contract> contractList,Map<Id,Contract> oldContractMap){
Set<Id> accIds = new Set<Id>();
List<Account> sentemailAccounts = new List<Account>();
List<Account> accs2Update = new List<Account>();
for(Contract con : contractList){
Contract oldCon = oldContractMap.get(con.Id);
Decimal consumedPercent;
if(con.Adjusted_Contract_Amount__c != null
&&con.Consumed_Amount__c != null
&& con.Adjusted_Contract_Amount__c >0 ){
consumedPercent = (con.Consumed_Amount__c / con.Adjusted_Contract_Amount__c)*100;
}
if(con.Business_Type__c == 'ACR'
&& con.Contracted_ACR_Package__c != null
&& consumedPercent >= Decimal.valueOf(Label.ContractExpiryEmailThresholdPercent)
&& consumedPercent != null){
accIds.add(con.AccountId);
}
}
Boolean expiryByDate = false;
if(accIds.size() >0){
for(Account acc : [SELECT AccountNumber,Name,Balance_Amount__c,Point_Of_Contact_Email__c,
Annual_Contract_Balance__c,Active_Contract__c,Consumed_ACR_Amount__c,
Active_Contract__r.of_Contract_Consumption__c,Active_Contract__r.Consumed_Amount__c,
Adjusted_Contract_Amount__c,Contract_Start_Date__c,Contract_Amount__c,
Consumed_Amount__c,Contract_End_Date__c,ST_Total_Consumable_ACR_Amount__c,
(SELECT ContactId,AccountId,Contact.Email,Roles FROM AccountContactRelations WHERE Roles includes ('Finance','Invoice Recepient')),
Email_Sent_at_85_Consumption__c,Email_Sent_at_90_Consumption__c,Email_Sent_at_95_Consumption__c,
Email_Sent_at_98_Consumption__c,Email_Sent_at_100_Consumption__c,of_Contract_Consumption__c
FROM Account
WHERE Id IN :accIds
AND Contracted_ACR_Package__c != null] ){
if(acc.AccountContactRelations.size() >0){
if(acc.Active_Contract__r.of_Contract_Consumption__c >=85 && acc.Active_Contract__r.of_Contract_Consumption__c <90 && !acc.Email_Sent_at_85_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_85_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=90
&& acc.Active_Contract__r.of_Contract_Consumption__c <95
&& !acc.Email_Sent_at_90_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_90_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=95
&& acc.Active_Contract__r.of_Contract_Consumption__c <98
&& !acc.Email_Sent_at_95_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_95_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=98
&& acc.Active_Contract__r.of_Contract_Consumption__c <100
&& !acc.Email_Sent_at_98_Consumption__c){
system.debug('Account 95-98'+acc.name);
sentemailAccounts.add(acc);
acc.Email_Sent_at_98_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=100
&& !acc.Email_Sent_at_100_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_100_Consumption__c = true;
accs2Update.add(acc);
}
}
}
}
if(accs2Update.size() > 0){
try{
update accs2Update;
}catch(Exception ex){
system.debug('Insert Failed for Consumption Account:::'+ex.getMessage());
}
}
if(sentemailAccounts.size() > 0){
List<Messaging.SingleEmailMessage> mailingList = SendEmailUtility.ContractExpiryEmailSender(Label.ContractConsumptionExpiryEmailTemplateName ,sentemailAccounts,expiryByDate);
Messaging.SendEmailResult[] results = Messaging.sendEmail(mailingList);
}
}
public static void populateinvoice(List<Contract> contractList){
Map<Id,Contract> orderContractMap = new Map<Id,Contract>();
Map<Id,blng__Invoice__c> orderInvoiceMap = new Map<Id,blng__Invoice__c>();
for(Contract con : contractList)
{
if(con.Business_Type__c == 'ACR'){
orderContractMap.put(con.SBQQ__Order__c, con);
}
}
if(!orderContractMap.isEmpty() && orderContractMap.size() > 0)
{
for(blng__Invoice__c inv : [Select Id, blng__Order__c, Name
From blng__Invoice__c
Where blng__Order__c In :orderContractMap.keySet() ])
{
orderInvoiceMap.put(inv.blng__Order__c, inv);
}
for(Contract con : contractList)
{
if(orderInvoiceMap.size() >0
&& orderInvoiceMap != null
&& con.Invoice__c == null
&& orderInvoiceMap.containsKey(con.SBQQ__Order__c)){
con.Invoice__c = orderInvoiceMap.get(con.SBQQ__Order__c).Id;
}
system.debug('invoice number : '+con.Invoice__c);
}
}
}
Set<Id> accIds = new Set<Id>();
List<Account> sentemailAccounts = new List<Account>();
List<Account> accs2Update = new List<Account>();
for(Contract con : contractList){
Contract oldCon = oldContractMap.get(con.Id);
Decimal consumedPercent;
if(con.Adjusted_Contract_Amount__c != null
&&con.Consumed_Amount__c != null
&& con.Adjusted_Contract_Amount__c >0 ){
consumedPercent = (con.Consumed_Amount__c / con.Adjusted_Contract_Amount__c)*100;
}
if(con.Business_Type__c == 'ACR'
&& con.Contracted_ACR_Package__c != null
&& consumedPercent >= Decimal.valueOf(Label.ContractExpiryEmailThresholdPercent)
&& consumedPercent != null){
accIds.add(con.AccountId);
}
}
Boolean expiryByDate = false;
if(accIds.size() >0){
for(Account acc : [SELECT AccountNumber,Name,Balance_Amount__c,Point_Of_Contact_Email__c,
Annual_Contract_Balance__c,Active_Contract__c,Consumed_ACR_Amount__c,
Active_Contract__r.of_Contract_Consumption__c,Active_Contract__r.Consumed_Amount__c,
Adjusted_Contract_Amount__c,Contract_Start_Date__c,Contract_Amount__c,
Consumed_Amount__c,Contract_End_Date__c,ST_Total_Consumable_ACR_Amount__c,
(SELECT ContactId,AccountId,Contact.Email,Roles FROM AccountContactRelations WHERE Roles includes ('Finance','Invoice Recepient')),
Email_Sent_at_85_Consumption__c,Email_Sent_at_90_Consumption__c,Email_Sent_at_95_Consumption__c,
Email_Sent_at_98_Consumption__c,Email_Sent_at_100_Consumption__c,of_Contract_Consumption__c
FROM Account
WHERE Id IN :accIds
AND Contracted_ACR_Package__c != null] ){
if(acc.AccountContactRelations.size() >0){
if(acc.Active_Contract__r.of_Contract_Consumption__c >=85 && acc.Active_Contract__r.of_Contract_Consumption__c <90 && !acc.Email_Sent_at_85_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_85_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=90
&& acc.Active_Contract__r.of_Contract_Consumption__c <95
&& !acc.Email_Sent_at_90_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_90_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=95
&& acc.Active_Contract__r.of_Contract_Consumption__c <98
&& !acc.Email_Sent_at_95_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_95_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=98
&& acc.Active_Contract__r.of_Contract_Consumption__c <100
&& !acc.Email_Sent_at_98_Consumption__c){
system.debug('Account 95-98'+acc.name);
sentemailAccounts.add(acc);
acc.Email_Sent_at_98_Consumption__c = true;
accs2Update.add(acc);
}
else if(acc.Active_Contract__r.of_Contract_Consumption__c >=100
&& !acc.Email_Sent_at_100_Consumption__c){
sentemailAccounts.add(acc);
acc.Email_Sent_at_100_Consumption__c = true;
accs2Update.add(acc);
}
}
}
}
if(accs2Update.size() > 0){
try{
update accs2Update;
}catch(Exception ex){
system.debug('Insert Failed for Consumption Account:::'+ex.getMessage());
}
}
if(sentemailAccounts.size() > 0){
List<Messaging.SingleEmailMessage> mailingList = SendEmailUtility.ContractExpiryEmailSender(Label.ContractConsumptionExpiryEmailTemplateName ,sentemailAccounts,expiryByDate);
Messaging.SendEmailResult[] results = Messaging.sendEmail(mailingList);
}
}
public static void populateinvoice(List<Contract> contractList){
Map<Id,Contract> orderContractMap = new Map<Id,Contract>();
Map<Id,blng__Invoice__c> orderInvoiceMap = new Map<Id,blng__Invoice__c>();
for(Contract con : contractList)
{
if(con.Business_Type__c == 'ACR'){
orderContractMap.put(con.SBQQ__Order__c, con);
}
}
if(!orderContractMap.isEmpty() && orderContractMap.size() > 0)
{
for(blng__Invoice__c inv : [Select Id, blng__Order__c, Name
From blng__Invoice__c
Where blng__Order__c In :orderContractMap.keySet() ])
{
orderInvoiceMap.put(inv.blng__Order__c, inv);
}
for(Contract con : contractList)
{
if(orderInvoiceMap.size() >0
&& orderInvoiceMap != null
&& con.Invoice__c == null
&& orderInvoiceMap.containsKey(con.SBQQ__Order__c)){
con.Invoice__c = orderInvoiceMap.get(con.SBQQ__Order__c).Id;
}
system.debug('invoice number : '+con.Invoice__c);
}
}
}
The code provided in question does not highlight the uncovered lines. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact suggestions. However, the below articles give a good insight into how coverage can be improved
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm
Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5
Deployment related code coverage issues
https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
https://salesforce.stackexchange.com/questions/148894/help-required-to-get-full-code-covered-in-test-class
If this information helps, please mark the answer as best. Thank you