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
The_A-RodThe_A-Rod 

Trigger check and wrong landing page

Hi all,

 

I'm totally new to Apex and am trying to leverage a template on another thread.

 

What I need is that whenever a custom picklist field called Software__c equals 'Fusion' and the record is updated on a custom object called User_Signature__c, a checkbox field called Fusion_User is ticked on the associated Contact.

 

The code below worked first time, which seems too good to be true, so I'm worried it's executing something I can't see or haven't noticed! Does it look correct?

 

The only other thing is that after the trigger updates the Fusion_User__c field, you're directed to the Contact. I need the page to stay on the User Signature record updated. How would I do that?

 

 

 

trigger IsFusionUser on User_Signature__c (before insert, before update) {

List<ID> ContactIds = New List<ID>();

  for(User_Signature__c o : Trigger.new){
    if(o.Software__c == 'Fusion' && o.Contact__c != null){
      ContactIds.add(o.Contact__c);
    }
  }

  List<Contact> ContactList = [SELECT id, Fusion_user__c FROM Contact WHERE id in :ContactIds];
  for(integer i = 0 ; i < ContactList.size(); i++){
    ContactList[i].Fusion_user__c = true;
  }

  update ContactList;
}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

To improve the efficiency you can simply query back those contacts where the field is currently false.

 

List<Contact> ContactList = [SELECT id, Fusion_user__c FROM Contact WHERE id in :ContactIds and Fusion_User__c = false];
  for(integer i = 0 ; i < ContactList.size(); i++){
    ContactList[i].Fusion_user__c = true;
  }

  update ContactList;

 

The trigger has no control over the landing page - there may not even be a page if the update is triggered from a data load for example. That side of things is handled by the platform if it is a standard page or the controller if it is a visualforce page.  

 

All Answers

bob_buzzardbob_buzzard

That code does what you are looking for.  It could be improved, as it is setting the Fusion_User__c field to true and updating the contact regardless of whether that field is already set, whereas it would be better to only update contacts where that field is currently false.  It doesn't seem like much of an overhead, but if you end up with a bunch of triggers firing when a contact is updated you can quickly burn through governor limits.

 

WRT the page, that isn't controlled by the trigger.  That is likely to be in the retURL parameter on the URL of  a standard page, or the action method of the associated controller if it is a visualforce page.

The_A-RodThe_A-Rod

Thanks Bob. We have very few triggers in our org, and the number of updates shouldn't trouble the limits. But thank you for highlighting it anyway

 

How would I ensure the field is only updated if it is false?

 

Is it possible to alter the trigger to divert the landing page back to the User Signature record and not the Contact?

bob_buzzardbob_buzzard

To improve the efficiency you can simply query back those contacts where the field is currently false.

 

List<Contact> ContactList = [SELECT id, Fusion_user__c FROM Contact WHERE id in :ContactIds and Fusion_User__c = false];
  for(integer i = 0 ; i < ContactList.size(); i++){
    ContactList[i].Fusion_user__c = true;
  }

  update ContactList;

 

The trigger has no control over the landing page - there may not even be a page if the update is triggered from a data load for example. That side of things is handled by the platform if it is a standard page or the controller if it is a visualforce page.  

 

This was selected as the best answer
The_A-RodThe_A-Rod

that's a great help

 

cheers