• aottaru
  • NEWBIE
  • 35 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 6
    Questions
  • 17
    Replies
Hello everyone,

I have been using a the following date format function to for format my date in the following format 'October 19, 2015'

The function has been working well but today we jsut found out that if the date selected is 2015-31-12, It is being formated as 'December 30, 2016' instead of 'December 30, 2015'

Does anyone knows what is causing this? 

This is how I am calling the function dateTimeScheduled.format('MMMM d, YYYY');
I have a custom object called Charter with a lookup relationship with accounts. I have created an apex trigger in opportunity to create Charter records. I am not getting any errors with the trigger but the records are also not created.

Does anyone knows why is this happening or is what I am trying to do even possible?
Does anyone know why I am getting the error "Didn't understand relationship 'dispatch__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

Here is the line of the code with the error.

List<Charter__c> Dispatch = [select id,(Select Id from dispatch__c WHERE Date_Scheduled__c != null)
                                 from Charter__c where ID IN: Trigger.NewMap.KeySet() ORDER BY Date_Scheduled__c];

Thanks.
Hello,

I get a deployment error code on saving apex class in Developer Console. Any help to resolve this error will be appreciated.


Error
Hello everyone,

I am getting the error below when trying to make changes to my test class in Sandbox. 
Has anyone seen this before. Do you know how to fix it.

Thanks
Deployment Error
I have written a trigger that updates a field in the parent record when the child record is updated.

The trigger is supposed to combine value entered in few fields in all child records associated to the parent record and then update a field in the parent object with this value.

The trigger is working 98% of the time, for the other time the field is not being updated properly. It appears there are some extra values added. I am not sure where they are coming from.

Can someone please help?

Below is my trigger. Thanks

trigger UpdateDispatches on Charter__c (before Update)
{
    //Declare variables
    String strScheduledDate ='';
    String strActualDispatch='';
    String strDispatchQuantity='';
    String strSchDate='';
    string strAcDate='';
    string Disp='';
   
    for(Charter__c c: Trigger.new)
    {
        //Selecting required fields from dispatches
         List<Dispatch__c> Dispatch = [select id,
                                       Date_Scheduled__c,
                                     Scheduled_Quantity_1__c,
                                    Scheduled_Quantity_2__c,
                                    Scheduled_Quantity_3__c,
                                    Scheduled_Quantity_4__c,
                                       Description__c,
                                       Actual_Date__c,
                                    Actual_Quantity_1__c, 
                                    Actual_Quantity_2__c,
                                    Actual_Quantity_3__c,
                                    Actual_Quantity_4__c
                                    from Dispatch__c where Charter__c = :c.Id ORDER BY Date_Scheduled__c ASC];
        for(Dispatch__c d: Dispatch)
        {
            //Going through each dispatches and assign values
             Integer intMonthIn;
             Integer intDayIn;
             Integer intYearIn;
             String strMonth;
             String StrQuantities;
         
             String strActualDate;
             intMonthIn =  d.Date_Scheduled__c.Month();
             intDayIn = d.Date_Scheduled__c.Day();
             intYearIn = d.Date_Scheduled__c.Year();
             strMonth = MonthString(intMonthIn);
             string scd;
             string acd;
            
            If(d.Description__c == null)
            {
                scd= 'Scheduled Date : ' +  strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
            }else
            {
                scd= d.Description__c +' : '+  strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
            }
             strScheduledDate = scd;
             disp = disp +'\n'+ strScheduledDate.substringBefore('/ null');
             If(String.valueOf(d.Actual_Date__c) == null)
                 {
                    
                 }else
                     {
                        intMonthIn =  d.Actual_Date__c.Month();
                        intDayIn = d.Actual_Date__c.Day();
                        intYearIn = d.Actual_Date__c.Year();
                        strMonth = MonthString(intMonthIn);
                        //acd = 'Actual Date : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c)+'\n';            
                     acd = '- Actual Date'+' : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c);
                     }
          
             if(acd ==null)
                 {
                  disp = disp +'\n';  
                 }
             else
                 {
                     strActualDispatch = acd;
                     disp = disp+'\n' + strActualDispatch.substringBefore('/ null')+'\n';
                 }
        } 
    } 
    for(Charter__c c: trigger.new)
         {
              c.Dispatch_Schedule__c = disp.remove('null');
         }
    public String MonthString(Integer intMonth)
    {
        //This method will return a string value of the month
         If(intMonth == 1)
         {
             return 'Jan';
         }else if (intMonth == 2)
         {
             return 'Feb';
         }
        else if (intMonth == 3)
         {
             return 'Mar';
         } else if (intMonth == 4)
         {
             return 'Apr';
         }
              else if (intMonth == 5)
         {
             return 'May';
         } else if (intMonth == 6)
         {
             return 'Jun';
         }else if (intMonth == 7)
         {
             return 'Jul';
         }
         else if (intMonth == 8)
         {
             return 'Aug';
         }
         else if (intMonth == 9)
         {
             return 'Sep';
         }
         else if (intMonth == 10)
         {
             return 'Oct';
         }
         else if (intMonth == 11)
         {
             return 'Nov';
         }
         else if (intMonth == 12)
         {
             return 'Dec';
         }   
        return '';
    }
}

