You need to sign in to do that
Don't have an account?
Raju Chi 5
test class help for trigger helper
Hi Team,
Please help me how to write test class for below helper.
public class Risk_Waiver_Aggreement_TriggerHelper {
// Populating RWA number to all child ERP device data using below method.
Public void updateParentRWA(List<Agreement__c> AgList){
List<Device__c> erplst = [SELECT Name, RWA__c,Base_Device__c,Customer_Short_Name__c,RWA_Single_Record__c,Base_Device_Unique_Name__c,Product_Family_Unique_Name__c FROM Device__c
where Base_Device_Unique_Name__c=: AgList[0].Base_Device_Unique_Name__c or Product_Family_Unique_Name__c=: AgList[0].Product_Family_Unique_Name__c
or Customer_Short_Name__c =: AgList[0].Account_Short_Name__c];
system.debug('erplst'+erplst.size());
Device__c erprecord = [select id,RWA__c from Device__c where id =:AgList[0].Device__c limit 1];
List<Device__c> erplist = new List<Device__c>();
for (Agreement__c rw : AgList){
for(Device__c erp : erplst) {
if(rw.status__c == 'Submitted'){
if(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' && rw.Apply_RWA_All_Product_family__c =='No'){
if(rw.Base_Device_Unique_Name__c == erp.Base_Device_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
} else{
if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'Yes' && rw.Apply_RWA_All_Product_family__c =='Yes'){
if(rw.Product_Family_Unique_Name__c == erp.Product_Family_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}else if(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='No'){
if(rw.Base_Device_Unique_Name__c == erp.Base_Device_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}else if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='Yes'){
if(rw.Product_Family_Unique_Name__c == erp.Product_Family_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}
}
}
}
if(rw.Status__c == 'Submitted'){
if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='No'){
erprecord.RWA__c = rw.id;
erplist.add(erprecord);
}else If(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'yes' && rw.Apply_RWA_All_Product_family__c =='No'){
erprecord.RWA__c = rw.id;
erplist.add(erprecord);
}
}
}
try{
if(erplist.size()>0){
update erplist;
}
}catch(Exception ex){
system.debug(ex);
}
}
// This method is updating child part number into RWA record to show the RWA form page.
public void updateERPPartNumber(List<Agreement__c> AgList){
String concatenateString = '';
List<Agreement__c> rwlist = new List<Agreement__c>();
List<Device__c> erplst = [SELECT Name,Part_Number__c,Device__r.Tapeout_Type__c,Device__r.Name FROM Device__c where RWA__c =:AgList];
String partnumber;
for(Agreement__c rw : AgList){
partnumber ='';
system.debug('erplst'+erplst.size());
for(Device__c erp : erplst){
If(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
if(erp.Device__r.Tapeout_Type__c == 'Single Product'){
partnumber = erp.Device__r.Name;
rw.Single_Product__c = true;
}
}else{
If(rw.Apply_RWA_All_Product_family__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
system.debug('concatenateString'+concatenateString);
}
}
}
rw.Initial_Part_Number_s_Covered__c = concatenateString;
if( rw.Description_and_Part_Number__c != null && (rw.Description_and_Part_Number__c.contains(partnumber))){
rw.Description_and_Part_Number__c = rw.Description_and_Part_Number__c;
}else if(rw.Description_and_Part_Number__c == '' || rw.Description_and_Part_Number__c == null ){
rw.Description_and_Part_Number__c = partnumber;
}else{
rw.Description_and_Part_Number__c = partnumber+ rw.Description_and_Part_Number__c;
}
rw.Customer_Product_Family_Name__c = true;
}
}
public void SendApprovalEmail(List<Agreement__c> AgList){
string AccountTeamRoles = EnvironmentSettings.get('Account_Team_Roles');
List<string> TeamRoles = AccountTeamRoles.split(',');
List<string> toRecipients=new List<string>();
List<string> ccAddresses=new List<string>();
Set<id> accid = new Set<id>();
Id templateId;
templateId = [select id, name from EmailTemplate where developername = 'RWA_after_Approval_To_Proxy_Users'].id;
List<Agreement__c> rwList =[select id,ERP_Device__r.Device__r.Account__c from Agreement__c where id=:AgList];
for(Agreement__c rw:rwList){
if(rw.ERP_Device__r.Device__r.Account__c != null){
accid.add(rw.ERP_Device__r.Device__r.Account__c);
system.debug('accid'+accid);
}
}
List<Account_Team__c> listofUsers = [Select Id,user__r.email,User__r.isActive,Team_Role__c,Account__c,User__c from Account_Team__c
where (Team_Role__c in :TeamRoles) AND Account__c =:accid AND User__r.isActive = True];
system.debug('listofUsers'+listofUsers.size());
for(Account_Team__c ATP : listofUsers){
toRecipients.add(ATP.User__c);
}
List<Messaging.SingleEmailMessage > emailList = new list < Messaging.SingleEmailMessage>();
for(Agreement__c rw:AgList){
if(rw.Status__c == 'Approved' && rw.Retrigger_to_ERP__c == false){
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.settoAddresses(toRecipients);
mail.setWhatId(rw.id);
mail.saveAsActivity = false;
mail.setTemplateId(templateId);
mail.setTargetObjectId(rw.OwnerId);
emailList.add(mail);
}
if (emailList.size() > 0) {
Messaging.sendEmail(emailList);
}
}
}
Public void sharingRWAtoCSR(List<Agreement__c> AgList){
Set<id> accid = new Set<id>();
string AccountTeamRoles = EnvironmentVariable.get('CSR Sharing Settings');
List<string> TeamRoles = AccountTeamRoles.split(',');
List<Agreement__c> rwList =[select id,ERP_Device__r.Device__r.Account__c from Agreement__c where id=:AgList];
for(Agreement__c rw:rwList){
if(rw.ERP_Device__r.Device__r.Account__c != null){
accid.add(rw.ERP_Device__r.Device__r.Account__c);
}
}
//Get the CER users from Account team proxy with same account rw.ERP_Device__r.Device__r.Account__c
List<Account_Team__c> listofUsers = [Select Id,user__r.email,User__r.isActive,Team_Role__c,Account__c,User__c from Account_Team__c
where (Team_Role__c in :TeamRoles) AND Account__c =:accid AND User__r.isActive = True];
// Share record to RWA (Agreement__Share)
List<Agreement__Share> sharerwa = new List<Agreement__Share>();
for(Agreement__c Rwa:AgList){
for(Account_Team__c ATP : listofUsers){
Agreement__Share rec = new Agreement__Share();
rec.AccessLevel = 'Edit';
rec.ParentId = Rwa.id;
rec.UserOrGroupId = ATP.User__c;
sharerwa.add(rec);
}
}
try{
if(sharerwa.size()>0){
insert sharerwa;
}
}catch(Exception e){
system.debug(e);
}
}
}
Thanks in advance,
Raj
Please help me how to write test class for below helper.
public class Risk_Waiver_Aggreement_TriggerHelper {
// Populating RWA number to all child ERP device data using below method.
Public void updateParentRWA(List<Agreement__c> AgList){
List<Device__c> erplst = [SELECT Name, RWA__c,Base_Device__c,Customer_Short_Name__c,RWA_Single_Record__c,Base_Device_Unique_Name__c,Product_Family_Unique_Name__c FROM Device__c
where Base_Device_Unique_Name__c=: AgList[0].Base_Device_Unique_Name__c or Product_Family_Unique_Name__c=: AgList[0].Product_Family_Unique_Name__c
or Customer_Short_Name__c =: AgList[0].Account_Short_Name__c];
system.debug('erplst'+erplst.size());
Device__c erprecord = [select id,RWA__c from Device__c where id =:AgList[0].Device__c limit 1];
List<Device__c> erplist = new List<Device__c>();
for (Agreement__c rw : AgList){
for(Device__c erp : erplst) {
if(rw.status__c == 'Submitted'){
if(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' && rw.Apply_RWA_All_Product_family__c =='No'){
if(rw.Base_Device_Unique_Name__c == erp.Base_Device_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
} else{
if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'Yes' && rw.Apply_RWA_All_Product_family__c =='Yes'){
if(rw.Product_Family_Unique_Name__c == erp.Product_Family_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}else if(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='No'){
if(rw.Base_Device_Unique_Name__c == erp.Base_Device_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}else if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='Yes'){
if(rw.Product_Family_Unique_Name__c == erp.Product_Family_Unique_Name__c){
erp.RWA__c = rw.id;
erplist.add(erp);
}
}
}
}
}
if(rw.Status__c == 'Submitted'){
if(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'No' && rw.Apply_RWA_All_Product_family__c =='No'){
erprecord.RWA__c = rw.id;
erplist.add(erprecord);
}else If(rw.Apply_RWA_All_Base_Device__c == 'No' && rw.Expose_RWA_to_Customer__c == 'yes' && rw.Apply_RWA_All_Product_family__c =='No'){
erprecord.RWA__c = rw.id;
erplist.add(erprecord);
}
}
}
try{
if(erplist.size()>0){
update erplist;
}
}catch(Exception ex){
system.debug(ex);
}
}
// This method is updating child part number into RWA record to show the RWA form page.
public void updateERPPartNumber(List<Agreement__c> AgList){
String concatenateString = '';
List<Agreement__c> rwlist = new List<Agreement__c>();
List<Device__c> erplst = [SELECT Name,Part_Number__c,Device__r.Tapeout_Type__c,Device__r.Name FROM Device__c where RWA__c =:AgList];
String partnumber;
for(Agreement__c rw : AgList){
partnumber ='';
system.debug('erplst'+erplst.size());
for(Device__c erp : erplst){
If(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
if(erp.Device__r.Tapeout_Type__c == 'Single Product'){
partnumber = erp.Device__r.Name;
rw.Single_Product__c = true;
}
}else{
If(rw.Apply_RWA_All_Product_family__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
system.debug('concatenateString'+concatenateString);
}
}
}
rw.Initial_Part_Number_s_Covered__c = concatenateString;
if( rw.Description_and_Part_Number__c != null && (rw.Description_and_Part_Number__c.contains(partnumber))){
rw.Description_and_Part_Number__c = rw.Description_and_Part_Number__c;
}else if(rw.Description_and_Part_Number__c == '' || rw.Description_and_Part_Number__c == null ){
rw.Description_and_Part_Number__c = partnumber;
}else{
rw.Description_and_Part_Number__c = partnumber+ rw.Description_and_Part_Number__c;
}
rw.Customer_Product_Family_Name__c = true;
}
}
public void SendApprovalEmail(List<Agreement__c> AgList){
string AccountTeamRoles = EnvironmentSettings.get('Account_Team_Roles');
List<string> TeamRoles = AccountTeamRoles.split(',');
List<string> toRecipients=new List<string>();
List<string> ccAddresses=new List<string>();
Set<id> accid = new Set<id>();
Id templateId;
templateId = [select id, name from EmailTemplate where developername = 'RWA_after_Approval_To_Proxy_Users'].id;
List<Agreement__c> rwList =[select id,ERP_Device__r.Device__r.Account__c from Agreement__c where id=:AgList];
for(Agreement__c rw:rwList){
if(rw.ERP_Device__r.Device__r.Account__c != null){
accid.add(rw.ERP_Device__r.Device__r.Account__c);
system.debug('accid'+accid);
}
}
List<Account_Team__c> listofUsers = [Select Id,user__r.email,User__r.isActive,Team_Role__c,Account__c,User__c from Account_Team__c
where (Team_Role__c in :TeamRoles) AND Account__c =:accid AND User__r.isActive = True];
system.debug('listofUsers'+listofUsers.size());
for(Account_Team__c ATP : listofUsers){
toRecipients.add(ATP.User__c);
}
List<Messaging.SingleEmailMessage > emailList = new list < Messaging.SingleEmailMessage>();
for(Agreement__c rw:AgList){
if(rw.Status__c == 'Approved' && rw.Retrigger_to_ERP__c == false){
Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.settoAddresses(toRecipients);
mail.setWhatId(rw.id);
mail.saveAsActivity = false;
mail.setTemplateId(templateId);
mail.setTargetObjectId(rw.OwnerId);
emailList.add(mail);
}
if (emailList.size() > 0) {
Messaging.sendEmail(emailList);
}
}
}
Public void sharingRWAtoCSR(List<Agreement__c> AgList){
Set<id> accid = new Set<id>();
string AccountTeamRoles = EnvironmentVariable.get('CSR Sharing Settings');
List<string> TeamRoles = AccountTeamRoles.split(',');
List<Agreement__c> rwList =[select id,ERP_Device__r.Device__r.Account__c from Agreement__c where id=:AgList];
for(Agreement__c rw:rwList){
if(rw.ERP_Device__r.Device__r.Account__c != null){
accid.add(rw.ERP_Device__r.Device__r.Account__c);
}
}
//Get the CER users from Account team proxy with same account rw.ERP_Device__r.Device__r.Account__c
List<Account_Team__c> listofUsers = [Select Id,user__r.email,User__r.isActive,Team_Role__c,Account__c,User__c from Account_Team__c
where (Team_Role__c in :TeamRoles) AND Account__c =:accid AND User__r.isActive = True];
// Share record to RWA (Agreement__Share)
List<Agreement__Share> sharerwa = new List<Agreement__Share>();
for(Agreement__c Rwa:AgList){
for(Account_Team__c ATP : listofUsers){
Agreement__Share rec = new Agreement__Share();
rec.AccessLevel = 'Edit';
rec.ParentId = Rwa.id;
rec.UserOrGroupId = ATP.User__c;
sharerwa.add(rec);
}
}
try{
if(sharerwa.size()>0){
insert sharerwa;
}
}catch(Exception e){
system.debug(e);
}
}
}
Thanks in advance,
Raj
Since this code provided is huge and requires an understanding of your implementation, it might not be possible to provide exact test class suggestions. However, the below information should help you get started.
The below articles give a good insight into how to begin writing test class and how coverage can be improved
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Examples:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
https://salesforce.stackexchange.com/questions/87533/writing-test-class-for-wrapper-class
https://salesforce.stackexchange.com/questions/108850/apex-test-method-for-wrapper-classes
If this information helps, please mark the answer as best. Thank you