• Miles Sonnenfeld 2
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
I am trying to get the minimum date from sevral records.  My code has no Problems, but when I attempt to run I get an error...Please help as I am not sure why I can select a string using SOQL, but not a Date.

"Line: 7, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AfterTenantOpp: execution of AfterInsert caused by: System.StringException: Ending position out of bounds: 4 Class.TenantOpp_CarrierLeaseComplete.CarrierExecution: line 21, column 1 Trigger.AfterTenantOpp: line 24, column 1: []"

My Trigger...
trigger AfterTenantOpp on Tenant_Opportunities__c (after insert) {

    for(Tenant_Opportunities__c  TO:trigger.new) {
        String Recordid = TO.Id;
        String SiteID = TO.Site_ID__c;
        String Carrier = TO.Carrier_Tenant__c;
        Integer Count =[SELECT count() 
                        FROM Tenant_Opportunities__c 
                        WHERE Carrier_Tenant__c=:Carrier AND Site_ID__c =: SiteID];
        
        List<Site_ID__c> OppIDList = [SELECT Opportunity__c
                        			  FROM Site_ID__c
                        			  WHERE Id=:SiteID];
        String OppID = string.valueof(OppIDList[0]);
        
system.debug('RecordId: '+Recordid);
system.debug('SiteID: '+SiteID);
system.debug('Carrier: '+Carrier);
system.debug('Count: '+Count);
        TenantOpp_ProjectExt.NewProjectExt(Recordid, SiteID, Carrier, Count);
        TenantOpp_CarrierNumber.CarrierNumber(Recordid, SiteID, Carrier, Count);
        TenantOpp_CarrierLeaseComplete.CarrierExecution(Recordid, SiteID, Carrier, OppID);
  }
}

My Class....
 
public class TenantOpp_CarrierLeaseComplete {
    public static void CarrierExecution(String Recordid, String SiteID, String Carrier, String OppID){
        List<Tenant_Opportunities__c> CarrierLeaseCompleteList = [SELECT Carrier_Lease_Complete__c
                                         						  FROM Tenant_Opportunities__c
                                         						  WHERE Carrier_Lease_Complete__c != null AND Site_ID__c=:SiteID AND Carrier_Tenant__c=:Carrier
                                                                  ORDER BY Carrier_Lease_Complete__c LIMIT 1];
		String StringDateComplete = string.valueof(CarrierLeaseCompleteList[0]);
        system.debug('StringDateComplete: '+StringDateComplete);
		String DateYear = StringDateComplete.substring(51,4);  
        system.debug('DateYear: '+DateYear);
    }
}

 
Hey, I just finished writing some code to count the number of attachments with 'proposal' in the name.  I am still pretty new to APEX so please let me know if you have a way to make this work better.  Please see the Trigger, Class, & TestClass Below.

Trigger
trigger AfterAttachment on Attachment (after insert, after delete, after undelete) {
    if(Trigger.new<>null){
    	for(attachment a:trigger.new){
            String OppID = a.ParentId;
            Opp_ProposalAttachmentCount.CountAttachment(OppID);
        }
    }else if(Trigger.old!=null){
        for(attachment a: trigger.old){
            String OppID = a.ParentId;
            Opp_ProposalAttachmentCount.CountAttachment(OppID);
        }
    }
}
Class
public class Opp_ProposalAttachmentCount {
    public static void CountAttachment(String OppID){
        Integer ACount = [SELECT count() FROM Attachment WHERE name like '%Proposal%' AND ParentID =:OppID];
        system.debug('Count of attachements with Proposal in the name----->'+ACount);
        Opportunity O = [SELECT Proposal_Count__c FROM Opportunity WHERE Id =:OppID];
        O.Proposal_Count__c = ACount;
        update O;
    }
}
@IsTest Class
@IsTest
public class Test_AddDeleteAttachment {
    static testMethod void testAttachment(){
//Create Account
        Account a = new Account();
        	a.name = 'TestAccount';
        	a.Property_Owner_Type__c = 'other';
        	a.BillingStreet = '660 Newport Center Dr.';
        	a.BillingCity = 'Newport Beach';
        	a.BillingState = 'CA';
        	a.BillingPostalCode = '92660';
        	insert a;
//Create Opportunity
        Opportunity o = new Opportunity();
        	o.accountid = a.id;
        	o.name = 'TestOpportunity1';
        	o.stagename = '0 - Prospecting';
        	o.CloseDate = date.newInstance(2014, 9, 18);
        	o.Stage_Detail__c = 'Researching Opportunity';
        	o.Current_Status_Next_Steps__c = 'This is a Test Class';
        	o.Agreement_Type__c = 'MLA';
        	o.vertical__c = 'Sports';
        	o.venue_type__c = 'MLB';
        	insert o;
//Attach test file
		Attachment Attach = new Attachment();
            attach.Name = 'Proposal - Test';
            Blob bodyBlob=Blob.valueOf('proposal attachement test class');
            attach.body=bodyBlob;
            attach.ParentId=o.id;
            insert attach;
            delete attach;
    }
}


