+ Start a Discussion
KyoKyo 

Average Trigger Code Be Low

Average Code Low . I can update Average Code?

 

 

trigger SumSodcSync on Sales_Order_Complete__c(Before insert) {
for(Sales_Order_Complete__c sodc:trigger.new){
      
        Sales_Order__c  Sod = [Select id from Sales_Order__c Where id = : ApexPages.currentPage().getParameters().get('id')];
        
        Sales_Order_Complete__c[] Scom = [Select id,OpportunitiesID__c ,DimensionTxWxL__c,Grade__c,Total_Weight__c,Quotes__c,
        Fail__c, Name, Products__c, Quantity__c,Quantity_Remain__c,Sales_OrderID__c,Success__c,Date__c,Pop__c,
        Sales_Order_Line_ItemID__c,Deliver_Remain__c,Delivery_Good_Pcs__c,Order_Quantity__c,Total_Finished_Good__c ,
        Delivery_Good_Ton__c,Delivery_Remain_Ton__c,Finished_Good_Remain__c,Finished_Good_Remain_Ton__c,Finished_Tons__c 
        from Sales_Order_Complete__c where Sales_OrderID__c  =: Sod.id And Pop__c != true ];
            
            Sodc_Sumary__c[] objssc = new Sodc_Sumary__c[]{};
            Sodc_Sumary__c Sc = new Sodc_Sumary__c();
      
                for(Sales_Order_Complete__c Ssc: Scom ){
                
                Sc = new Sodc_Sumary__c();
                Sc.Sales_Order__c = Ssc.Sales_OrderID__c;
                Sc.Dimen__c = Ssc.DimensionTxWxL__c;
                Sc.Grade__c = Ssc.Grade__c;
                Sc.Order_Status__c = Ssc.Id;
                Sc.Name = Ssc.Name;
                Sc.Finished_Good_Remain__c = Ssc.Finished_Good_Remain__c;
                Sc.Finished_Good_Remain_Ton__c = Ssc.Finished_Good_Remain_Ton__c;
                Sc.Deliver_Remain__c= Ssc.Deliver_Remain__c;
                Sc.Delivery_Remain_Ton__c = Ssc.Delivery_Remain_Ton__c;
                Sc.Quantity__c = Ssc.Order_Quantity__c;
                
                objssc.add(Sc); 
                }
                insert objssc; 
        
    }
}

 

 

@isTest
private class TestSumSodcSync {
 static testMethod void myUnitTest() {    
                Account Acc = New Account(Name = 'test',Type='Partner');
                insert Acc;
                Opportunities__c Opp = new Opportunities__c(Account_Name__c = Acc.ID,Stage__c = 'Negotiation',Probability__c ='60%',Unit__c ='Ton');
                insert Opp;
                Opportunities_Line_Item__c OppL = new Opportunities_Line_Item__c(OpportunitiesID__c = Opp.id);
                insert OppL;
                Quotes__c N = new Quotes__c(OpportunitiesID__c=Opp.id,Account_Name__c = Acc.id);
                insert N;    
                Quotes_Line_Item__c QL = new Quotes_Line_Item__c(QuotesID__c = N.id,Opportunities_Line_Item__c = OppL.id);
                insert QL;
                Sales_Order__c Sod = new Sales_Order__c(Quotes__c = N.id);
                insert Sod;
                Sales_Order_Line_Item__c SoL = new Sales_Order_Line_Item__c(Sales_OrderID__c = Sod.id);
                insert SoL;
                Sales_Order_Complete__c Sodc = new Sales_Order_Complete__c(Sales_OrderID__c = Sod.id,OpportunitiesID__c = Opp.id,Quotes__c = N.id,Sales_Order_Line_ItemID__c = SoL.id);
                insert Sodc;
                Sodc_Sumary__c Sos = new Sodc_Sumary__c(Order_Status__c = Sodc.id,Sales_Order__c = Sod.id);
                insert Sos;

              
  }                 
}

 

Thank you so much.

 

 

joshbirkjoshbirk

It's probably low because you aren't replicating the currentPage, which you are using in your first select.  You can see some code samples here:

 

www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

 

That said, I'm not sure I'd recommend have a reference to CurrentPage() being the basis for a trigger, since it will fail if the data is being accessed in any other way, such as how your unit test is doing so.  I believe you'll get an error back on the second SOQL in this case.

 

Also, I'd suggest moving the DML statement out of your initial for loop, as it could easily hit a governor limit if multiple records are inserted at once.