• Katia Hage
  • NEWBIE
  • 60 Points
  • Member since 2011

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 16
    Replies

Hi guys

 

Reading the Apex Develpers Guide.  It says "A list can contain up to four levels of nested collections inside it." (p43 in the pdf file for Winter 13 version).   Since I wanted to be 100% sure, I went to the Developer Console and ran this code:

 

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

 

To me, that's 4 levels.  And as expected, the code compiled.

 

I added one more level, so my code became this, and I expected it to crash.

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

 

No, it didn't crash.

 

I finally added one more level:

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

List<List<List<List<List<List<Integer>>>>>> f = new List<List<List<List<List<List<Integer>>>>>>();
f.add(d);

 

And I finally got an error that said:

line -1, column -1: Nested type exceeds maximum level: 5

 

That is not quite what the book said.

 

I then googled a bit and realized according to Database.com page (here at http://docs.database.com/dbcom/en-us/db_apex/langCon_apex_collections_lists.htm?version=178.15), the list can contain up to 5 levels of nested collections.  There seems to be some inconsistency.  

 

Can anyone verify?  Has something been updated to Apex lately?

 

Thanks a lot.

King

 

 

 

 

 

 

 

 

According to (www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm, see Rules and Guidelines section), the upsert operation supports use of External IDs in reference fields. That is,

I'm inserting an Opportunity and I need to specify the related Account. However, I have only the External Id value of Account. Usually, I would query the Accout's Id, the use it to insert a new Opportunity but the Salesforce API lets you skip this step - (see http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_upsert.htm)

However, I'm curious to know how this can be done via Apex code - the URL above talks about the Web Services API which is in Java or other environment code.

Has anyone been able to do that?

 

  • May 10, 2012
  • Like
  • 0
Hello All,
We have two custom objects called Financial_Account__c and Household__c with a junction object called household_account__c.
On the household object we have a checkbox field called Is_new_Household__c.
On the Financial Account object we have a date field called Account_Open_Date__c.
I am trying to write a class that checks all financial accounts associated to a household which has been modified within the last day and checks if any of them have an open date which is prior to the household's creation date. If so, the household will not be marked as new.

Here is what I have so far:

global class NewHouseholdBatch implements Database.Batchable<sObject>{
 
     global NewHouseholdBatch(){
                // Batch Constructor
     }
     
     // Start Method
     global Database.QueryLocator start(Database.BatchableContext BC){
         String query = 'SELECT Id, Is_New_Household__c, CreatedDate FROM Household__c WHERE SystemModStamp > :Datetime.now().addDays(-1)'
      return Database.getQueryLocator(query);
     }
    
   // Execute Logic
    global void execute(Database.BatchableContext BC, List<Household__c>scope){
        for (Household__c hh : scope){
            Boolean isnew = true;
            List<Household_Account__c> HouseholdAccounts = [SELECT Household__c.id, Financial_Account__c.id FROM Household_Account__c WHERE Household__c.id = hh.id ];
            for (Household_Account__C hha : HouseholdAccounts ){
                List<Financial_Account__c> FinAccount = [SELECT Account_Open_Date__c, AAC_Account_Status__c FROM Financial_Account__C WHERE Id = hha.Financial_Account__c.id LIMIT 1];
                For (Financial_Account__C fa : FinAccount){
                    if (fa.Account_Open_Date__c < hh.CreatedDate){
                        isnew = false;
                    }
                }
                
            }
            if (isnew==false){
                hh.Is_New_Household__c =false;
            }
        }    
   
    }
   
    global void finish(Database.BatchableContext BC){
         // Logic to be Executed at finish
    }
 }


The developer console keeps saying "unexpected token: 'return' "
I am cofused about this error.
Also, do I need to appent the '.id' to the ennd of the name of the objects in my query on the household account object?

Thank you.
i am trying to fetch fields from a aggregteresult but it is giving me error :Invalid field Time_Account_Mapping__c for SObject AggregateResult
 
Time__c tes = [select Time_Account_Mapping__c from Time__c order by LastModifiedById desc limit 1];
     
        AggregateResult obj = [Select Time_Account_Mapping__c,sum(Hours__c) from time__c group by Time_Account_Mapping__c having Time_Account_Mapping__c in (:tes.Time_Account_Mapping__c)];

	Account ppla = new Account();
        ppla.id = obj.Time_Account_Mapping__c;
        ppla.Total_Client_Hour__c=obj.sum(Hours__c);
    //    ppla.Total_Client_Hour__c = test.Unknown_Field__1;
        update ppla;
Help me out here.
Thanks
Hi ,

I am trying to create a mock response for Http request , I receive the compile error as display below.
Type is not visible: httpcalloutmock

The class is :

@istest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
       System.assertEquals('http://test.bhtp.com/api/quotes', req.getEndpoint());
        System.assertEquals('Post', req.getMethod());
       
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(mockJSON);
        res.setStatusCode(200);
        return res;
    }
}

