You need to sign in to do that
Don't have an account?
Lago S.p.a.
Update a triggered object with a trigger
Hi everyone! I'm a little bit frustrated 'cause I'm trying to write a trigger like this:
trigger MediaTrigger on Media__c (after insert) {
for(integer i=0;i<trigger.new.size();i++){
if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP'){
List<task_apt__c> att=[select a.Id from task_apt__c a where a.Id=:trigger.new[i].task__c];
task_apt__c attivita = att.get(0);
List <Candidatura_Redesigner__c> cand=[select c.Id from Candidatura_Redesigner__c c where c.Lead__c=:attivita.lead_name__c];
Candidatura_Redesigner__c cnd=cand.get(0);
List<Media__c>md=[select m.Id from Media__c m where m.Id=:Trigger.new[i].Id];
Media__c media=md.get(0);
media.cand_redesigner__c=cnd.Id;
update media;
}
}
}
But the object named media won't be updated after inserted.
Of course I'm wrong ..but I would like to know where is the error because I'm not able to find it out!
Thank you and have a nice day :)
trigger MediaTrigger on Media__c (after insert) {
for(integer i=0;i<trigger.new.size();i++){
if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP'){
List<task_apt__c> att=[select a.Id from task_apt__c a where a.Id=:trigger.new[i].task__c];
task_apt__c attivita = att.get(0);
List <Candidatura_Redesigner__c> cand=[select c.Id from Candidatura_Redesigner__c c where c.Lead__c=:attivita.lead_name__c];
Candidatura_Redesigner__c cnd=cand.get(0);
List<Media__c>md=[select m.Id from Media__c m where m.Id=:Trigger.new[i].Id];
Media__c media=md.get(0);
media.cand_redesigner__c=cnd.Id;
update media;
}
}
}
But the object named media won't be updated after inserted.
Of course I'm wrong ..but I would like to know where is the error because I'm not able to find it out!
Thank you and have a nice day :)
For example, if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP') always return false then nothing will happen.
Also you syntax is unusual, typically the syntax is:
if (Trigger.isInsert)
{ for (Media__c a : Trigger.new) { ............Then use the variable a to represent the Media_c instances......}}
Last, you should check each object to see if the object is null.
You can use System.assert() to halt the process if any of these turn out to be nulls and you did not expect it to be.
Try again and post more questions if you run into issues.
Thx
All Answers
For example, if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP') always return false then nothing will happen.
Also you syntax is unusual, typically the syntax is:
if (Trigger.isInsert)
{ for (Media__c a : Trigger.new) { ............Then use the variable a to represent the Media_c instances......}}
Last, you should check each object to see if the object is null.
You can use System.assert() to halt the process if any of these turn out to be nulls and you did not expect it to be.
Try again and post more questions if you run into issues.
Thx
Hi everyone and thanks for your kind reply!
I've undestood all your comments, but I've found a workaround keeping my code as it is, and changing this line
if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP'){
with this:
List<task_apt__c> att=[select a.lead_name__c, a.RecordTypeId from task_apt__c a where a.Id=:trigger.new[i].task__c];
task_apt__c attivita = att.get(0);
System.debug('attivita='+attivita);
if (attivita.RecordTypeId=='01213000001WGQP')
maybe in Apex trigger probably the relationship field could be problematic :)
thanks and have a nice day :)