+ Start a Discussion

New in Apex and Stuck!!!

I am new in Apex Coding and stuck!!! Here is our simple application, I have a custom object - Service Plan (Service_Plan__c), which has field - Price Rate (Rate__c).
Now I want to create Quote for customers, which has Quote Lines (Quote_Line__c), each Quote Line has direct relationship with one Service Plan.
Now I want to get that Price Rate field value in Quote Line, insert that Price Rate field value in each Quote Line Value (Value__c) field.
How can do this? I tried with (before insert) trigger, but every time it shows syntax error.
How can I get the plan name in trigger code to find the corresponding rate.
Thanks in Advance!

I tried this code -

trigger updateQuoteLine on Quote_Line__c bulk (before insert) {
        Trigger.new.Plan_Rate__c = [select Service_Plan__c.Plan_Rate__c from Service_Plan__c
        where Service_Plan__c.Name = :Trigger.new.Service_Plan__c].Service_Plan__c.Plan_Rate__c;

which shows error -

Error: Compile Error: Initial term of field expression must be an SObject: ARRAY:SOBJECT:Quote_Line__c at line 3 column 39

What does this means, I just followed the code example.



Fixed the compile issue, but still problem, here is my trigger code -

trigger updateQuoteRate on Quote_Line__c (before insert) {

if (Trigger.new.Service_Plan_Name__c != NULL)
             Trigger.new.Plan_Rate__c = [select Service_Plan__c.Plan_Rate__c from Service_Plan__c  where Name = :Trigger.new.Service_Plan_Name__c ].Plan_Rate__c;


I am trying to get the Plan name field value in :Trigger.new.Service_Plan_Name__c, but its getting some object string value instead of field value, any idea??????

First, you'll want to revert this back to a bulk trigger soon because we'll no longer be giving you a single sobject with trigger.new come May 9th. For more info on that change please see this page:


In your latest example is the Service_Plan_Name__c field on Quote_Line__c a lookup (or master/detail relationship) or a text (string) field?

I'll go ahead and presume it's a lookup which would likely cause your inline query to come back with no results and give you an NPE when trying to call the accessor for Plan_Rate__c.

When you create a lookup field in saleforce we present the name field in the UI from the related object but the actual data value on the Quote_Line__c object in the field Service_Plan_Name__c will be an 18 digit record ID. So unless you are setting the Name field on the Service_Plan__c object to be equivalent to the record ID, your query won't find a result.

If my presumption is correct then simply replacing the Name field with the ID field in the where clause of your query will fix the issue I suspect.

If my presumption was incorrect then you'll need to supply more information like the exact runtime exception you are getting and something more about the data model and the values for the given test you are using  perhaps you could post the testMethod that exercises this trigger too.

Thanks Andrew, it works fine.