You need to sign in to do that
Don't have an account?

need test class for the below trigger attached.
Hi Guys, need Test class for the below Code Snippet.please help :
trigger AccountContractTrigger on AccountContract__c (after insert, after delete) {
List<AccountContract__c> targetList;
if(Trigger.isInsert) {
targetList = Trigger.new;
} else if(Trigger.isDelete) {
targetList = Trigger.old;
}
Set<Id> parentIds = new Set<Id>();
for(AccountContract__c rec: targetList) {
parentIds.add(rec.Contract__c);
}
List<ContractSheet__c> parentList = [SELECT Id, ContractPartner__c, (SELECT Account__r.Name FROM AccountChild__r) FROM ContractSheet__c WHERE Id IN: parentIds];
system.debug('names is'+parentList);
for(ContractSheet__c parent : parentList) {
String names = '';
for(AccountContract__c nameSource: parent.AccountChild__r) {
names += nameSource.Account__r.Name + ', ';
}
names = names.removeEnd(', ');
parent.ContractPartner__c = names;
system.debug('names is'+parent.ContractPartner__c);
}
update parentList;
}
trigger AccountContractTrigger on AccountContract__c (after insert, after delete) {
List<AccountContract__c> targetList;
if(Trigger.isInsert) {
targetList = Trigger.new;
} else if(Trigger.isDelete) {
targetList = Trigger.old;
}
Set<Id> parentIds = new Set<Id>();
for(AccountContract__c rec: targetList) {
parentIds.add(rec.Contract__c);
}
List<ContractSheet__c> parentList = [SELECT Id, ContractPartner__c, (SELECT Account__r.Name FROM AccountChild__r) FROM ContractSheet__c WHERE Id IN: parentIds];
system.debug('names is'+parentList);
for(ContractSheet__c parent : parentList) {
String names = '';
for(AccountContract__c nameSource: parent.AccountChild__r) {
names += nameSource.Account__r.Name + ', ';
}
names = names.removeEnd(', ');
parent.ContractPartner__c = names;
system.debug('names is'+parent.ContractPartner__c);
}
update parentList;
}
Please go through this trailhead module, and you can write the test class easily for this triggers.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers
Feel free to ask any query related to your test class.