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
RajashriRajashri 

Compare Picklist and MultiSelectPicklist values

Hi,

I have  custom object in Salesforce name  'Project' and It has a Picklist Field 'Proj Implementation' Type.

and my second custom object is name 'SEC' and it has picklist(Multiselect) field name 'SEC DD'.

I want to compare if  value of 'Proj Implementatuon Type' equals to 'SEC DD' then add some records.

How can i do this comparison.

Can anyone plz guide?

 

Thanks in advance.

Rajashri

Best Answer chosen by Admin (Salesforce Developers) 
JHayes SDJHayes SD

Because you have compare() bound to an apex:commandButton element, that function needs to return a PageReference object.  If you paste in the example function I orignally posted (comparePicklistValues()) you can then do something like the following:

 

public PageReference compare() {
	PageReference opptyPage = null;
        Boolean isMatch = comparePicklistValues();
	if (isMatch) {           
        	Project__c project;
		opptyPage = new ApexPages.StandardController(project).view();
      		opptyPage.setRedirect(true);
	}
	return opptyPage;
}

 

Keep in mind that if you use the example directly you will have to hardcode values in the SOQL queries. 

 

Regards, jh

All Answers

Ankit AroraAnkit Arora

If you are doing this using apex then single select picklist will return string and multi select will return values separated by ";". So you can split the multiselect values and compare it with single select value.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

RajashriRajashri

Thanks Ankit.

Can you please provide the code.I am new to it.

 

RajashriRajashri

Can anyone please provide me the solution?

JHayes SDJHayes SD

Hi Rajashri,

 

Is there a relationship defined between the two objects?  If so, which object is the parent and which is the child?

 

Thanks, Jeremy

JHayes SDJHayes SD

Here is a quick example:

 

public class PicklistExamplesController {
	public Boolean comparePicklistValues() {
		Boolean isMatch = false;
		Project__c p = [Select Name, Implementation_Type__c From Project__c Where Name='Test Project'];
		SEC__c s = [Select Name, SEC_DD__c From SEC__c Where Name='Test SEC'];		
		Set<String> dds = new Set<String>();
		dds.addAll(s.SEC_DD__c.split(';'));
		isMatch = dds.contains(p.Implementation_Type__c);
		return isMatch;
	}	
}

 If comparePicklistValues() returns true then the Implementation Type from the Project record matches one of the selected values on the SEC record.

RajashriRajashri

Hi Jeremy,

Thanks for the reply.

Below is my code.Can you please provide me the solution.There is no relationship defined between the objects.

Below is the complete code , i tried to integrate your code with my code but it is giving me an error that

"VisualForce action method must return page reference and not Boolean value java.lang.boolean"

public class newsecControllers{

public PageReference cancel() {
    return null;
    }
    //public PageReference compare() {
      public static Boolean compare()
    {
        //SEC__C sec;
         Boolean isMatch = false;
         Project__c p = [Select Name, Project_Implementation_Type__c From Project__c Where Name='Test DBS'];
          SEC__c s = [Select Name, SEC_DD_Level__c From SEC__c Where Name='DS-CD-00170'];
          Set<String> dds = new Set<String>();
          dds.addAll(s.SEC_DD_Level__c.split(';'));
          isMatch = dds.contains(p.Project_Implementation_Type__c);
          return isMatch;
           
          // Project__c project;
// pageReference opptyPage = new ApexPages.StandardController(project).view();
      //opptyPage.setRedirect(true);

     //return opptyPage;
}


           SEC__c sec;
   
      public newsecControllers() {

    }


    public newsecControllers(ApexPages.StandardController controller) {

    }
    public SEC__c getSec() {
      if(sec == null)
      sec = new SEC__c();
      return sec;
         
   }

 

   

}

 

 

<apex:page tabStyle="SEC__c" Controller="newsecControllers">

<script>

function confirmCancel() {

      var isCancel = confirm("Are you sure you wish to cancel?");

      if (isCancel) return true;

 

     return false;

  } 

 

 

</script>

  <apex:sectionHeader title="New SEC" subtitle="Step 1 of 1"/>

    <apex:form >

      <apex:pageBlock title="SEC Project Information" mode="edit">

      <apex:inputField id="employeefirstname" value="{!sec.SEC_DD_Level__c}"/>

     

      <apex:pageBlockButtons >

                <!-- <apex:commandButton action="{!cancel}" value="Cancel"  onclick="return confirmCancel()" immediate="true"/>-->

                 <apex:commandButton action="{!cancel}" value="Cancel"  onclick="return confirmCancel()" immediate="true"/>

                 <apex:commandButton action="{!compare}" value="Compare"  />

        </apex:pageBlockButtons>

        </apex:pageBlock>

       

 

</apex:form>

  </apex:page>

JHayes SDJHayes SD

Because you have compare() bound to an apex:commandButton element, that function needs to return a PageReference object.  If you paste in the example function I orignally posted (comparePicklistValues()) you can then do something like the following:

 

public PageReference compare() {
	PageReference opptyPage = null;
        Boolean isMatch = comparePicklistValues();
	if (isMatch) {           
        	Project__c project;
		opptyPage = new ApexPages.StandardController(project).view();
      		opptyPage.setRedirect(true);
	}
	return opptyPage;
}

 

Keep in mind that if you use the example directly you will have to hardcode values in the SOQL queries. 

 

Regards, jh

This was selected as the best answer