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
Ravi_SFDCRavi_SFDC 

Sorting Code Issue

User-added image

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

User-added image

I have defined the data type as integer but it is giving the error as above and it should message me that if it is true or false on the visual force page. Can you please suggest.

Best Answer chosen by Ravi_SFDC
badibadi
okay, 
1. No issues here as you are not able to sort with 2 same numbers and error is displaying. Right?
2. To display success message add 
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.INFO, 'Saved! Sort numbers have been changed successfully');
            ApexPages.addMessage(msg);

after the  update actPList; in the IF block. 
3. I am assuming 
return new PageReference('/'+AccountPlan.id);

will return to the the record detail page. doing so will not display the success/error message. You can add a output panel which has back button to go back to previous page

Not knowing how your application is designed is a little hard there may be other better options too. 

All Answers

badibadi
Venkata,

Number fields are saved as Decimal in salesforce.
So change your line to
 
duplicate_Check.add(Integer.valueOf(cp.Field1__c));

 
Ravi_SFDCRavi_SFDC
Hi Badi, Thank you for the solution. But how can i display a message on the visual force page if the number (sorting number) already exist. Can you please suggest with the code. 
badibadi
Sure, add the following code in the else statement block 
if(IsDuplicateLis == false){
   //update
}else{
     ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists ');
            ApexPages.addMessage(msg);
}

in VF page 
make sure you have 
<apex:pageMessages/>

 
Ravi_SFDCRavi_SFDC

Thank you Badi. I am still getting an error as below.

Error: unexpected token: 'else' at line 65 column 20

User-added image

 

Please check the whole APEX Code and do suggest.

if (strType=='AP'){
                List<Company_Priorities__c> actPList = [select Field1__c from Company_Priorities__c];
                System.Debug('= = = = = = = ='+actPList );
                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;
badibadi
"update actPList" needs to be in if block
 
if(IsDuplicateLis ==false){
    update actPList;
}else{ 
     ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists ');               
     ApexPages.addMessage(msg); 
​}

make this change and try again, let me know if it doesn't work
Ravi_SFDCRavi_SFDC
User-added image

Hi Bhavana, still have an issue, please find the code as below
try{
            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){
                            update actPList;
                    }else{
                            ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists ');              
                            ApexPages.addMessage(msg);
                    ​}
badibadi
Looks like there is a " (double quotes) somewhere in line 68 I cannot see that in the code you have pasted, can you do a search for double quotes? if that does not work, remove the if, else block and try to save the file.  If it saves with no error, rewrite if and else block
Ravi_SFDCRavi_SFDC

Hi, Please see the final code. As i was not able to final any '' (double quotes). But the below code is saved with no issues. Nothing is getting changed. 

try{
            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){
                            update actPList;
                    }else{
                            ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists');              
                            ApexPages.addMessage(msg);
}

 

My Requirement - Please see the below screen shot

User-added image

When i click on the UPDATE button, it should check for the sorted numbers. If the user is trying to save any of the existing sorted numbers with the same number it should give me an error, or else save with the updated (changed sorted numbers) with successfully message. Kindly suggest with the code.

badibadi
Can you post your visualforce code for this functionality?
Ravi_SFDCRavi_SFDC

My VisualForce Page Code

<apex:page standardController="Account_Plan__c" extensions="AccountPlanSort" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
        <!--Begin of Account Priority Block-->  
        <apex:pageBlock title="Sorting Account Priorities for {!Account_Plan__c.Name}" mode="edit" rendered="{!If(strType == 'AP', true, false)}">
                <apex:messages />
                    <apex:pageBlockButtons location="top">
                        <apex:commandButton value="Update" action="{!UpdateAction}"/>
                        <apex:commandButton value="Cancel" action="{!Cancel}"/>
                    </apex:pageBlockButtons>
                    <apex:pageBlockTable value="{!actPList}" var="item">
                        <apex:column headervalue="#">
                            <apex:inputField value="{!item.Field1__c}" style="width:25px"/>
                        </apex:column>
                        <apex:column value="{!item.Name}"/>
                    </apex:pageBlockTable>
            </apex:pageBlock>
        <!--End of Account Priority Block-->
        
    </apex:form>
</apex:page>


My APEX Code too

public with sharing class AccountPlanSort {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccountPlan {get; set;}
    public list<Company_Priorities__c> actPList  {get; set;}    //Account Priorities Sort
    public string strType {get; set;}

    public AccountPlanSort (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        this.stdController = stdController;
        this.AccountPlan = (Account_Plan__c)this.stdController.getRecord();
        //actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccountPlan.Id ORDER BY Field1__c ASC ];
        actPList = [select Field1__c, Name from Company_Priorities__c where Account_Plan__c =: AccountPlan.Id ORDER BY Field1__c ASC ];
        strType = ApexPages.currentPage().getParameters().get('type');
    }

    public PageReference UpdateAction(){
            set<Decimal> duplicate_Check = new set<Decimal>();
            Boolean IsDuplicateLis = false;
            Savepoint sp = Database.setSavepoint();
        try{
            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){
                            update actPList;
                    }else{
                            ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists');              
                            ApexPages.addMessage(msg);
}
            }
catch(Exception e){
            System.debug('H'+e.getMessage());
            Database.rollback(sp);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
        }
        return new PageReference('/'+AccountPlan.id);
    }
}
badibadi
to be clear, you want to make sure no two sort numbers are same?
or do you want to check say ABC was in sort number 1 and after changing it is not in number 1 any more?
Ravi_SFDCRavi_SFDC
1. Yes, No two sort numbers are 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.
Ravi_SFDCRavi_SFDC
1. Yes, No two sort numbers are 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.
Ravi_SFDCRavi_SFDC
1. Yes, No two sort numbers are 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.
badibadi
I have modified the code a little bit, let me know if it works 

