You need to sign in to do that
Don't have an account?
Miles 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})
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})
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
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
I have updated you class code, Please find updated code attached.
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Regards,
Kapil
(forcecube@gmail.com)