You need to sign in to do that
Don't have an account?

Updating child records if parent is updated through trigger
First_visit__c is parent record
Time__c child record
Here i am creating new record if first_visit__c is created.
i am able to insert the record, but i need help to update it. I know this can be done by workflow rule but i would like to do it with trigger.
i also tried this way but havnt tested but it should work:
Time__c child record
Here i am creating new record if first_visit__c is created.
i am able to insert the record, but i need help to update it. I know this can be done by workflow rule but i would like to do it with trigger.
//Generate New Time Child Record when first Visit record is created trigger Time_FirstVisit on First_Visit__c (after insert) { Time__c ts= new Time__c(); //fvlast holds the value of last created firstvisit record First_Visit__c fvlast = [select Id from First_Visit__c order by CreatedDate desc limit 1]; for(First_Visit__c fv:trigger.new) { ts.Purpose_of_Visit__c='First Visit'; ts.Time_Account_Mapping__c= fv.Account_First_Visit_Mapping__c; ts.Hours__c=fv.Time_Taken__c; ts.Task_Date__c=Date.today(); ts.First_Visit_Time_Mapping__c=fvlast.id; } //insert new time record insert ts; }
i also tried this way but havnt tested but it should work:
//Generate New Time Child Record when first Visit record is created trigger Time_FirstVisit on First_Visit__c (after insert,after update) { if(Trigger.isInsert) { List<Time__c> tl = new List <Time__c>(); for(First_Visit__c fv:trigger.new) { if(fv.Time_Taken__c != null) { tl.add(new Time__c(Purpose_of_Visit__c='First Visit', Time_Account_Mapping__c= fv.Account_First_Visit_Mapping__c, Hours__c=fv.Time_Taken__c, Task_Date__c=Date.today(), First_Visit_Time_Mapping__c=fv.id)); } } } if(Trigger.isUpdate) { for // First_Visit__c FVL = [SELECT ID FROM First_Visit__c order by LastModifiedDate desc LIMIT 1]; // First_Visit__c childTL = [SELECT (Select ID from times__r) from first_visit__c Limit 1]; } }
Relying on CreatedDate is asking for trouble. You'll likely run into issues quickly with either concurrent records being created and incorrectly associated as parent / child relationships or your relationship tree will become too long and cause issues as well.
That being said, for updating children records you would need something similar to the following:
code i am getting error on last line.
times__r child relationship name.