You need to sign in to do that
Don't have an account?
Tigger to update Picklist field from one object to another object
Hi All,
I'm very brand new to apex coding and need a little help creating a trigger that will update a picklist field (Occupied_Status__c) from one custom object (Inspection_Checklist__c) to another custom object (Property_Evaluation__c) only when another field is TRUE in the original object that is being updated (Inspection_Checklist__c.Inspection_Complete__c = TRUE).
Below is the code I've created so far, I might be completely off, but any help would be appreciated. Thanks in advance!
Right now I'm getting the following error in line 6: Save error: unexpected token: ':'
trigger OccupancyStatus on Inspection_Checklist__c (after update) { List<Property_Evaluation__c> eval = [SELECT j.Occupied_Status__c FROM Inspection_Checklist__c j WHERE j.Inspection_Checklist__c.Inspection_Complete__c = TRUE : Trigger.new FOR UPDATE]; for (Property_Evaluation__c li: eval){ li.Occupied_Status__c=li.Inspection_Checklist__r.Occupied_Status__c; } update eval; }
here you go
trigger OccupancyStatus on Inspection_Checklist__c (before update) {
List<Property_Evaluation__c> peList = new List<Property_Evaluation__c>();
for(Inspection_Checklist__c ic : [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where id in :Trigger.newMap.keySet()])
{
if(ic.Inspection_Complete__c && ic.Property_Evaluation__r!=null && ic.Occupied_Status__c !=null){
peList.add(new Property_Evaluation__c(id=ic.Property_Evaluation__r.id,Occupied_Status__c =ic.Occupied_Status__c,));
}
}
update peList;
}
All Answers
I dont know the relation between the object you have described but I assume particular relation and here is the code if it works for you
if it doesnt work would you please describe in detail so that it would help me understand the problem
Hi Dharmesh,
Here is the error that I am getting:
System.NullPointerException: Attempt to de-reference a null object: Trigger.OccupancyStatus: line 5, column 13
I have a one to many relationship where the Property_Evaluation is the parent and the Inspection_Checklist is the child. I'm want the parent updated whenever the child is updated.
Thanks!
Try following code
hi as per seeing your scenarion i am writing a trigger hope it works for you
try this one instead of the other one i made some changes...........
Dharmesh,
The code returns no errors, however it is not updating the Property_Evaluation__c.Occupied_Status__c field after I update the Inspection_Checklist__c object.
Nagas,
So your trigger is on the Parent object and not on the object that I would be updating? I'm also getting the following error on your code:
It's looking for a parentheses between Trigger.new[0].id] and
need to check the value in log as below. I think because of some value is null, its not entering into if loop and hence not updating the value
in that case try to complie the first trigger i have given only and let me know where you are getting the error.i didnot compiled it you may get some error.
Nagas
here's the error I'm receiving
try this one made some more modifications
No errors in the code, but it's not updating the Property_Evaluation__c.Occupied_Status__c field....
Dharmesh,
I added your code, where do I go to get the log?
check here
Administration Setup->Monitoring->Debug Log
add your name here and execute the code. the detail log will be available at specified location
Here's what I get when I first create the obejct, but I dont' get another reference to the trigger after I try updating the record.
try this out. made some change. it should work now
I can feel that we're almost there :)
here you go
trigger OccupancyStatus on Inspection_Checklist__c (before update) {
List<Property_Evaluation__c> peList = new List<Property_Evaluation__c>();
for(Inspection_Checklist__c ic : [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where id in :Trigger.newMap.keySet()])
{
if(ic.Inspection_Complete__c && ic.Property_Evaluation__r!=null && ic.Occupied_Status__c !=null){
peList.add(new Property_Evaluation__c(id=ic.Property_Evaluation__r.id,Occupied_Status__c =ic.Occupied_Status__c,));
}
}
update peList;
}
Thanks a million!!!
the only change I made was (after update), but you did all the hard work.
Thanks again Dharmesh.
Hey Dharmesh,
I wanted to try and write the test method myself before I asked for help, but here's what I got so far, do you have any suggestions?
Thanks again.
I'm getting an error on line 26: Duplicate variable: ic
if you see the trigger.
in test following things should be covered and your coverage should be more than75%
1) create both custom objects with set of fields
2) update one custom object so that it update second one depending the field value.