You need to sign in to do that
Don't have an account?
Ravi_SFDC
Sort Issue
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.
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;
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
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.
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
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;
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.