2

I'm exploring Apex and I'd like to know the best approach to accomplish the following scenario:

Given a list of records, each having name and position (increment), I want once I update the position of a record the other records update themselves.

Example: a 1, b 2, c 3, d 4, e 5

I want to update D and set it to 2, so, I expect the following result:

a 1, D 2 , b 3, c 4, e 5

I'm thinking to use a Trigger to update the position. Is this the best approach or is there anything else?

Tkx

  • 1
    if using a trigger, you'll need to handle the use case where two or more elements in the batch update their position to contradictory values. You may need to define some rule that the highest (lowest) key or oldest (newest) key "wins" – cropredy Jul 20 '15 at 3:59
1

If you require this update to occur when a deletion of one of these records occurs as well, then I think a trigger is definitely your best option. If it is only when a record is updated then you could possibly use visual flow to accomplish the task combined with process builder it would work just like a trigger.

Put together a quick example of how this can work in flow, using accounts and contacts:

process builder
flow Before Jane's Change Position
After Jane's Change Positon

  • Hi Jenny, thank you for your suggestion, however despite the fact that the core duo "Visual Flow" and "Process Builder" can replace the Trigger in a bunch of User Cases, I don't think it can help in this specific ones. The problem is, we need hold the old value, add the new ones then loop the other values and update them with incremented values starting from the new updated field. – Wils Jul 19 '15 at 16:16
  • @Wils I put together a quick example of how this can be done in flow. – Jenny B Jul 19 '15 at 21:41
  • Hi Jenny, thank you for the update. I've accepted your answer but would u please confirm that it would work with more than 2 records, let's say 5 or more...If possible update the screen thus other people may use it in the future =D Tkx. – Wils Jul 20 '15 at 14:11
  • @Wils Updated with new screen shots. – Jenny B Jul 20 '15 at 14:19
0

Jenny B answer was very valuable however for now I've decided to use a Trigger. I will give another try to Visual WorkFlow later =D

trigger MCTRankUpdate on MCTRank__c (before update) {
    if(checkRecursive.runOnce() && Trigger.isUpdate) {
        List<MCTRank__c> oldRecord = Trigger.old;
        List<MCTRank__c> newRecord = Trigger.new;
        Integer newRankValue = Integer.valueOf(newRecord.get(0).Rank__c);
        MCTRank__c oldToBeUpdated = new MCTRank__c();
        Integer counter = newRankValue+1;
        List<MCTRank__c> recordsToUpdate = new List<MCTRank__c>();
        for(MCTRank__c m : [select id, name, Rank__c from MCTRank__c where Rank__c >= :newRankValue AND id <> :newRecord.get(0).id ORDER BY Rank__c ASC]) {
            m.Rank__c = counter;
            recordsToUpdate.add(m);
            counter++;
        }
        update recordsToUpdate;
    }
}

// Knowledge Article Number: 000133752 
public class CheckRecursive {
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

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.