Thank you!


 
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})
Hello,

​I am trying to write a Test Class for a APEX trigger that looks up a value based on a picklist value.  The APEX trigger works, however I can't deploy it because I can't get higher than 50%.  Can anyone help me write the additional code for my test class for the following code?

>>>>>>>>>>>>>>>>APEX Trigger>>>>>>>>>>>>>>>>>
trigger UpdatePrice on X6_Remote_Unit__c (before insert, before update){
    Set<String> locationSet = new Set<String>();
    Map<String,Decimal> MapPrice = new Map<String,Decimal>();
    for (X6_Remote_Unit__c RU: trigger.new){
        if(!String.isBlank(RU.X1_OEMPartDescription__c)){
            locationSet.add(RU.X1_OEMPartDescription__c);
        }
    }
    if(!locationSet.isEmpty()){
        for(Deployment_Part_Price_List__c PartPrice : [SELECT Name, Price__c FROM Deployment_Part_Price_List__c WHERE Name IN :locationSet]){
            MapPrice.put(PartPrice.Name, PartPrice.Price__c);
            //Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
        }
    }
    for (X6_Remote_Unit__c RU: trigger.new){
        if(!String.isBlank(RU.X1_OEMPartDescription__c) && MapPrice != null && MapPrice.get(RU.X1_OEMPartDescription__c) != null){
                RU.X1_Total_Price__c = MapPrice.get(RU.X1_OEMPartDescription__c);
        }
        else{
            RU.X1_OEMPartDescription__c = null;
        }
    }
}
>>>>>>>>>>>>>>>>TEST CLASS>>>>>>>>>>>>>>>>>
@isTest
private class TestClassUpdatePrice{
    static testMethod void validateUpdatePrice(){
        User u = [SELECT name, Id FROM user LIMIT 1];
        system.runAs(u){
            Deployment_Costing__c DC=new Deployment_Costing__c(Name='APEX Test');
            insert DC;
            system.debug('----->>End Adding Deployment Cost'+DC.name);
            Deployment_Costing__c DCRead=[SELECT Name FROM Deployment_Costing__c WHERE name='APEX Test'];
            system.debug('----->>End Reading Deployment Cost'+DC.name);
            
            Deployment_Part_Price_List__c DPPL=new Deployment_Part_Price_List__c(Name='A', Price__c=999);
            insert DPPL;
            system.debug('----->>End Adding Price List'+DPPL.name);
            Deployment_Part_Price_List__c DPPLRead=[SELECT Name FROM  Deployment_Part_Price_List__c WHERE name='A'];
            system.debug('----->>End Reading Price List'+DC.name);
            
        X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', X1_OEMPartDescription__c='A',Deployment_Costing__c='APEX TEST');
        insert RU;
            system.debug('----->>End Adding RU'+RU.name);
            X6_Remote_Unit__c RURead=[SELECT Name FROM  X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
            system.debug('----->>End Reading RU'+DC.name);
        }
    }
}
I am trying to get the minimum date from sevral records.  My code has no Problems, but when I attempt to run I get an error...Please help as I am not sure why I can select a string using SOQL, but not a Date.

"Line: 7, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AfterTenantOpp: execution of AfterInsert caused by: System.StringException: Ending position out of bounds: 4 Class.TenantOpp_CarrierLeaseComplete.CarrierExecution: line 21, column 1 Trigger.AfterTenantOpp: line 24, column 1: []"

My Trigger...
trigger AfterTenantOpp on Tenant_Opportunities__c (after insert) {

    for(Tenant_Opportunities__c  TO:trigger.new) {
        String Recordid = TO.Id;
        String SiteID = TO.Site_ID__c;
        String Carrier = TO.Carrier_Tenant__c;
        Integer Count =[SELECT count() 
                        FROM Tenant_Opportunities__c 
                        WHERE Carrier_Tenant__c=:Carrier AND Site_ID__c =: SiteID];
        
        List<Site_ID__c> OppIDList = [SELECT Opportunity__c
                        			  FROM Site_ID__c
                        			  WHERE Id=:SiteID];
        String OppID = string.valueof(OppIDList[0]);
        
system.debug('RecordId: '+Recordid);
system.debug('SiteID: '+SiteID);
system.debug('Carrier: '+Carrier);
system.debug('Count: '+Count);
        TenantOpp_ProjectExt.NewProjectExt(Recordid, SiteID, Carrier, Count);
        TenantOpp_CarrierNumber.CarrierNumber(Recordid, SiteID, Carrier, Count);
        TenantOpp_CarrierLeaseComplete.CarrierExecution(Recordid, SiteID, Carrier, OppID);
  }
}

My Class....
 
public class TenantOpp_CarrierLeaseComplete {
    public static void CarrierExecution(String Recordid, String SiteID, String Carrier, String OppID){
        List<Tenant_Opportunities__c> CarrierLeaseCompleteList = [SELECT Carrier_Lease_Complete__c
                                         						  FROM Tenant_Opportunities__c
                                         						  WHERE Carrier_Lease_Complete__c != null AND Site_ID__c=:SiteID AND Carrier_Tenant__c=:Carrier
                                                                  ORDER BY Carrier_Lease_Complete__c LIMIT 1];
		String StringDateComplete = string.valueof(CarrierLeaseCompleteList[0]);
        system.debug('StringDateComplete: '+StringDateComplete);
		String DateYear = StringDateComplete.substring(51,4);  
        system.debug('DateYear: '+DateYear);
    }
}

 
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})
Hello,

