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. 

Update a lookup ID with a trigger .

Hi everybody , I have troubles with my code.
What I have to do is update a Field in a object with the same field taken from another object.

I'm trying this with a trigger , here's the code below :

trigger TriggerCreateTaskBroker on Candidatura_Tenant__c (after update)
{
  for (Integer i = 0; i < Trigger.new.size(); i++) 
   {
        if (Trigger.new[i].selected__c != Trigger.old[i].selected__c && Trigger.old[i].selected__c == null && Trigger.new[i].selected__c == 'Si')
      {
          Task_apt__c obj = new Task_apt__c();
          obj.type__c = 'Attivazione Playourhouse';
          obj.responsabile__c = Trigger.old[i].agente_cand__c;
          obj.RecordTypeId = '012a0000001WEaI';
          obj.task_desc__c = 'prova trigger roger x agente';
          obj.phone__c = 20142014;
          insert obj;
          //BookInsert.put(obj.id,obj);
      }
   }
}

The problem is that in the class test, the assert that I've set is wrong,Expected 0, actual 1. So, probably my trigger doesn't do what  have to do.
Anybody has some idea to solve my problem?
thsnk you in advance
Slava RamSlava Ram
show your test class
Lago S.p.a.Lago S.p.a.
@isTest(SeeAllData=true)
public with sharing class TriggerCreateTaskBrokerTest
{
static testMethod void myUnitTest() {
  Test.startTest();
  List<Candidatura_Tenant__c> cand = [select a.id,a.selected__c
   from Candidatura_Tenant__c a where a.Lead__c = '00Qa000001K1Pds'];
  System.assertEquals(cand.size(), 1);
  //System.assertEquals(cand.size(), 1);
 
  Candidatura_Tenant__c candidatura = cand.get(0);
  candidatura.selected__c = 'Si';
  update candidatura;
  System.assertEquals(candidatura.selected__c, 'Si');
 
  List<task_apt__c> task = [select a.id
   from task_apt__c a where a.responsabile__c = '003a000001c6C7k'];
  System.assertEquals(task.size(), 1);
  Test.stopTest();
}
}
Andy BoettcherAndy Boettcher
The test is failing because you're specifying specific recordIds in your test class.  Ids are created on the fly and cannot be hard-coded in this manner.

I see you are attempting to reference existing data in your org by using "SeeAllData=true" - this is not best practice and opens you up to future test failures if the Ids referenced are deleted.  A proper Unit Test creates all data needed for the test and does not rely on existing organization data.
Slava RamSlava Ram
test.stop() should be plased after update and before assert method. Read docs about start() and stop(), especialy on db transactions.
Lago S.p.a.Lago S.p.a.
Hi Slava, thanks for support ,sorry but maybe I don't understand to put the line in my code.

Lago S.p.a.Lago S.p.a.
Hi tecman97 , thanks for your reply.
Here's my code now, but the error is the same , as I supposed.
Have you an idea to show me where I wrong?

public with sharing class TriggerCreateTaskBrokerTest
{
static testMethod void myUnitTest()
{
  Test.startTest();
  Lead dummy =new lead();
  Candidatura_Tenant__c candid = new Candidatura_Tenant__c();
  //List<Candidatura_Tenant__c> cand = [select a.id,a.selected__c
  // from Candidatura_Tenant__c a where a.Lead__c = '00Qa000001K1Pds'];
  //System.assertEquals(cand.size(), 1);
  //System.assertEquals(cand.size(), 1);
  dummy.FirstName ='Pippo';
  dummy.LastName = 'Pluto';
  dummy.Email ='pippo.pluto@gmail.com';
  insert dummy;
  List<Lead> lead = [select a.id from Lead a where a.Email ='pippo.pluto@gmail.com'];
  Lead leadtest = lead.get(0);
  candid.Lead__c = leadtest.Id;
  insert candid;
  Account acc = new account();
  acc.Name = 'Test';
  acc.CustomerCode__c = '990909';
  insert acc;
  Contact contatto = new Contact();
  contatto.FirstName ='Paolino';
  contatto.LastName ='Paperino';
  contatto.Email = 'paolino.paperino@gmail.com';
  insert contatto;
  List<Account> cliente = [select a.Id from Account a where a.CustomerCode__c ='990909'];
  //Candidatura_Tenant__c candidatura = cand.get(0);
  //candidatura.selected__c = 'Si';
  Account account = cliente.get(0);
  List<Contact> cont = [select a.Id from Contact a where a.Email ='paolino.paperino@gmail.com'];
  Contact contact = cont.get(0);
  contact.AccountId = account.Id;
  List<Candidatura_Tenant__c> candidatura = [select a.Id from Candidatura_Tenant__c a where a.Lead__c = :leadtest.Id];
  Candidatura_Tenant__c candidato = candidatura.get(0);
  candidato.agente_cand__c = contact.Id;
  candidato.selected__c= 'Si';
  update account;
  update contact;
  update candidato;
  //System.assertEquals(candidatura.selected__c, 'Si');
  List<task_apt__c> task = [select a.id
   from task_apt__c a where a.responsabile__c = :contact.Id];
  System.assertEquals(task.size(), 1);
  Test.stopTest();

}