1

I have a before insert trigger that calls the following function:

private void setActualDate(List<RS_Assessment__c> newlist){
    List<Id> activityList = new List<Id>();
    for (Assessment__c assessment : newlist){
        if (assessment.Activity__c != null && assessment.Activity__r.Actual_Date__c == null){
            if (assessment.Activity__r.Last_Assessment_Date__c == null){
                activityList.add(assessment.Activity__c);
            }
        }
    }

    List<RS_Activity__c> processesToUpdate = [SELECT Id FROM RS_Activity__c WHERE Id IN: activityList];
    if (processesToUpdate.size() > 0){
        for (RS_Activity__c act : processesToUpdate){
            RS_Assessment__c assessment = [SELECT Id, Assessment_Date__c FROM RS_Assessment__c WHERE Activity__c  =: act.Id LIMIT 1];
            act.Actual_Date__c = assessment.Assessment_Date__c;
        }
        update processesToUpdate;
    }

However, I get a attempt to deference a null object at the point of doing the assignment i.e. line 7. Is there a work around for this?

UPDATE: I have changed the code and now i get compilation error of: DML requires SObject or SObject list type : List

1
  • Please clarify which object the trigger is on. Please share more code, if possible. Commented Jun 3, 2015 at 20:56

3 Answers 3

2

I think you are passing trigger.new as an argument in your class method "setDefaultProcessActualDate". And in this line you are trying to access field value from master object.

"assessment.Activity__r.Last_Assessment_Date__c"

Unfortunately, trigger.new does not gives the field values from related objects, so you have to query those fields.

2
  • please have a look at my updated question. thx
    – codeSlayer
    Commented Jun 3, 2015 at 9:30
  • You are trying to update a list of string "activities" which doesn't make sense in SFDC. Also, arcusregserv__Activity__c this field is already a lookup so you don't have to use ".Id" with it to get its id. For adding ids use - "activityList.add(assessment.arcusregserv__Activity__c);" For querying records use - "WHERE arcusregserv__Activity__c IN: activityList"
    – Nebula
    Commented Jun 3, 2015 at 9:38
0

The trigger just brings in the fields on that object, not on related objects. You need to query for the relation, something like this:

for (RS_Assessment__c assessment : [SELECT Id, Assessment_Date__c, Activity__r.Actual_Date__c FROM RS_Assessment__c WHERE Id IN :TriggerNewKeySet]){

Update:

List<Activity__c>activities = new List<Activity__c>(); 
for (RS_Assessment__c assessment : [SELECT Id, Assessment_Date__c, Activity__r.Actual_Date__c FROM RS_Assessment__c WHERE Activity__r.Id IN: activityList]){
    assessment.Activity__r.Actual_Date__c = assessment.Assessment_Date__c;
    activities.add(new Activity__c(Id=assessment.Activity__c));
}
update activities;
2
  • please have a look at my updated question. thx
    – codeSlayer
    Commented Jun 3, 2015 at 9:29
  • It looks like the problem was that you were using a list of strings instead of a list of your sObject.
    – martin
    Commented Jun 3, 2015 at 11:12
0

If your trigger is on the RS_Activity__c object and you are trying to pull in data from elsewhere, then remove your DML statement. Also, I don't know why you are querying RS_Activity__c if the trigger is on those records.

If your trigger is on Assessment__c, then I don't see where you are doing anything to the object itself, and your trigger should be AFTER insert.

If your trigger is on RS_Assessment__c, then it looks like you are trying to update other RS_Assessment__c records (other than those in the trigger), so you should make it AFTER insert.

Sorry - I don't mean to be so harsh, but it might be best to step back, explain what you are trying to do, and write it out instead of just showing code. If one of my options above helps, then no need! :)

You must log in to answer this question.

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