0

I am new to Apex and trying to code a Trigger on the child Offer to update the parent Job Application fields to new values when a new offer record is created. This is the code that is giving me an error stating - Save error: Initial term of field expression must be a concrete SObject: LIST Any help would be appreciated.


trigger OfferTrigger on Offer__c (after insert, after update) {

    //Create a List to hold the JobApps to be updated
    List<Job_Application__c> jobApps = new List<Job_Application__c>();
    //There is a validation rule on the Offer__c object that requires Job_Application__c 
    // to be Specified. Therefore we can assume all offers have a valid Job Application
    for(List<Offer__c> offers:[select job_application__r.name from offer__c where id IN :Trigger.newMap.keySet()]){
        for(Offer__c offer:offers){
            offer.job_application__r.stage__c ='Offer Extended';
            offer.job_application__r.status__c = 'Hold';
            jobApps.add(offer.job_application__r);
        }
    }
    // perform the update of job application records
    if (jobApps.size() > 0){
        try {
            Database.SaveResult[] saveResults = Database.update(jobApps.false);
            // Now go thru the results and send errors to the debug logs.
            Integer x = 0;
            for(Database.SaveResult result:saveResults){
                if(!result.isSuccess()){
                    //Get the first save result error
                    Database.Error err = result.getErrors()[0];
                    System.debug('Unable to update Job Application, ' + jobApps[x].name + ' Error');
                    }
                    x++;
            }
        }catch (Exception e){
            System.debug('error updating job applications: ' + e);
        }
    }
}
4
  • I tried with a more simple code and it worked. I think what had happened was in Force.com IDE when the Trigger was initially written I had not downloaded from server the Offer__c object definition from server and that was the reason it was displaying the above error. Here is a working code for reference. Lesson learnt: Please download all your referenced objects in your Trigger from Server before writing the code. Commented Sep 27, 2014 at 9:48
  • trigger OfferTrigger2 on Offer__c (after insert, after update) { //Create a List to hold the JobApps to be updated List<Job_Application__c> jobApps = new List<Job_Application__c>(); for(List<Offer__c> offers:[select job_application__r.name from offer__c where Id IN:Trigger.newMap.keySet() ]){ for(Offer__c offer:offers){ offer.job_application__r.stage__c='Offer Extended'; offer.job_application__r.status__c='Hold'; jobApps.add(offer.job_application__r); } } update jobApps; } Commented Sep 27, 2014 at 9:48
  • I got my original code to work too. There was a coding mistake. There was a dot instead of a comma in (jobApps,false) Commented Sep 27, 2014 at 10:09
  • I changed this Database.SaveResult[] saveResults = Database.update(jobApps.false); to this Database.SaveResult[] saveResults = Database.update(jobApps,false); Commented Sep 27, 2014 at 10:10

2 Answers 2

0

If there is a validation in Offer__c that it should be associated to a Job_application__c then my suggestion would be to convert the relationship to Master detail if it is a lookup relationship now. Then you can use a workflow to update the parent field instead of going for a trigger. Since your code is updating the parent with a static value you can very well use field update and have the static text in the formula section of the field update. In this way your functionality will be clean.

More information on cross object field update can be found here : https://help.salesforce.com/HTViewHelpDoc?id=workflow_cross_object_field_updates.htm&language=en_US

0

You have an error at the first for loop

Instead of this:

for(List<Offer__c> offers:[select job_application__r.name from offer__c where id IN :Trigger.newMap.keySet()]){
        for(Offer__c offer:offers){
            offer.job_application__r.stage__c ='Offer Extended';
            offer.job_application__r.status__c = 'Hold';
            jobApps.add(offer.job_application__r);
        }
    }

Use:

for(Offer__c offer:[select job_application__r.name from offer__c where id IN :Trigger.newMap.keySet()]){

            offer.job_application__r.stage__c ='Offer Extended';
            offer.job_application__r.status__c = 'Hold';
            jobApps.add(offer.job_application__r);

  }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .