0

I am new to apex and need to write a trigger on the Contacts object to move the values of X_field (multipicklist) to Y_field (multipicklist) when Z_field (checkbox) is true. X_field should then be blank.

Likewise, when Z_field is changed to = FALSE, I need the values of Y_field (if ! Null) to move back to X_field leaving Y_field blank.

3
  • Are we talking 2 different multipicklist fields or the same multipicklit but moving the values from available->selected and vice versa? Nov 27 '14 at 4:56
  • Two different fields sorry. The same values exist in both fields. X_field remains on the page and Y_field sits in the backend for the sole purpose of capturing the information without users viewing it.
    – MickeyS
    Nov 27 '14 at 5:05
  • 3
    And have you tried anything? Folks at SFSE appreciate your initiative and then swoop in with assistance.
    – cropredy
    Nov 27 '14 at 5:13
2

Based on the comments, from what I understood, it's pretty simple:

trigger Contact on Contact (before insert, before update)
{
    for (Contact contact : trigger.new)
    {
        if (contact.Z__c == true && contact.X__c != null)
        {
            contact.Y__c = contact.X__c;
            contact.X__c = null;
        }
        if (contact.Z__c == false && contact.Y__c != null)
        {
            contact.X__c = contact.Y__c;
            contact.Y__c = null;
        }
    }
}

Replace the X, Y and Z fields with your own fields and that should do.

If that's not what you're after and you want to play around with the selected values of the fields only, then the values of the multipicklists are semi-colon separated strings, e.g. 'value1;value2;value3'.

2
  • trigger UpdateYonZtrigger on Contact(before insert, before update) { List<Contact> listContact = new List<Contact>{}; for(Contact sContact:Trigger.new) { if(sContact.Zfield == true) { Xfield = Yfield; } update sContact; } if(Zfield == false || Yfield != null) { Yfield = Xfield } update sContact; } }
    – MickeyS
    Nov 27 '14 at 5:29
  • based on your response i can see where i've gone wrong, lots to learn.
    – MickeyS
    Nov 27 '14 at 5:32

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.