+ Start a Discussion
Lago S.p.a.Lago S.p.a. 

How to pass ID to a method

Hi everyone!
I've a little problem with a trigger. This trigger cicles on custom objects called Candidatura_Tenant__c and if a condition is ok, it calls a method (that creates another custom object)on a particular class. But for calling this method, I need to pass it the Id of the current Candidatura_Tenant__c, but the method creates the custom object withoud linking it to che current Candidatura_Tenant__c.
Here's the part of the trigger:
for(Candidatura_Tenant__c i: Trigger.new) {
        //search on candidatura_tenant all ID in the map
        Lead l = leads.get(i.Lead__c);
        String cnd = i.Id; // or ID cnd= i.Id; result is the same
        if(i.punteggio__c >= 21) {
            if(l.Country=='IT'){
                //assign the contact on candidatura
                List<Contact> cont = [SELECT c.Id FROM Contact c WHERE c.Name='Cristina Reginato' AND
                                     c.Account.Name = 'LAGO S.P.A.'];
                Contact contatto = cont.get(0);
                //i.contact_lago__c = '003c000000hafFz';
                i.contact_lago__c = contatto.Id;
                if (Trigger.isInsert == true){
                TaskHandler tsk = new TaskHandler();
                task_apt__c t = tsk.task_discover(Contatto,cnd,i.Lead__c);
                }
            }
.... and here's the method called by the trigger:
public class TaskHandler {
public task_apt__c task_discover(Contact cnt,ID cnd,ID lead){
task_apt__c task_disc = new task_apt__c();
task_disc.responsabile__c = cnt.Id;
task_disc.type__c = 'Candidatura Discover';
task_disc.RecordTypeId = '01213000001WGF7';
task_disc.state__c = 'Aperto';
task_disc.tipo_feedback__c = 'Risposta Libera';
task_disc.candidatura__c = cnd;
task_disc.task_end_date__c = date.today()+3;
task_disc.task_desc__c = 'Contatta il cliente per verificare e approvare la candidatura al progetto discover ';
task_disc.lead_name__c = lead;
insert task_disc;
return task_disc;
}
//metodo che crea la task di tipo discover (2016) 
}
Any help?
Thanks in advance
Rohit K SethiRohit K Sethi
Hi Lago S.p.a.,

I thing this trigger are you writting before insert that is why the custom object Candidatura_Tenant__c  is not binded with newly created object task_apt__c.

Here is the reason, when before insert trigger the records can not have the Id of the inserted record . So we can say that after bypass through before insert trigger the id is genrated for newly created records .

So you need to write trigger on after insert so that the "task_apt__c" can be assign the manage relation/Linking.

If this post solves your problem kindly mark it as solution.
Thanks. 
Lago S.p.a.Lago S.p.a.
Hi, unfortunately I can only work with before insert trigger...can you give me a solution?
Thanks
Rohit K SethiRohit K Sethi
Hi Lago S.p.a.,

There is a flow of execution of trigger first before insert  trigger fire then after the Id is assign to the inserted Record so we have limitation to run your trigger on after insert . 

And I thing you are executing some other work on before insert .So have one more way to saprate you code with after trigger as following condtion :
                        if(trigger.isInsert() && trigger.isAfter()){
                             // Write code for after as we require  
                        }else if(trigger.isInsert() && trigger.isBefore()){
                             // Write code for before
                         }

And if you have query plz ask.

Thanks.

 
Lago S.p.a.Lago S.p.a.
I don't understand how can i pass the Id on my method..I've understood that in a before trigger there are complications because ID is not created..but I need to do it :(