function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lago S.p.a.Lago S.p.a. 

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!
Best Answer chosen by Lago S.p.a.
J CesarJ Cesar
HI Lago,

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:
else
i.contact_lago__c = i.Lead__r.Agente__r.Id;

All Answers

Ray C. KaoRay C. Kao
Try this one...
//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.Agente__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;
        }
    }
}

 
Lago S.p.a.Lago S.p.a.
Hi, it doesn't work.. but what is the difference between mine?
Best regards
Ray C. KaoRay C. Kao
Could you provide the error message?
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()
Lago S.p.a.Lago S.p.a.
Hi, thanks for your response. There's not an error, basically the trigger doesn't do nothing. I aspect that it fills the field "contact_lago__c" with the value, but it still remain empty.
Ray C. KaoRay C. Kao
Coud you try again this one...
//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.Agente__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){
                i.contact_lago__c ='003c000000hafFz';
            else 
                i.contact_lago__c =i.Lead__r.Agente__c;
        }
    }
}

 
J CesarJ Cesar
HI Lago,

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:
else
i.contact_lago__c = i.Lead__r.Agente__r.Id;
This was selected as the best answer