Any help would be appreciated .

Thank you
Hii Friends

I am not familiar  at writing test clas i got arequirement
so please kindly  help me in writing this test class

this  is my class 

public class EF_Approval_Utility {
  public static string currentuser;
  public EF_Requested__c efreq = new EF_Requested__c();
  public List<EF_Request_Approve_Controller.EF_ApprovalHistory> lstApprovalHistory {get;set;}
  public EF_Approval_Utility()
  {
   
  }
  public void captureApprovalHistory(List<EF_Request_Approve_Controller.EF_ApprovalHistory> approvalHistory, String Id)
  {
    try
    {
    String historyfield='';
    for(EF_Request_Approve_Controller.EF_ApprovalHistory ah:approvalHistory)
    {
       String s = '';
       s = ah.CreatedDate +''+ah.AssignedTo+''+ah.RequestorComments+ah.ApprovedRejectedBy+ah.ApproverComments;
      historyfield = historyfield+s;
    }
    efreq = [Select Approval_Comments__c from EF_Requested__c where Id=:Id];
    efreq.Approval_Comments__c=historyfield;
    update efreq;
    }catch(Exception e)
    {
      Apexpages.addMessages(e);
    }
  }
}




Thanks in advance 
  • March 13, 2014
  • Like
  • 1

Hi All,

 

I need your attention for unknown functionality for me of Salesforce.com/Force.com platform. I am doing UPSERT without External ID on an object. I have a custom object "Sales Order Summary" which is a child object of Account via Master-Detail relationship.

 

I am doing UPSERT with standard Name field of custom object "Sales Order Summary". Before this, Only thing which I knew was doing UPSERT with an External ID on an object. It is working Perfect !!! for me. I am just confirming from you guys (Guru's of Force.com platform) that whether it is ok to do it or I should query the records and perform INSERT / UPDATE based on condition. Please help me out in it.

 

Note: Following things I already know about the UPSERT.

1) UPSERT with External ID (I cannot create External ID on this object due of some reasons).

2) Query records and perform INSERT / UPDATE based on condition.

________________________________________________________________________________________

//Sample Order Summary Code for UPSERT in Apex

 

//List of Sales Order Summary for UPSERT
List<Sales_Order_Summary__c> listOfSalesOrderSummaryToUpsert = new List<Sales_Order_Summary__c>();

//Created a new Account "Test Sales Order Summary" with JDE Address Number = 1001

//JDE Address Number is an External ID on an Account. I am getting particular record.
Account a = new Account (JDE_AddressNumber__c = 1001);

//Create Sales Order Summary records
for(integer i=0; i<=13; i++){
    Sales_Order_Summary__c salesOrder = new Sales_Order_Summary__c();
    salesOrder.Account__r = a;
    salesOrder.Name = 'Sales Order Summary'+'---'+i;
    salesOrder.Amount__c = 300 + i;
    salesOrder.Date__c = System.today() + i;
    listOfSalesOrderSummaryToUpsert.add(salesOrder);
}

//The External_ID_Field is of type Schema.SObjectField, that is, a field token.
Schema.SObjectField schemaField = Sales_Order_Summary__c.Fields.Name;

 

//Creates new sObject records or updates existing sObject records within a single statement, using an optional custom field to determine the presence of existing objects.
Database.UpsertResult[] upsertResult = Database.upsert(listOfSalesOrderSummaryToUpsert, schemaField, false);
System.Debug('>>> Sales Order Summary UPSERT Result <<<'+upsertResult);

 
/*
Note: You cannot use custom fields as a parameter of Schema field.
Schema.SObjectField schemaField = Sales_Order_Summary__c.Fields.Date_c;
Note: If you use then you will get an error
System.SObjectException: Invalid field for upsert, must be an External Id custom or standard indexed field: Date__c
*/

Hello:

 

I have two unrelated custo objects that share a common key (Service_Order__c.Release__c = Resweep_Assignment__c.Name).

 

