function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shaanRocksshaanRocks 

Making use of Maps

I have an issue of GLimit when using the list and set.

I have written a trigger on a custom object which is in relation with contacts.

The trigger performs action based on updation or insertion of records.

Here is my code:

 

Set<String> cntctIds = new Set<String>();

List<obj__c> obj =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where id In:trigger.newMap.keyset()];

for(Integer j=0;j< obj.size();j++)

{
        cntctIds.add(obj[j].Contact__c);
}

 

List<obj__c> obj_Total =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where id IN: cntctIds];

 

//List used to retrieve contacts based on cntctIds

List<Contact> objCntct = [Select id,Account.Name,lookupOwnerID__c, SSN__c, PCN__c, Status__c, FirstName, LastName,AccountId, OwnerId,  from Contact where id IN: cntctIds];        

 

//List used to retrieve Obj__c based on cntctIds and Terminated Status.

List<obj__c> obj_Terminated =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where obj__c.Status__c = 'Terminated' and id  IN: cnctctIds];

 

//List used to retrieve Obj__c based on cntctIds and Current Status.

List<obj__c> obj_Current =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where obj__c.Status__c = 'Current' and id  IN: cnctctIds];

 

now i need to check the conditions if all the records are terminated or current.

For that i am using the condition:

for(j=0; j<objCntct ; j++)

{

if(obj_Total.size() == obj_Terminated.size())

{

//functionality

}

else if(obj_Total.size() == obj_Current.size())

{

 

}

 

}

 

This is working perfectly fine if the batch size is 1.

Becuase in that scenario there will be only one contact but if the batch size is 200 then there will be many contacts for 200 obj__c records.And then the condition fails bacause the the obj_Total count and the terminated or current count never matches.

 

Can anyone provide a soln for this.

Or any one has the idea how to make use of Maps here to check the conditions.

 

 

 

Thanks

shaan

 

 

 

sfdcfoxsfdcfox

 

	// A mapping of contact id to a map of a string to a list of custom objects.
	Map<Id,Map<String,List<Obj__c>>> objects = new Map<Id,Map<String,List<Obj__c>>>();

	// Initialize the vectors for this map.
	for(Obj__c obj:Trigger.new) {
		objects.put(obj.contact__c,new map<string,list<obj__c>>());
		objects.get(obj.contact__c).put('Total',new list<obj__c>());
		objects.get(obj.contact__c).put('Current',new list<obj__c>());
		objects.get(obj.contact__c).put('Terminated',new list<obj__c>());
	}
	
	// Find all objects for all contacts that were listed; categorize by current or terminated.
	for(Obj__c obj:[select id,ssn__c,name,status__c,contact__c,ownerid,account_name__c from obj__c where contact__c in :objects.keyset()]) {
		objects.get(obj.contact__c).get('total').add(obj);
		if(obj.status__c == 'Terminated' || obj.status__c == 'Current') {
			objects.get(obj.contact__c).get(obj.status__c).add(obj);
		}
	}

	// Here's the contact information; you can look up contact details in this map: contacts.get(contactId)...
	Map<Id,Contact> contacts = new map<id,contact>([Select id,Account.Name,lookupOwnerID__c, SSN__c, PCN__c, Status__c, FirstName, LastName,AccountId, OwnerId where ID in :objects.keyset()]);
	
	// Now you can check and see if all records are current or terminated.
	for(id contactId:objects.keySet()) {
		if(objects.get(contactId).get('Total').size()==objects.get(contactId).get('Terminated').size()) {
			// All records are in terminated status.
		}
		if(objects.get(contactId).get('Total').size()==objects.get(contactId).get('Current').size()) {
			// All records are in current status.
		}
	}

 

We use only two queries, and build mapped data to easily keep track of this complex data structure. A well-organized structure will almost certainly reduce coding requirements and probably heap memory usage as well. If you need help understanding what's going on, please let me know.

 

shaan85shaan85

Hey many thanks for your response.

 

I tried using your Logic its perfect.

But i have an issue , Within the object i have a status field  in obj__c and the status field can have values as Terminated or Current.

There is no Status with Total value.

So it is throwing an error at:

objects.get(obj.contact__c).put('Total',new list<obj__c>());

While Total here is the combination of Current and Terminated records.

If i can get the Total Records in a map (i.e Combination of Current and Terminated records).

Then i can complete my task.

 

Please help me out if any solution.

 

Regards

shaan

 

 

 

sfdcfoxsfdcfox

What error are you getting? The words 'Total', 'Current', and 'Terminated' are merely keys for an associative array, and so should not present a problem, even though 'Total' is not a field or a status on the Obj__c entity.

shaan85shaan85

Sorry for the earlier post there was no error actually ... there was a mistake  from my side.  I used the value of key in the map as 'total' instead of 'Total' ( case sensistive)  :smileyhappy:.

Any ways thanks for the reply. this is working perfectly now. I am trying to bulid my code as there are many conditions involved here.

 

Will let you know when its perfectly done.

 

Thanks a lot.

 

Regards,

Shaan

sfdcfoxsfdcfox

Yes, my example code did the same thing, so that was at least partially my fault. But it's useful to know that Maps are always case sensitive, likely because they use a hash-key internally, such as many other languages use to create efficient string key indexing.