I have written a trigger to fill some lookup fields depending on a given number

it worked fine until you try to mass update.

here is the trigger

trigger getTradingAccountUPD on Case (before update) {
List<Contract> abc = new List<Contract>();
List<Account> mta = new List<Account>();

for(Case oldCase :trigger.old){
    if(oldCase.Trading_Account__c == null && oldCase.Master_trading_Account__c == null && oldCase.Customer__c == null){

for(Case nCase :trigger.new){
    if(nCase.Legacy_Account_Number__c != null){
        nCase.System_Notification__c ='';
        abc = [SELECT Id,AccountId FROM Contract WHERE Legacy_Account_Number__c =: nCase.Legacy_Account_Number__c LIMIT 1];
        if(abc.size()>0){
           
              nCase.Trading_Account__c = abc[0].Id;
              nCase.Master_Trading_Account__c = abc[0].AccountId;
              mta = [SELECT ID,ParentId FROM Account WHERE Id =: abc[0].AccountId LIMIT 1];
              if(mta[0].Id != null) {
                  nCase.Customer__c = mta[0].ParentId;    
                  }  
            }
         if(abc.size() == 0) {
             mta = [SELECT Id,ParentId FROM Account WHERE Legacy_Account_Number__c =: nCase.Legacy_Account_Number__c LIMIT 1];
             if(mta.size() >0) {
             nCase.Master_Trading_Account__c = mta[0].Id;
             if(mta[0].ParentId != null) {
                 nCase.Customer__c = mta[0].ParentId;
                 }
             }    
          }
          if((abc.size()==0) && (mta.size()==0)){
              nCase.System_Notification__c = '(NOT FOUND IN DATABASE)';
              }
        }
     }
   }
  }
}

 Okay I saw that the select query is in the loop so i moved my head around and came up with the following

 

 

trigger getTradingAccountUPD on Case (before update) {

List<Account> cus = new List<Account>();

Set<id> mta = new Set<id>();
Map<id,String> cases = new Map<id,String>();

 for(Case oldCase :trigger.old){
    if(oldCase.Trading_Account__c == null && oldCase.Master_Trading_Account__c == null && oldCase.Customer__c == null){
       for(Case ncase :trigger.New){
          cases.put(ncase.Id,ncase.Legacy_Account_Number__c);
          ncase.System_Notification__c = '';
       }
    }
 }
Set<Contract> ta = new Set<Contract>([SELECT Id,AccountId,Legacy_Account_Number__c FROM Contract WHERE Legacy_Account_Number__c IN : cases.values() Limit 1]);
If(!ta.isEmpty()){
  for(Case ncase :trigger.New){
    if(ncase.Legacy_Account_Number__c != null){
        for(Contract lan : ta){
            if(ncase.Legacy_Account_Number__c.equals(lan.Legacy_Account_Number__c)){
                ncase.Trading_Account__c = lan.id;
                mta.add(lan.AccountId);
            }
        }
    }
   }
 }
 if(!mta.isEmpty()){
     List<Account> acc = new List<Account>([SELECT Id,ParentId FROM Account WHERE Id IN : mta]);
     for(Case ncase :trigger.New){
       ncase.Master_Trading_Account__c = acc[0].Id;
       if(acc[0].ParentId != null){
           ncase.Customer__c = acc[0].ParentId;
           }
       }
  }      
 if(ta.isEmpty() && mta.isEmpty()){
     List<Account> amaster = new List<Account>([SELECT Id,ParentId,Legacy_Account_Number__c FROM Account WHERE Legacy_Account_Number__c IN : cases.values()]);
        if(!amaster.isEmpty()){
          for(Case ncase :trigger.New){
            ncase.Master_Trading_Account__c = amaster[0].Id;
            if(amaster[0].ParentId != null){
              ncase.Customer__c = amaster[0].ParentId;
            }
          }
        } 
        else if ((amaster.isEmpty()) &&(ta.isEmpty() && mta.isEmpty()) ){
          for(Case oldCase :trigger.old){
              if(oldCase.Trading_Account__c == null && oldCase.Master_Trading_Account__c == null && oldCase.Customer__c == null){
                 for(Case ncase :trigger.New){
                    ncase.System_Notification__c = 'NOT FOUND';
                 }
               }  
           } 
         }    
 }
    
           
}

 but now my test class runs into the same System.LimitException: Too many SOQL queries: 101 issue at

Set<Contract> ta = new Set<Contract>([SELECT Id,AccountId,Legacy_Account_Number__c FROM Contract WHERE Legacy_Account_Number__c IN : cases.values() Limit 1]);

 

Any Idea how can I avoid that issue, because there are regular updates with the data loader

 

some backgroud

the account number is an external id which will be pulled into the legacy_account_number_ c

 

this number is pointing to a Contract object ( unique ), depending on that contract I need to pull the

ID of the related Account Object from that Contract and if any the parent account of that account.

 

I just wonder if there is anything else I could render to avoid the SQL limits

 

Thanks in advance

 

my testclass looks like that

@isTest(SeeAllData=true) 
public class getTradingAccountTest {
static testMethod void test_getTradingAccount(){
 Test.startTest();
        Case cs = new Case();
        cs.email__c = 'test.test@gmail.com';
        cs.Preferred_Reply_Method__c = 'Letter';
        cs.Post_Code__c = '95050';
        cs.Phone__c = '408123123';
        cs.First_Line_of_Address__c = 'test address';
        cs.Tracking_Job_Number__c = '1234567';
        cs.Consignment_Number__c = '24242323';
        cs.description= 'test';
        cs.Account_Number__c = '38J010W';
        insert cs;
        
        Case ds = new Case();
        ds.email__c = 'test.test@gmail.com';
        ds.Preferred_Reply_Method__c = 'Letter';
        ds.Post_Code__c = '95050';
        ds.Phone__c = '408123123';
        ds.First_Line_of_Address__c = 'test address';
        ds.Tracking_Job_Number__c = '1234567';
        ds.Consignment_Number__c = '24242323';
        ds.description= 'test';
        ds.Account_Number__c = 'test0001';
        insert ds;
        
        Case es = new Case();
        es.email__c = 'test.test@gmail.com';
        es.Preferred_Reply_Method__c = 'Letter';
        es.Post_Code__c = '95050';
        es.Phone__c = '408123123';
        es.First_Line_of_Address__c = 'test address';
        es.Tracking_Job_Number__c = '1234567';
        es.Consignment_Number__c = '24242323';
        es.description= 'test';
        es.Account_Number__c = 'rolle roe';
        insert es;
        
        //for code coverage in the controller
        NewCaseController x = new NewCaseController();
        x.trackAccount(cs);
        x.trackAccount(ds);
        x.trackAccount(es);
        //end of code coverage in controller
        
        es.Legacy_Account_Number__c = '38J010W';
        update es;
        es.Legacy_Account_Number__c=null;
        es.Trading_Account__c=null;
        es.Master_Trading_Account__c=null;
        es.Customer__c=null;
        update es;
        es.Legacy_Account_Number__c='test0001';
        update es;
        es.Legacy_Account_Number__c=null;
        es.Trading_Account__c=null;
        es.Master_Trading_Account__c=null;
        es.Customer__c=null;
        update es;
        es.Legacy_Account_Number__c='nope';
        update es;
        
        Test.stopTest();
}
}