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

Apex Trigger problem
HI, I have a problem with this trigger, I am not able to update the fields contact_lago__c :(
Here's the code:
//this trigger assign an owner for each Candidatura Discover based on score.
trigger AssignOwnerDiscover on Candidatura_Tenant__c (before insert,before update) {
//Set<ID> ids = Trigger.newMap.keySet();
List<Candidatura_Tenant__c> c = [SELECT Id,punteggio__c,contact_lago__c,Lead__r.Country FROM Candidatura_Tenant__c WHERE Id in :Trigger.newMap.keySet()];
if (c.size()==0)
return;
for(Candidatura_Tenant__c i: c){
if(i.punteggio__c >=21){
if(i.Lead__r.Country=='IT')
i.contact_lago__c ='003c000000hafFz';
else
i.contact_lago__c ='003a000001v34dh';
}
else{
if(i.punteggio__c >14)
i.contact_lago__c ='00313000028crUg';
else
i.contact_lago__c =i.Lead__r.Agente__c;
}
}
}
Thanks in advance!
Here's the code:
//this trigger assign an owner for each Candidatura Discover based on score.
trigger AssignOwnerDiscover on Candidatura_Tenant__c (before insert,before update) {
//Set<ID> ids = Trigger.newMap.keySet();
List<Candidatura_Tenant__c> c = [SELECT Id,punteggio__c,contact_lago__c,Lead__r.Country FROM Candidatura_Tenant__c WHERE Id in :Trigger.newMap.keySet()];
if (c.size()==0)
return;
for(Candidatura_Tenant__c i: c){
if(i.punteggio__c >=21){
if(i.Lead__r.Country=='IT')
i.contact_lago__c ='003c000000hafFz';
else
i.contact_lago__c ='003a000001v34dh';
}
else{
if(i.punteggio__c >14)
i.contact_lago__c ='00313000028crUg';
else
i.contact_lago__c =i.Lead__r.Agente__c;
}
}
}
Thanks in advance!
You're running Trigger.newMap.keySet() on BEFORE trigger that's why it's not doing anything because the keyset is empty! KeySet populates with IDs and IDs are only assigned AFTER the operation (insert/update).
You can either make it an after trigger or drop the keyset and use Trigger.New and a list to hold Candidatura_Tenant__c records.
Also, change the last 'else' statement to reference Id, not the whole object:
All Answers
Best regards
And, the difference is SELECTId,punteggio__c,contact_lago__c,Lead__r.Agente__c,Lead__r.Country FROM Candidatura_Tenant__cWHERE Id in :Trigger.newMap.keySet()
You're running Trigger.newMap.keySet() on BEFORE trigger that's why it's not doing anything because the keyset is empty! KeySet populates with IDs and IDs are only assigned AFTER the operation (insert/update).
You can either make it an after trigger or drop the keyset and use Trigger.New and a list to hold Candidatura_Tenant__c records.
Also, change the last 'else' statement to reference Id, not the whole object: