+ Start a Discussion
Ravi_SFDCRavi_SFDC 

Sort Issue

User-added image

I am trying to update these numbers (sorting). If i see the repeated number it should give me an error only when i click the update button. Please find my APEX Code as below

public PageReference UpdateAction(){
        Savepoint sp = Database.setSavepoint();
        try{
            if (strType=='AP'){
            List<Company_Priorities__c> actPList = [select Field1__c, Name from Company_Priorities__c];
            integer i = 0; 
            integer j = 0;
            integer k = 0;
            for(i = 0; j< actPList.length–1; j ++);
               {
                    for(k=(i+1); j< actPList.length; j ++)
                    {
                       if(actPList[i].number == actPList[j].number)
                        {
                            Raise Error
                            Break;
                        }
                    }
                }
                update actPList;
I am getting error. Can anyone help.
Sagar vadariaSagar vadaria
Hi Venkata,

Instead of looping throough multiple for loop you can implement this logic by taking theleverage of Salesforce Collection Set. as given below.


01    public PageReference UpdateAction(){
            set<Field1__c:Data Type> duplicate_Check = new set<Field1__c:Data Type>();
            Boolean IsDuplicateLis = false;

02        Savepoint sp = Database.setSavepoint();
03        try{
04            if (strType=='AP'){
05            List<Company_Priorities__c> actPList = [select Field1__c, Name fromCompany_Priorities__c];
06            
07            For(Company_Priorities__c cp : actPList)
08            {
                      if(duplicate_Check.contains(cp.Field1__c)==false)   //checking if the cuurent element is already present or not
                      duplicate_Check.add(cp.Field1__c); 
                      else
                         {
                          Raise Error;
                          IsDuplicateLis =true;
                         }

                }
09                if(IsDuplicateLis ==false)
20                update actPList;
Ravi_SFDCRavi_SFDC

Hi Sagar, Thank you for the code. But i am getting the error as below (please refer to the below screen shot for the complete code). Please provide your inputs.

Error: Compile Error: expecting a semi-colon, found ':' at line 37 column 25


User-added image

Sagar vadariaSagar vadaria
When I say "Field1__c:Data Type", It means data type of a particular field for which you want to check the duplicate values.

For Example If you want to check whether list has duplicate Names or not then you would say:set<string> duplicate_Check = new set<string>(); because string is the data type of Name field.
So put the appropriate dataType of Field1__c in the defination of set and try to save. If it is number you can use integer while defining duplicate_Check set or if it is text then you should use string.
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm
For more information on how to use set check out above link. It will help you to understand how exactly set works in salesofrce and you will be able to implement this loginc whithout any hiccups. 
Ravi_SFDCRavi_SFDC

Thank you Sagar, sorry for the confusion. I have defined the data type as integer but it is giving the error as below. Can you please suggest.

Error: Incompatible element type Decimal for collection of Integer at line 46 column 21

User-added image

Ravi_SFDCRavi_SFDC
Hi Sagar, it should message me the if it is true or false o the visual force page.
Sagar vadariaSagar vadaria
ok..change the integer data type to Decimal and try to save
Ravi_SFDCRavi_SFDC
Hi Sagar, i have changed to decimal, but it is not displaying whether the number exists or a new sorting number. Can you please suggest where can make changes to the code so that it can display a message on the VF page whether the same number is repeated or a new number.
Ravi_SFDCRavi_SFDC
Hi Sagar, Can you please check my final code as below and do suggest
if (strType=='AP'){
                List<Company_Priorities__c> actPList = [select Field1__c from Company_Priorities__c];
                for(Company_Priorities__c cp : actPList)
                {
                    if(duplicate_Check.contains(cp.Field1__c)==false)   //checking if the cuurent element is already present or not
                    duplicate_Check.add(Integer.valueOf(cp.Field1__c));
                    else {
                            IsDuplicateLis =true;
                         }
                }
                    if(IsDuplicateLis ==false)
                    else{
                            ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists ');
                            ApexPages.addMessage(msg);
                    }
                    update actPList;
Sagar vadariaSagar vadaria
if (strType=='AP'){
02                List<Company_Priorities__c> actPList = [select Field1__c fromCompany_Priorities__c];
03                for(Company_Priorities__c cp : actPList)
04                {
05                    if(duplicate_Check.contains(cp.Field1__c)==false)   //checking if the cuurent element is already present or not
06                    duplicate_Check.add(Integer.valueOf(cp.Field1__c));
07                    else {
08                            IsDuplicateLis =true;
                                ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR,'This sorting number already exists ');
14                            ApexPages.addMessage(msg);
09                         }
10                }
11                    if(IsDuplicateLis ==false)
12                    update actPList;
Ravi_SFDCRavi_SFDC

Hi Sagar,

When i test this code, please check the scenarios

1. When i provide the same number (example 1, 1) it is not giving any error but returning back to the page.

2. If i give the new number (example: replace 5 with 6) it is not saving the new number too.

OUTPUT Should be as Below

1. No two sort numbers should be the same. (example: 1, 2, 3, 4, 5, etc :: But no 1, 1, 2, 3, 4, 4, 5) 

2. I should also be able to change the sorting sequence at the same time no two numbers should be the same.

Please suggest the code.