​I am trying to write a Test Class for a APEX trigger that looks up a value based on a picklist value.  The APEX trigger works, however I can't deploy it because I can't get higher than 50%.  Can anyone help me write the additional code for my test class for the following code?

>>>>>>>>>>>>>>>>APEX Trigger>>>>>>>>>>>>>>>>>
trigger UpdatePrice on X6_Remote_Unit__c (before insert, before update){
    Set<String> locationSet = new Set<String>();
    Map<String,Decimal> MapPrice = new Map<String,Decimal>();
    for (X6_Remote_Unit__c RU: trigger.new){
        if(!String.isBlank(RU.X1_OEMPartDescription__c)){
            locationSet.add(RU.X1_OEMPartDescription__c);
        }
    }
    if(!locationSet.isEmpty()){
        for(Deployment_Part_Price_List__c PartPrice : [SELECT Name, Price__c FROM Deployment_Part_Price_List__c WHERE Name IN :locationSet]){
            MapPrice.put(PartPrice.Name, PartPrice.Price__c);
            //Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
        }
    }
    for (X6_Remote_Unit__c RU: trigger.new){
        if(!String.isBlank(RU.X1_OEMPartDescription__c) && MapPrice != null && MapPrice.get(RU.X1_OEMPartDescription__c) != null){
                RU.X1_Total_Price__c = MapPrice.get(RU.X1_OEMPartDescription__c);
        }
        else{
            RU.X1_OEMPartDescription__c = null;
        }
    }
}
>>>>>>>>>>>>>>>>TEST CLASS>>>>>>>>>>>>>>>>>
@isTest
private class TestClassUpdatePrice{
    static testMethod void validateUpdatePrice(){
        User u = [SELECT name, Id FROM user LIMIT 1];
        system.runAs(u){
            Deployment_Costing__c DC=new Deployment_Costing__c(Name='APEX Test');
            insert DC;
            system.debug('----->>End Adding Deployment Cost'+DC.name);
            Deployment_Costing__c DCRead=[SELECT Name FROM Deployment_Costing__c WHERE name='APEX Test'];
            system.debug('----->>End Reading Deployment Cost'+DC.name);
            
            Deployment_Part_Price_List__c DPPL=new Deployment_Part_Price_List__c(Name='A', Price__c=999);
            insert DPPL;
            system.debug('----->>End Adding Price List'+DPPL.name);
            Deployment_Part_Price_List__c DPPLRead=[SELECT Name FROM  Deployment_Part_Price_List__c WHERE name='A'];
            system.debug('----->>End Reading Price List'+DC.name);
            
        X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', X1_OEMPartDescription__c='A',Deployment_Costing__c='APEX TEST');
        insert RU;
            system.debug('----->>End Adding RU'+RU.name);
            X6_Remote_Unit__c RURead=[SELECT Name FROM  X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
            system.debug('----->>End Reading RU'+DC.name);
        }
    }
}