+ Start a Discussion
Miles Sonnenfeld 2Miles Sonnenfeld 2 

Trying to get a string from a list

I have 4 tables that are associated with my opportunities.  Each of these need an accounting id to populate that increases in value as new records are created.  The format has been descided and can't be changed.  I want to create a APEX class (trigger will fire before/after insert) that will generate the next ID # (Project Agreement #) for that record.  The Project Agreement # must always have 3 digits (example: 001, 002, 003...010, etc)
1) get all records "Project Agreement #" where records match opportunity
2) Sort records decending and get first record
3) pull out string and convert to int
4) add 1 to int and convert back to 3 digit string format

My code is as follows....
Right now I am able to get a decensding list, however I can't pull the "0" record from the list and further just the "project_Agreement__c" field.

public class NewProjectExtension {
    public static void CreateExtension(String Carrier, id OpportunityID){
        
        if(Carrier=='1'){
            List<1Carrier_Sales__c> AgreementList = [SELECT Project_Agreement__c FROM 1Carrier_Sales__c WHERE Opportunity__c =: OpportunityID];
            AgreementList.sort();
            
            List<1Carrier_Sales__c> Desc_AgreementList = new List<1Carrier_Sales__c>();
            for(Integer i = AgreementList.size()-1;i>=0;i--){
                Desc_AgreementList.add(AgreementList[i]);
            
            }
            system.debug('Check Order -->'+Desc_AgreementList); //prints order of Project_Agreement__c
            
        }
    }
}

DEBUG LOG
18:32:43:017 USER_DEBUG [13]|DEBUG|Check Order -->(AT_T_Carrier_Sales__c:{Project_Agreement__c=004, Id=a0B2C0000007XpRUAU}, AT_T_Carrier_Sales__c:{Project_Agreement__c=003, Id=a0B2C0000007XpWUAU}, AT_T_Carrier_Sales__c:{Project_Agreement__c=002, Id=a0B2C0000007XpbUAE}, AT_T_Carrier_Sales__c:{Project_Agreement__c=001, Id=a0B2C0000007XpMUAU})
Best Answer chosen by Miles Sonnenfeld 2
Maharajan CMaharajan C
Hi Mlles,

Try the below changes to get the first value from list:

public class NewProjectExtension {
    public static void CreateExtension(String Carrier, id OpportunityID){
        
        if(Carrier=='1'){
            List<1Carrier_Sales__c> AgreementList = [SELECT Project_Agreement__c FROM 1Carrier_Sales__c WHERE Opportunity__c =: OpportunityID];
            AgreementList.sort();
            
            List<1Carrier_Sales__c> Desc_AgreementList = new List<1Carrier_Sales__c>();
            for(Integer i = AgreementList.size()-1;i>=0;i--){
                Desc_AgreementList.add(AgreementList[i]);
            
            }
            system.debug('Check Order -->'+Desc_AgreementList); //prints order of Project_Agreement__c

            Integer AddInt = Integer.ValueOf(Desc_AgreementList[0].Project_Agreement__c)+1;
            String str = String.ValueOf('00'+inte); //  In the above line the leading zeros will removed.
            system.debug('str --> ' + str); 


        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thank,
Raj

All Answers

Maharajan CMaharajan C
Hi Mlles,

Try the below changes to get the first value from list:

public class NewProjectExtension {
    public static void CreateExtension(String Carrier, id OpportunityID){
        
        if(Carrier=='1'){
            List<1Carrier_Sales__c> AgreementList = [SELECT Project_Agreement__c FROM 1Carrier_Sales__c WHERE Opportunity__c =: OpportunityID];
            AgreementList.sort();
            
            List<1Carrier_Sales__c> Desc_AgreementList = new List<1Carrier_Sales__c>();
            for(Integer i = AgreementList.size()-1;i>=0;i--){
                Desc_AgreementList.add(AgreementList[i]);
            
            }
            system.debug('Check Order -->'+Desc_AgreementList); //prints order of Project_Agreement__c

            Integer AddInt = Integer.ValueOf(Desc_AgreementList[0].Project_Agreement__c)+1;
            String str = String.ValueOf('00'+inte); //  In the above line the leading zeros will removed.
            system.debug('str --> ' + str); 


        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thank,
Raj
This was selected as the best answer
KapilCKapilC
Hi Miles,

I have updated you class code, Please find updated code attached.
 
public class NewProjectExtension {
    public static void CreateExtension(String Carrier, id OpportunityID){
        
        if(Carrier=='1'){
            List<1Carrier_Sales__c> AgreementList = [SELECT Project_Agreement__c FROM 1Carrier_Sales__c WHERE Opportunity__c =: OpportunityID];
            AgreementList.sort();
            
            List<1Carrier_Sales__c> Desc_AgreementList = new List<1Carrier_Sales__c>();
            for(Integer i = AgreementList.size()-1;i>=0;i--){
                Desc_AgreementList.add(AgreementList[i]);
            
            }
            system.debug('Check Order -->'+Desc_AgreementList); //prints order of Project_Agreement__c
			
			// Ok so Desc_AgreementList list have the Project_Agreement__c max element on it's 0 index(Desc_AgreementList[0].Project_Agreement__c). 
			// P.S.:Integer.ValueOf('005') will return 5 not 005.
			Integer projectAgId = Integer.ValueOf(Desc_AgreementList[0].Project_Agreement__c)+1;
            String NextProjectAgreementId = '';
            if(projectAgId <9){
               NextProjectAgreementId = '00'+String.ValueOf(projectAgId);
            }
            else  if(projectAgId >9 && projectAgId <100){
                  NextProjectAgreementId = '0'+String.ValueOf(projectAgId);
             }
              else{
                NextProjectAgreementId = String.ValueOf(projectAgId);
              }
              system.debug('NextProjectAgreementId:::::  ' + NextProjectAgreementId); 
            
        }
    }
}

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
Miles Sonnenfeld 2Miles Sonnenfeld 2
Thank you both for your reply!  Maharajan was first to post and it worked!  Thank you very much!