0

I am new to writting triggers so I could use your help with this one.

I have 2 object : Location and Listing. On the location object (pba__Location__c )I have this picklist Area__c. Every time when I have a value in this I want to update another picklist Area_location__c on the second object (pba__Listing__c)

There is also a lookup relationship between pba__Listing__c and pba__Location__c

This is my code si far but I get an error on line 19:

Field is not writeable: pba__Listing__c.Area_location__c

trigger listingAreaUpdate on pba__Listing__c (before insert) {

     //Update the Area_location__c base on the Area__c from the Location
    List<pba__Listing__c> listings = Trigger.new;
    Map<Id, pba__Listing__c> oldListings = null;
    if (!Trigger.isInsert) {
      oldListings = Trigger.oldMap;
    }

    for (pba__Listing__c listing : listings) {

      if (!Trigger.isInsert) {
        pba__Listing__c oldListing = oldListings.get(listing.Id);
        if (oldListing.Location__r.Area__c == listing.Location__r.Area__c) {
          continue;
        }
      }
      if (listing.Location__r.Area__c == null) {
        listing.Area_location__c = null;
        continue;
      }

      pba__Location__c l = [Select Area__c from pba__Location__c where Id =:listing.Location__c];
      if (l != null) {
        listing.Area_location__c = l.Area__c;
      }

    }
}
  • The above code is throwing an error because in the trigger.new you will get only LocationId. For location record values you need to perform a query on Location objects and fetch its value and then update that value on Associated child records. – Abhishek Daware Aug 29 '17 at 9:31
  • Hi Martin I am new to this can you please help me on how to do this – Bogdan Pascu Aug 29 '17 at 9:34
  • There are some issues with your trigger code (for example, you have a query inside of a loop, which is a bad idea), but we can address those later. The Field is not writable error shouldn't be an issue with your provided code. That error indicates that you either don't have permission to edit pba__Listing__c records, or that Area_Location__c is either a formula field, or its Field-Level-Security is set to "Read Only" for your profile. Can you check both of those things (to see if that's the cause of the specific error message you're reporting)? – Derek F Aug 29 '17 at 13:35
  • Also, are these two objects part of a managed package that you have installed? If so, you may not be able to change those field/object permissions. – Derek F Aug 29 '17 at 13:37
0

You need to write your trigger on child object not on parent. In your case pba__Location__c is child. I believe pba__Listing__c is your lookup field name in your child object. Here is the code which you can use for your reference:

trigger listingAreaUpdate on pba__Listing__c   (before insert, before update) {

//check contacts prior to insert or update for invalid data
Map<Id,string> mapId = new Map<Id,string>();
if(Trigger.isInsert){
For (pba__Listing__c   c : Trigger.New) {
    if(c.Area__c != null) {   
        mapId.put(c.pba__Location__c ,c.Area__c);
    }
}
}

if(trigger.isUpdate){
    For (pba__Listing__c   c : Trigger.New) {
    if(c.Area__c != null) {   
        mapId.put(c.pba__Location__c ,c.Area__c);
    }
}
}
List<pba__Location__c> accList = new List<pba__Location__c>();
for(pba__Location__c acc : [Select id,Area_location__c from pba__Location__c where Id IN: mapId.keyset()]){
    acc.Area_location__c = mapId.get(acc.Id);
    accList.add(acc);
}

update accList;

}

  • I believe is the other eay around the lookup relationship is from pba__Listing__c to pba__Location__c – Bogdan Pascu Aug 29 '17 at 14:16
  • I get this error: Invalid field pba__Listing__c for SObject pba__Location__c – Bogdan Pascu Aug 29 '17 at 14:20
  • I have updated the code accordingly – salesforce Developer Aug 29 '17 at 15:45
  • basically the key in Map should be your lookup field value – salesforce Developer Aug 29 '17 at 15:45
0

Please refer below code :

trigger listingAreaUpdate  on pba__Listing__c  (before insert, before update) {


//Collection of LocationId
Set<Id> setLocationId = new Set<Id>();    
Map<Id,Location__c> mapLocationIdToValues = New Map<Id,Location__c>();

for(pba__Listing__c listing : trigger.new){
    if(!string.isBlank(listing.Location__c)) setLocationId.add(listing.Location__c);
}

for(Location__c objLocation : [select Id, Area__c from Location__c where Id IN :setLocationId ]){
    mapLocationIdToValues.put(objLocation.Id,objLocation);
}

for(pba__Listing__c listing : trigger.new){

    if(mapLocationIdToValues.containsKey(listing.Location__c)){
        listing.Area_location__c  = mapLocationIdToValues.get(listing.Location__c).Area__c;
    }else{
        listing.Area_location__c = null;
    }
}

}

It will fulfill your requirement though if parent field i.e Area is updated then it won't replicate on child record. For same you need to write another trigger on the parent object.

  • Hi Daware I am getting this error on line 5: unexpected token: '(' – Bogdan Pascu Aug 29 '17 at 14:22
  • Edited my answer let me know if you find any other challenges. – Abhishek Daware Aug 30 '17 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.