0

i have problem with a exercise Writing a trigger in Apex that updates the field "location" of the Boat, when the "boat location" field of the associated Contact is modified, for all the boats that are linked to that Contact. The "Location of boat object" field should be the same as the "location of object contact boat

Boat = custom object-------location = (data type : picklist = Argentina,Brasil,España,India)------ field name= Contact, data type=Lookup Relationship

Contact = object standard----- boat_location = (data type : picklist = Argentina,Brasil,España,India)

my code is wrong and I don't know how to fix it

trigger UpdateLocation on Contact (before update) {

    if (Trigger.isUpdate) {
        List<Contact> cList = new List<Contaact>();

        Map<Id,Contact> acctsWithOpps = new Map<Id,Contact>(
        [SELECT Id,(SELECT Id FROM Boat) FROM Contact WHERE Id IN :Trigger.New]);

}
}

enter image description here

enter image description here

enter image description here

3
  • 1
    Welcome to SFSE. This code looks like it's incomplete; right now, it doesn't try to do something. Where did you get stuck when trying to finish this code? Have you completed Trailhead modules like Apex Triggers? The community can help you with specific issues you run into while you try to solve this problem and learn your way around, but it's not really best practice to just provide the solution. (Please make an edit to add information to your question)
    – David Reed
    Dec 9, 2019 at 2:14
  • 2
    If you're having a hard time just saving this trigger, look at this line: List<Contact> cList = new List<Opportunity>(); Can a List of Contacts ever be a List of Opportunities?
    – David Reed
    Dec 9, 2019 at 2:15
  • @DavidReed Hi David i am new here and with apex, yes i readed trailmex trigger but i cant not resolve this problem.
    – user76893
    Dec 9, 2019 at 3:31

2 Answers 2

0

Generally, when you want to update another object in trigger, you would do it in After event trigger.

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);
    }
}

The relationship name of Boats__r in the code above is just my assumption. You can find the exact name from the Custom Field Definition Detail page of the Boat's Location field.

6
  • for this example, Boats__r is location__c or contact__c? data type location__c = picklist and contact__c = data type = Lookup(Contact) I added images. thank you.
    – user76893
    Dec 9, 2019 at 16:04
  • Relationship can only be either Lookup or Master-Detail. So it's the lookup field (not the picklist). Dec 9, 2019 at 16:11
  • <code>trigger UpdateLocation on Contact (before insert) { if (trigger.isAfter && Trigger.isUpdate) { List<Boat__c> boats = new List<Boat__c>(); for (Contact c : [ SELECT location_boat__c, (SELECT Id FROM Contact__c) FROM Contact WHERE Id = :trigger.new ]) { for (Boat__c b : c.Contact__c) { b.location__c = c.location_boat__c; boats.add(b); } } update(boats); } }</code>
    – user76893
    Dec 9, 2019 at 16:29
  • console, Variable does not exist: Contact__c and Contact__c is name of relationship in the object boat, i dont understand :(
    – user76893
    Dec 9, 2019 at 16:31
  • You should be able to find the Child Relationship Name by clicking on the Contact__c field on the Boat object. If the child relationship name is "Boats", then the API name would be Boats__r. Dec 9, 2019 at 16:52
0

Yes your code doesn't make sense at all. There is data type mismatch (you can't assign Opportunity list to Contact list, is that a typo?). And you are not doing anything other than just quering contact records.

Try this (handle bulk updates too and each line is easy to understand, you can seach the few keywords which is used below to understand in detail)

trigger UpdateLocation on Contact (before update) {
    Map<Id, Contact> updatedLocationContacts = new Map<Id, Contact>();
    for (Contact c : Trigger.new) {
        if (c.location_boat__c != Trigger.oldMap.get(c.Id).location_boat__c) {
            updatedLocationContacts.put(c.Id, c);
        }
    }

    if (updatedLocationContacts.size() > 0) {
        List<Boat__c> relatedBoats = new List<Boat__c>([
            select Id, Contact__c, location__c from Boat__c where Contact__c IN :updatedLocationContacts.keySet()
        ]);

        for (Boat__c b : relatedBoats) {
            b.location__c = updatedLocationContacts.get(b.Contact__c).location_boat__c;
        }

        update boats;
    }
}
2
  • thanks but this problem could be solved with Trigger.isUpdate ? or I understand wrong the conceptof isUpdate?
    – user76893
    Dec 9, 2019 at 5:03
  • so you can handle update, delete, create all 3 in a single trigger class. Trigger.isUpdate is used to check if a trigger is called on record update action. In above class we have only before update so we don't need to check isUpdate. Dec 9, 2019 at 5:06

You must log in to answer this question.