You need to sign in to do that
Don't have an account?
DiegoT.
Test class for trigger update relationship on Contact object
Hi everyone, i am new with Apex and i have a question.
I want to update the Contact object field location_boat__c and that the location__c fields of the Boat object be updated also be updated My test is incompleted. i want test my trigger UpdateLocationand i dont know how do it
the object Boat field name Contac__c have relationship lookup with the Contact (Object standard)
My test is wrong but i can not how resolve this problem
I want to update the Contact object field location_boat__c and that the location__c fields of the Boat object be updated also be updated My test is incompleted. i want test my trigger UpdateLocationand i dont know how do it
the object Boat field name Contac__c have relationship lookup with the Contact (Object standard)
My test is wrong but i can not how resolve this problem
trigger UpdateLocation on Contact (after update) { if (trigger.isAfter && Trigger.isUpdate) { List<Boat__c> boats = new List<Boat__c>(); for (Contact c : [ SELECT location_boat__c, (SELECT Id FROM Boats__r) FROM Contact WHERE Id = :trigger.new ]) { for (Boat__c b : c.Boats__r) { b.location__c = c.location_boat__c; boats.add(b); } } update(boats); }}
@isTest public class PruebaBotes { @isTest static void TestActualizar() { Boat__c b1 = new Boat__c(Name='bote uno' ,Price__c=20, Type__c='Velero', location__c='Argentina'); Boat__c b2 = new Boat__c(Name='bote dos' ,Price__c=50, Type__c='Yate', location__c='Brasil'); Boat__c b3 = new Boat__c(Name='bote tres' ,Price__c=125, Type__c='Catamaran', location__c='España'); Boat__c b4 = new Boat__c(Name='bote cuatro' ,Price__c=700, Type__c='Velero', location__c='India'); insert b1; insert b2; insert b3; insert b4; Contact a1 = new Contact(Lastname='Pepe', location_boat__c = 'Brasil'); Contact a2 = new Contact(Lastname='Jose', location_boat__c = 'Argentina'); Contact a3 = new Contact(Lastname='Maria', location_boat__c = 'India'); Contact a4 = new Contact(Lastname='Ana', location_boat__c = 'Argentina'); insert a1; insert a2; insert a3; insert a4; Contact updateLocation; updateLocation = [SELECT location_boat__c FROM Contact WHERE location_boat__c = 'Argentina' LIMIT 1]; updateLocation.location_boat__c = 'India'; update updateLocation; Test.startTest(); Contact afterUpdate = [SELECT location_boat__c FROM Contact WHERE Id=:updateLocation.Id]; System.assertEquals('India', afterUpdate.location_boat__c); Test.stopTest(); } }
It should be after update (As per the code you have shared in the question).
please change it
All Answers
Always trigger should be bulkified (WHERE Id IN :trigger.new) It should not be written in single record context (WHERE Id = :trigger.new)
I have modified the code. Please have a look.
size.list = 4 and the query select count = 1
i wanna see if the location__c of the Account is the same as the boat_location__c of object Boat after update but i dont know as do the test of trigger.
That's why want to examine debug log to check if something else is going on between the flow.
ubicacion__c = location__c
ubicacion_bote__c = boat_location, sorry i have in spanish.
1. Is the trigger active?
2. Can you please post extact trigger code which you have deployed. Is it same what I have given?
mainly first line of the trigger is having after update in the context right?
trigger UpdateLocation on Contact (after update) {
It should be after update (As per the code you have shared in the question).
please change it
Any extra bibliography that you recommend (also of trailmix or apex documentation)
Also, go through standard apex documentation. Yes, you can start with trailhead modules. I don't have link.
I have been given training in my company + coding tasks . Hence, I didn't really refer online stuff.
Il share list of trigger tasks which you can try in free time.
I have last question Nayanam why result of the query is empty?
what do you recommend to see the results of query of a test?
Does it really has records? Create a object tab for Bote and see if record is present in listview.