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
SFDC KeeperSFDC Keeper 

PLEASE HELP: Copy a mulit pick list Value in a new field

I am trying to figure out the code or if anyone who has done this before....
 
Trying to copy over a multi select field picklist into a field with certain criteria and condense the multi pick list into a 4 option trigger....that activates a flag of a different color on each option.
 
The scontrol would activate at the time they viewed the page.
 
Anyone out there
sfdcfoxsfdcfox
You can do this in an S-Control:
<script language="javascript" src="/soap/ajax/9.0/connection.js">
</script>
<script language="javascript">
try
{ if("{!Lead.Field_1__c}" != "{!Lead.Field_2__c}") { Lead = new sforce.SObject("Lead") Lead["Id"] = "{!Lead.Id}" Lead["Field_2__c"] = "{!Lead.Field_1__c}" sforce.connection.update([Lead])
alert("Lead Information Updated. Refreshing page.")
 window.top.location.href=window.top.location.href }
} catch(e)
{ // Let's not annoy the user with error messages.
}
With that code in your S-Control (be sure to get the actual field names), you can then add it to the page layout; Setup | App Setup | Customize | Leads | Page Layouts | Edit (Lead Layout). Use the field palette on the right ("View Lead Fields") to find the S-Control (use the dropdown next to "View" on the palette, change it to "S-Controls"). Once you add it to your page layout, double-click on it to view the properties, and change its' height to 0. This effectively "hides" the control on your layout so the code runs automatically without user interaction.

This S-Control checks to see if the value of "Field 1" is not equal to "Field 2", and if so, copies the value from the first field to the second using the AJAX Toolkit (the API). You can then use a workflow rule to trigger or something on the update of Field 2, or use formulas that reference Field 2. This will work for those times where you can not use a multi-select picklist effectively. Note that the format of the text will be "value 1;value 2;value 3", depending on the values selected.

This script reloads the page so as to show the new value of any calculated fields that would have happened because of the update. Reloading the page is optional if you don't care about the flag showing until the next time the field is viewed.

Feel free to send me a message if you get stuck, I'll see if I can't help you out.

~ sfdcfox ~

SFDC KeeperSFDC Keeper

Hey this is great....I have one more question though.  I would like to condense the list down to 4 categories.  Example:  If I choose the options in the list box that are the 1st 2nd and 4th, then I want to have the new field be populated at 1 then if I want options 3, 6, 7, choosen, I want to have 2 populated in the next box.  Sounds kinda messy, but would be really cool way to sort the multi pic list in to 4 categories.

Just curious..

Thank You,

 

sfdcfoxsfdcfox
Well, yes, you're right. Messy. But you can code any type of custom logic into the example I provided. Try this:

Code:
<script language="javascript" src="/soap/ajax/9.0/connection.js">
</script>
<script language="javascript">
try
{ var Field_1__c = "{!Lead.Field_1__c}"
  var Field_2__c = "{!Lead.Field_2__c}"
  var Temp_Value = ""
  switch(Field_1__c)
  { case "Value 1":
    case "Value 1;Value 3":
      Temp_Value = "Result 1";
      break;
    case "Value 2;Value 3":
      Temp_Value = "Result 2";
      break;
    case "Value 1;Value 4":
    case "Value 2;Value 4":
      Temp_Value = "Result 3";
      break;
    default:
      Temp_Value = "Result 4";
  }
  if(Temp_Value <> Field_2__c)
  { Lead = new sforce.SObject("Lead")
    Lead["Id"] = "{!Lead.Id}"
    Lead["Field_2__c"] = Temp_Value
    sforce.connection.update([Lead])
    alert("Lead Information Updated. Refreshing page.")
    window.top.location.href=window.top.location.href
} catch(e)
{ // Let's not annoy the user with error messages.
} 

 Obviously, this has some holes in it, but basically, if "value 1" or "value 1" and "value 3" are selected, the field value is "Result 1". If it's "value 2" and "value 3", it's "Result 2". If it's "Value 1" or "Value 2" and "Value 4" in either case, then it's "Result 3". Finally, if it's any other combination, it's "Result 4". This isn't the best way to doing this, either, but it is the most simple to understand. A better version would be one that checks for the presence of each value and does a bit more robust logic. But I'm just here to give you an example. If you need more help, just let us know.

~ sfdcfox ~