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
EvertonSzekeresEvertonSzekeres 

Test class for trigger with delete

Hi !

I have this trigger

trigger DeletarProdutos_Qtd0 on Asset (after update) {

for(Asset ast:[SELECT quantity FROM Asset WHERE quantity = 0])

    delete ast;
}


I don't know if this trigger have something wrong, but in my sandbox worked.

I'm facing trouble with the test class.
I tried this:

@isTest (Seealldata=true)
private class DeletarProdutos_Qtd0_test {

    static testMethod void myUnitTest() {
       
        Account acc = new Account(firstname='Teste', lastname='Test');
        insert acc;
       
        Asset ass = new Asset();
        ass.name = 'SUPER ÁLBUM';
  ass.quantity = 0;
  ass.accountid = acc.id;
  insert ass;
       
  ass.quantity = 0;
  ass.accountid = acc.id;
  delete ass;
}
}


Thanks!
Ramu_SFDCRamu_SFDC
You might have to use a select query and retrieve the newly inserted asset. Change the last 3 lines as 
Asset Ass1=new Asset([select id from Asset where name='SUPER ALBUM');
delete Ass1;

Please let me know if this helped.
EvertonSzekeresEvertonSzekeres
I get this error:

Save error: SObject constructor must use name=value pairs
Ramu_SFDCRamu_SFDC
My bad, i missed a close square bracket and the limit clause. Please use the below line and it should fix the problem. Please let me know if it throws any error

Asset Ass1=new Asset([select id from Asset where name='SUPER ALBUM' limit 1]);
delete Ass1;
EvertonSzekeresEvertonSzekeres
I noticed the square bracket :D.

I'm still getting the same error with the limit 1