You need to sign in to do that
Don't have an account?
vivek singh08481200707119766
Please help ,this is my trigger how we make its test class
trigger makeInvoice on Opportunity (after insert, after update) {
for(Opportunity op :trigger.new){
if(op.StageName == 'Closed Won - Signed'){
List<OpportunityLineItem> opli = new List<OpportunityLineItem>();
opli = [select id,Gross__c, Product2id, OpportunityId from OpportunityLineItem where OpportunityId =: op.id order by Product2id];
if(opli.size() > 0){
String tmpId = '';
List<Invoices__c> lstInvoice = new List<Invoices__c>();
Invoices__c objInvoice = new Invoices__c();
Integer lm = opli.size();
Integer i = 1;
for(OpportunityLineItem ob: opli){
if(tmpId == ''){
tmpId = ob.Product2id;
objInvoice = new Invoices__c();
objInvoice.Opportunity_invoices__c = ob.OpportunityId;
objInvoice.Amount__c = ob.Gross__c;
}else{
if(tmpId == ob.Product2id){
objInvoice.Amount__c = objInvoice.Amount__c + ob.Gross__c;
}else{
lstInvoice.add(objInvoice);
tmpId = ob.Product2id;
objInvoice = new Invoices__c();
objInvoice.Opportunity_invoices__c = ob.OpportunityId;
objInvoice.Amount__c = ob.Gross__c;
}
}
if(i == lm){
lstInvoice.add(objInvoice);
}
i++;
}
if(lstInvoice.size() > 0){
try{
insert lstInvoice;
system.debug('!Lst '+lstInvoice);
}catch(Exception e){
system.debug('!Bingo '+e);
}
}
}
}
}
}
for(Opportunity op :trigger.new){
if(op.StageName == 'Closed Won - Signed'){
List<OpportunityLineItem> opli = new List<OpportunityLineItem>();
opli = [select id,Gross__c, Product2id, OpportunityId from OpportunityLineItem where OpportunityId =: op.id order by Product2id];
if(opli.size() > 0){
String tmpId = '';
List<Invoices__c> lstInvoice = new List<Invoices__c>();
Invoices__c objInvoice = new Invoices__c();
Integer lm = opli.size();
Integer i = 1;
for(OpportunityLineItem ob: opli){
if(tmpId == ''){
tmpId = ob.Product2id;
objInvoice = new Invoices__c();
objInvoice.Opportunity_invoices__c = ob.OpportunityId;
objInvoice.Amount__c = ob.Gross__c;
}else{
if(tmpId == ob.Product2id){
objInvoice.Amount__c = objInvoice.Amount__c + ob.Gross__c;
}else{
lstInvoice.add(objInvoice);
tmpId = ob.Product2id;
objInvoice = new Invoices__c();
objInvoice.Opportunity_invoices__c = ob.OpportunityId;
objInvoice.Amount__c = ob.Gross__c;
}
}
if(i == lm){
lstInvoice.add(objInvoice);
}
i++;
}
if(lstInvoice.size() > 0){
try{
insert lstInvoice;
system.debug('!Lst '+lstInvoice);
}catch(Exception e){
system.debug('!Bingo '+e);
}
}
}
}
}
}
I see couple of things in your code which are against the best practices of salesforce :-
1.) You are running your SOQL inside a For loop which is against the best practices.
2.) You are performing DML again inside a For loop whihch is also not recommended.
So,you must move these statements outside the For loop.
As per your test class is concerned,you just need to insert an opportunity record where StageName='Closed Won - Signed' and you are good to go,something like below :-
If this helps,please mark it as best answer to help others :)