Hello I try build a trigger, this trigger fire in the register object, and when its create send the information to times object, but before insert the data in time object, have to testing if the Project object have the Project Pm field different of null.
I try to do this code but doesn't work.
I did this steps.
- Create a trigger with object and instantiate
- Create a list to object were I want send the data Registo->Time
- With Select find the field Projecto_PM__c in object Projectos__c and compare if is the same selected in a lookup in Registo__c object
- Compare if Projecto_PM__c is different from null
And insert data in Time object
trigger updatetime on Registo__c (After insert) { for (Registo__c registo : Trigger.New) { List<Milestone1_Time__c> sr = new List<Milestone1_Time__c>(); Projectos__c test = [SELECT Id, Projecto_PM__c FROM Projectos__c WHERE Name = :registo.Projecto_associado__c]; if (test.Projecto_PM__c != NULL) { sr.add(new Milestone1_Time__c( Description__c = registo.Descricao__c, Hours__c = registo.Numero_de_horas_dispendidas__c, Date__c = registo.Data_da_tarefa__c, Project_Task__c = registo.task_relacao__c )); } insert sr;
Somebody can help me with this, I'm new in apex and don't know the next step.
Please help me.
Thanks.
Attribution to: Lisyy
Possible Suggestion/Solution #1
Few things that stand out in the code above:
- Don't do SOQL queries or DML operations in the loop body
- You say you have a lookup from
Registo__c
toProjectos__c
, in that case you should be comparing it withId
and notName
field ofProjectos__c
Have a look at the code below:
trigger updatetime on Registo__c (After insert) {
List<Milestone1_Time__c> sr = new List<Milestone1_Time__c>();
List<Id> projectoIds = new List<Id>();
for (Registo__c registo : Trigger.New) {
projectoIds.add(registo.Projecto_associado__c);
}
Map<Id, Projectos__c> projectos = new Map<Id, Projectos__c>([SELECT Projecto_PM__c FROM Projectos__c WHERE Id in :projectoIds]);
for (Registo__c registo : Trigger.New) {
if (projectos.get(registo.Projecto_associado__c).Projecto_PM__c != NULL) {
sr.add(new Milestone1_Time__c(
Description__c = registo.Descricao__c,
Hours__c = registo.Numero_de_horas_dispendidas__c,
Date__c = registo.Data_da_tarefa__c,
Project_Task__c = registo.task_relacao__c
));
}
}
insert sr;
Attribution to: IvanR
Possible Suggestion/Solution #2
Just following your code you could do this:
trigger updatetime on Registo__c (After insert) {
Set<ID> projectosIDs = New Set<ID>();
for(Registo__c registo : trigger.new){
projectosIDs.add(registo.Projecto_associado__c);
}
Map<ID,Progectos__c> proj = New Map<ID<Projectos_c>([Select ID, Projecto_PM__c From Projectos__c Where ID IN : projectosIDs]);
Milestone1_Time__c[] sr = New Milestone1_Time__c[]{};
for(Registo__c registo: Trigger.New) {
if(proj.containsKey(registo.Projecto_associado__c)){
if(prom.get(registo.Projecto_associado__c).Projecto_PM__c != null){
sr.add(new Milestone1_Time__c(
Description__c = registo.Descricao__c,
Hours__c = registo.Numero_de_horas_dispendidas__c,
Date__c = registo.Data_da_tarefa__c,
Project_Task__c = registo.task_relacao__c
));
}
}
if(!sr.isEmpty())
insert sr;
}
Attribution to: Eric
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34102