1

We have 2 custom objects Objective and Publisher both are unrelated object, we have a lookup to Publisher on Objective object. We have one Active Publisher for each campus. We are trying to populate active Publisher on to the lookup field on Objective based on Campus. I'm new in Apex, I'm trying to figure this out by writing a trigger on Objective. The below code is not populating the field Publisher name look up field. I could be completely lost in here. Can someone please guide on how to achieve this?

trigger UpdateObj on Objective__c (after insert) {

Set<id>ids=new set<id>();
For(Objective__c  obj:trigger.new){
    if(obj.Request_Center_LookUp__c!=Null){ //Request center is a parent object of Objective
        ids.add(obj.id);     
    }

 if(!ids.Isempty()){
list<publisher__c> NewPL=[select id,name from publisher__c where Mediapartner__c= TRUE AND 
Campus_Text__c = 'RUMC' AND Publisher_Status__c = 'Active' AND id in:Ids]; 

for(publisher__c publisher: NewPL){

  for(Objective__c  obj1: trigger.new){ 
   publisher.Id= obj.Publisher_Name__c;   
      }
    }
   }
 }
}
0

I strongly suggest you use trigger auto lunched FLOW ("Autolaunched Flow with a Record Trigger" fairly new functionality) instead of writing APEX trigger for that. The new trigger flows are made exactly for cases like that they will save you time and will serve you pretty good for the future.

See trail head:https://trailhead.salesforce.com/content/learn/modules/flow-builder

More details: https://help.salesforce.com/articleView?id=flow_concepts_trigger_record.htm&type=5

2
  • Is it okay to use Flow over Trigger? I've heard it runs little slow and takes couple of seconds to actually process. Is it best practice to use flow instead of Trigger given the slowness of Flow? – salmaan shaik Jan 19 at 17:04
  • Yes compared to a APEX trigger it could be a bit slower, but I do not thing it is something that would be significantly slower and it is less than 2 seconds. I think for simple operations it is better to go with a flow. SF main guidance "Writing code is a last resort" – Boris Gichev Jan 20 at 12:35
0

You're running an after insert trigger, which means you need to do a separate update DML command to actually make the update.

For this situation, if you just use before insert instead of an after insert, it should work as anticipated.

0

Yes, I recently did have the same requirement and I ended up doing it with a Flow, that was much easier and quicker. In my scenario I had two fields that were equals on each object so that was the match I used.

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.