You need to sign in to do that
Don't have an account?
Need to update related record on edit of parent record
I have code that creates a new Contract_Line__c record whenever I change the status of my parent object, Asset, to 'Active'.
How do I make sure the Contract_Line__c is updated whenever I edit my Asset thereafter?
(As an aside, all of my Contract_Line__c fields are updated by formula fields related to the parent Asset - but this doesn't take care of updating the record. I need it to actually update the record so I can have a second trigger fire (from the Contract_Line__c record)).
Thanks for any help!
Here is my current code, which only takes care of creating new Contract_Line__c records (and not creating duplicates):
trigger Asset on Asset (after update) { List <Contract_Line__c> contractLines = new List <Contract_Line__c> (); Map <Id, Asset> oldmap = new Map <Id, Asset>(); Map <Id, Asset> newmap = new Map <Id, Asset>(); //System.debug('Chris in trigger'); if(Constants.ASSET_FIRST_RUN == False){ }else for(Asset a: Trigger.new) { //System.debug('Chris in asset loop') if ((Trigger.oldMap.get(a.Id).Status != Trigger.newMap.get(a.Id).Status) && Trigger.newMap.get(a.Id).Status == 'Active') { //System.debug('Chris in status changed'); Contract_Line__c cl = new Contract_Line__c (); cl.Asset__c = a.Id; //this needs to "update" the record so that it also "updates" the related Contract Header //cl.Line_No__c = //need to add new number for the Line Number (cl.Line__c) on Contract Line //but make sure it's not the same number as one that was already existing for this Contract_Header__c //System.debug('Chris creating new contract line and adding to list'); contractLines.add(cl); } Constants.ASSET_FIRST_RUN = False; } if(contractLines.size()>0) { //System.debug('Chris has values to insert = '+ contractLines.size()); try { insert contractLines; } catch (System.Dmlexception e) { system.debug (e); } } }
I am checking the value for Constants.Asset_First_Run to be false to run the code...
now i guess this will work.
All Answers
try this.
Error: Compile Error: Variable does not exist: newConList at line 35 column 16
Hello Naidu,
Thanks very much. I added a Date/Time field to update.
I am now getting the following error when I try to save, however: Error: Compile Error: Variable does not exist: newConList at line 35 column 16
try this
Hmm.
Well it's compiling, but the trigger is no longer working.
Did you try with debug statements in the code above?
try to display the newConList before upserting the records. If possible can you post the debug log.
Here is what I have now.
DEBUG is not even touching the first statement.
can you post debug log now?
Hey Naidu,
I do need the code in red, b/c I need there to only be a 1:1 relationship between each Asset and each Contract_Line__c record.
I have 2 possible scenarios:
1. It is a new record and so it needs to insert a new Contract_Line__c
2. It is not a new record, and there is an existing Contract_Line__c record associated with the Asset - in which case it should update the Date_Last_Updated__c field.
Thanks very much for all of your help - I very much appreciate it.
Here is the new debug log. (I have inserted the code from above)
This is only scenario 1 - when creating a new Contract_Line__c from the Asset.
I am checking the value for Constants.Asset_First_Run to be false to run the code...
now i guess this will work.
Hey Naidu!
It worked! Thanks again for all of your help!
Chris
Here is my final code.
FYI, I used the same code, just tweaked a bit to do the same thing for Contract to Contract_Header__c changes!
Thanks again, Naidu. You've been a great help. See tweaked code.
:)