Both custom objects have date fields: Service_Order__c.Order_App_Date__c and Resweep_Assignment__c.Date_Assigned__c

 

What I am looking to do is to update a number field (Orders__c) with a count of Service_Order__c records where Service_Order__c.Order_App_Date__c is within 30 days of Resweep_Assignment__c.Date_Assigned__c.

 

Here is what I have, which is not saving. Any suggestions are greatly appreciated.

 

trigger ServiceOrderCount on Service_Order__c (after insert) {

     Set<String> serviceOrder = new Set<String>(); 
     for (Service_Order__c sord : Trigger.new) {
        serviceOrder.add(sord.Release__c);

     Map<String, Service_Order__c> sordMap = new Map<String,Service_Order__c>();
     for(Service_Order__c sord1 : [Select Name, Order_App_Date__c, Release__c from Service_Order__c where Name in : serviceOrder and Video__c=1]);
     sordMap.put(sord1.Release__c, sord1);

     Map<String, Resweep_Assignment__c> resweepMap = new Map<String, Resweep_Assignment__c>();
     for(Resweep_Assignment__c resweep1: [Select Name, Date_Assigned__c, Orders__c from Resweep_Assignment__c where Name in : serviceOrder]);
     resweepMap.put(resweep1.Name, resweep1);

     Set<String> resweepDate = new Set<String>();
     resweepDate.add(resweep1.Date_Assigned);

     integer i = [Select Count() from Service_Order__c where Name =: serviceOrder and (resweepDate < Order_App_Date__c < resweepDate+30)];

     List<Resweep_Assignment__c> resweepToUpdate = new List<Resweep_Assignment__c>();
          for(Resweep_Assignment__c a : [Select Name, Date_Assigned__c, Orders__c where Name in : serviceOrder]){
               if(serviceOrderMap.containskey(a.Name){
                    a.Orders__c = i;
               resweepToUpdate.add(a);
   }
 }
update resweepToUpdate
}

 Thanks,

 

Hampton

 

Hello!
I want to send a Static resource image in Email through a controller. the code which i have written for it is given below

Public void sendMail(){
     
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {info.Location__r.Account__r.Email__c}; 
         mail.setToAddresses(toAddresses);
         mail.setSenderDisplayName('Support');
         mail.setSubject('Invoice');
         mail.setBccSender(false); 
         mail.setUseSignature(false);
         mail.setHtmlBody('<image src="' + StaticResourceURL.GetResourceURL('cpLogo')+ '"/>'); 
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); }

 and the StaticResourcUrl is a class which i have search on net for getting the url of a static resource

public class StaticResourceURL
{
    //Pass the resource name
    public static String GetResourceURL(String resourceName)
    {
        //Fetching the resource
        List<StaticResource> resourceList = [SELECT Name, NamespacePrefix, SystemModStamp FROM StaticResource WHERE Name = :resourceName];
                            
        //Checking if the result is returned or not
        if(resourceList.size() == 1)
        {
           //Getting namespace
           String namespace = resourceList[0].NamespacePrefix;
           //Resource URL
           
           return '/resource/' + resourceList[0].SystemModStamp.getTime() + '/' + (namespace != null && namespace != '' ? namespace + '__' : '') + resourceName; 
        }
        else return '';
    }
    
    public static testMethod void testStaticResourceURL(){
    	StaticResourceURL.GetResourceURL('cpLogo');
    	System.debug(StaticResourceURL.GetResourceURL('cpLogo'));
    }
    
}

 

but this code is not working for me although system.debug in the class returns /resource/1368709494000/cpLogo ... kindly guide me what is wrong with it

I have written the below for a trigger that I'm working on and both seem to produce the desired result.  Based on what I've learned they are two best pracitce ways to use SOQL queries.  But can someone please help me understand why I wouldn't want to just use the version with the SOQL For Loop almost all the time? (since it can help can avoid governor limits)

 

Version 1: SOQL List

 

List<OpportunityLineItem> OppProducts = new List<OpportunityLineItem>([SELECT Id, OpportunityId, PricebookEntryId, Post_Sale_Project__c FROM OpportunityLineItem WHERE OpportunityId IN: OppIds]);
		system.debug('OppProducts list size : ' + String.valueof(OppProducts.size()));
		
		for(OpportunityLineItem oli: OppProducts){
			if(psp.Opportunity__c == oli.OpportunityId){
				ResetOppProducts.put(oli.Id, new OpportunityLineItem(Id=oli.Id, OpportunityId = psp.Opportunity__c, Post_Sale_Project__c = null));
				UpdateOppProducts.put(oli.Id, new OpportunityLineItem(Id=oli.Id, OpportunityId = psp.Opportunity__c, Post_Sale_Project__c = psp.Id));
				system.debug('Post Sale Project Opportunity__c : ' + psp.Opportunity__c);
				system.debug('OpportunityLineItem OpportunityId : ' + oli.OpportunityId);
				system.debug('OpportunityLineItem Id : ' + oli.Id);
				system.debug('OpportunityLineItem ProductId : ' + oli.PricebookEntryId);
				system.debug('OpportunityLineItem Post Sales Project : ' + oli.Post_Sale_Project__c);
			}
		}

 

Version 2: SOQL List in For Loop

 

for(List<OpportunityLineItem> OppProducts: [SELECT Id, OpportunityId, PricebookEntryId, Post_Sale_Project__c FROM OpportunityLineItem WHERE OpportunityId IN: OppIds]){
			for(OpportunityLineItem oli: OppProducts){
				if(psp.Opportunity__c == oli.OpportunityId){
					ResetOppProducts.put(oli.Id, new OpportunityLineItem(Id=oli.Id, OpportunityId = psp.Opportunity__c, Post_Sale_Project__c = null));
					UpdateOppProducts.put(oli.Id, new OpportunityLineItem(Id=oli.Id, OpportunityId = psp.Opportunity__c, Post_Sale_Project__c = psp.Id));
				}
			}
		}

 

Thanks in advance for your help!

 

Hi guys

 

Reading the Apex Develpers Guide.  It says "A list can contain up to four levels of nested collections inside it." (p43 in the pdf file for Winter 13 version).   Since I wanted to be 100% sure, I went to the Developer Console and ran this code:

 

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

 

To me, that's 4 levels.  And as expected, the code compiled.

 

I added one more level, so my code became this, and I expected it to crash.

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

 

No, it didn't crash.

 

I finally added one more level:

List<Integer> a = new List<Integer>();
a.add(34);

List<List<Integer>> b = new List<List<Integer>>();
b.add(a);

List<List<List<Integer>>> c = new List<List<List<Integer>>>();
c.add(b);

List<List<List<List<Integer>>>> d = new List<List<List<List<Integer>>>>();
d.add(c);

List<List<List<List<List<Integer>>>>> e = new List<List<List<List<List<Integer>>>>>();
e.add(d);

List<List<List<List<List<List<Integer>>>>>> f = new List<List<List<List<List<List<Integer>>>>>>();
f.add(d);

 

And I finally got an error that said:

line -1, column -1: Nested type exceeds maximum level: 5

 

That is not quite what the book said.

 

I then googled a bit and realized according to Database.com page (here at http://docs.database.com/dbcom/en-us/db_apex/langCon_apex_collections_lists.htm?version=178.15), the list can contain up to 5 levels of nested collections.  There seems to be some inconsistency.  

 

Can anyone verify?  Has something been updated to Apex lately?

 

Thanks a lot.

King

 

 

 

 

 

 

 

 

According to (www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm, see Rules and Guidelines section), the upsert operation supports use of External IDs in reference fields. That is,

I'm inserting an Opportunity and I need to specify the related Account. However, I have only the External Id value of Account. Usually, I would query the Accout's Id, the use it to insert a new Opportunity but the Salesforce API lets you skip this step - (see http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_upsert.htm)

However, I'm curious to know how this can be done via Apex code - the URL above talks about the Web Services API which is in Java or other environment code.

Has anyone been able to do that?

 

  • May 10, 2012
  • Like
  • 0

According to the documentation when you call getOrgDefaults on a Custom Setting it should return null if there are no Org Defaults defined.  Since the Spring 12 release this method is not returning null but instead is returning a blank object.  Try the following code from the Developer Console on a custom setting with no values defined.  This causes problems if you have code that has defaults defined in the case of the Custom Setting not being configured yet (i.e. getOrgDefaults returning NULL).

 

System.debug(YourCustomSetting__c.getOrgDefaults());

 

The debug output will look like below instead of saying Null

 

10:05:08:098 USER_DEBUG [1]|DEBUG|YourCustomSetting__c:{}

 

Has anyone else noticed this?  I am going to log a case with Developer Support as well.

 

 

  • February 14, 2012
  • Like
  • 0

hi

 

Why can't we use SoSL in Triggers.

 Thanks In advance

  • January 11, 2011
  • Like
  • 0