function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff BomarJeff Bomar 

Problem with Apex trigger

Can someone tell me what I am doing wrong?
I want to create a trigger that when the Custom type = Tape Drop  and Total hours are updated so does not = Null or > 0 the trigger will get the cost of the Custom Development from the price book (189.00) and the cost of the Tape Drop from the price book (750.00)  and put the
(Tape Drop (750) + Total Hours * Custom Development) in Testing_Balance__c here is what I have so far.
 
trigger UpdateBalanceTapeDrop on Custom_Programming__c (before insert, before update) {
 for (Custom_Programming__c custom : Trigger.new) 
    {

    //SQL statement to lookup price 
    List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name = 'Custom Development'AND PriceBook2.Name='DAKCS'];
    List<PricebookEntry> sqlResult2 = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name = 'Tape Drop'AND PriceBook2.Name='DAKCS'];
        if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c != null)                
        {
            custom.Testing_Balance__c = sqlResult2[0].UnitPrice + (Total_Hours__c*sqlResult[0].UnitPrice);
        }
    }        
}

Any help would be much appreciated. 
 
Best Answer chosen by Jeff Bomar
Jeff BomarJeff Bomar
I was able to figure this out with this 
trigger UpdateBalanceTapeDrop on Custom_Programming__c (before insert, before update) {
 for (Custom_Programming__c custom : Trigger.new) 
    {

    //SQL statement to lookup price 
        List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
            if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c != null)            
        {
            // Tapedrop  + (Custom Programing * Total Hours)
            custom.Testing_Balance__c = sqlResult[1].UnitPrice + (sqlResult[0].UnitPrice * custom.Total_Hours__c);
        }
    }        
}

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Jeff,

Greetings!

I would suggest you to try by adding the System.debug() statements in the code to make sure that the data is available.

Also,Capture the debug logs to see the complete code execution which makes easy to troubleshoot further on this.

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm&language=en_us

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Jeff BomarJeff Bomar
I know the data is available I because I just added it to the price book I was just not sure how to grab two separate entry from the price book one for the product and one for the hourly cost to install it. 
Jeff BomarJeff Bomar
I was able to figure this out with this 
trigger UpdateBalanceTapeDrop on Custom_Programming__c (before insert, before update) {
 for (Custom_Programming__c custom : Trigger.new) 
    {

    //SQL statement to lookup price 
        List<PricebookEntry> sqlResult = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
            if (custom.Custom_Type__c == 'Tape Drop' && custom.Total_Hours__c != null)            
        {
            // Tapedrop  + (Custom Programing * Total Hours)
            custom.Testing_Balance__c = sqlResult[1].UnitPrice + (sqlResult[0].UnitPrice * custom.Total_Hours__c);
        }
    }        
}
This was selected as the best answer