VF
<apex:page standardController="Account_Plan__c" extensions="AccountPlanSort" tabStyle="Account" id="thePage">
    <apex:form id="theForm" >
	<apex:pageMessages />
        <!--Begin of Account Priority Block-->  
        <apex:pageBlock title="Sorting Account Priorities for {!Account_Plan__c.Name}" mode="edit" rendered="{!If(strType == 'AP', true, false)}">
                    <apex:pageBlockButtons location="top">
                        <apex:commandButton value="Update" action="{!UpdateAction}"/>
                        <apex:commandButton value="Cancel" action="{!Cancel}"/>
                    </apex:pageBlockButtons>
                    <apex:pageBlockTable value="{!actPList}" var="item">
                        <apex:column headervalue="#">
                            <apex:inputField value="{!item.Field1__c}" style="width:25px"/>
                        </apex:column>
                        <apex:column value="{!item.Name}"/>
                    </apex:pageBlockTable>
            </apex:pageBlock>
        <!--End of Account Priority Block-->
        
    </apex:form>
</apex:page>

Apex
 
public with sharing class AccountPlanSort {    

    public ApexPages.StandardController stdController {get; set;}
    public Account_Plan__c AccountPlan {get; set;}
    public list<Company_Priorities__c> actPList  {get; set;}    //Account Priorities Sort
    public string strType {get; set;}

    public AccountPlanSort (ApexPages.StandardController stdController){
        System.debug('Is it in the Constructor');
        stdController = stdController;
        AccountPlan = (Account_Plan__c)this.stdController.getRecord();
        //actPList = [select Field1__c,Account_Priorities__c from Company_Priorities__c where Account_Plan__c =: AccountPlan.Id ORDER BY Field1__c ASC ];
        actPList = [select Field1__c, Name from Company_Priorities__c where Account_Plan__c =: AccountPlan.Id ORDER BY Field1__c ASC ];
        strType = ApexPages.currentPage().getParameters().get('type');
    }

    public PageReference UpdateAction(){
            set<Decimal> duplicate_Check = new set<Decimal>();
            Boolean IsDuplicateLis = false;
            Savepoint sp = Database.setSavepoint();
        try{
            if (strType=='AP'){
               // List<Company_Priorities__c> actPList = [select Field1__c from Company_Priorities__c]; you do not need this line
                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){
						update actPList;
			}else{
					ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'This sorting number already exists');              
					ApexPages.addMessage(msg);
			}
		}catch(Exception e){
			System.debug('H'+e.getMessage());
			Database.rollback(sp);
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
		}
        //return new PageReference('/'+AccountPlan.id);
		return null;
    }
}

 
Ravi_SFDCRavi_SFDC

Hi Badi, Thank you for the response.

1. If i am trying to save the two sort numbers as same, it is not taking, Important thing is it is not giving any message on the page. (Which is actually needed) 

2. I am able to change the sorting sequence but remaining same as in point number 1.(
Hear too no successful message displays)

Please suggest.

badibadi
Did you try with the code I have pasted above?

Please make sure <apex:pageMessages /> is on your Visualforce page (refer to my code pasted in my previous repsonse)  and instead of your page returing 
return new PageReference('/'+AccountPlan.id);
try return null

Let me know if that fixes the issue

 
Ravi_SFDCRavi_SFDC

Hi Badi, I do have <apex:pageMessages /> in my VF page.

Now i checked and made changes and tried with return null it is working fine with no issues.

1. When 
i have the 2 sort numbers with the same value it is giving me error message., that sorting number already exists.

2. When i have the sorting sequence changed it is working fine with no success message. 

3.  But in both (1 & 2) cases it should return to the previous page after following the (1 & 2 steps). Please suggest.

badibadi
okay, 
1. No issues here as you are not able to sort with 2 same numbers and error is displaying. Right?
2. To display success message add 
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.INFO, 'Saved! Sort numbers have been changed successfully');
            ApexPages.addMessage(msg);

after the  update actPList; in the IF block. 
3. I am assuming 
return new PageReference('/'+AccountPlan.id);

will return to the the record detail page. doing so will not display the success/error message. You can add a output panel which has back button to go back to previous page

Not knowing how your application is designed is a little hard there may be other better options too. 
This was selected as the best answer