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
kkr.devkkr.dev 

Design Help

Hi All,

 

     I need to sort the records after insertion of all records through data loader.If i go with trigger i need to sort records after each insert.We insert the records only through data loader once in a day not through UI.So what do you think the best approach to do it?Please post some sample code.

 

Thanks

 

 

STest123STest123

I know very well about your questions but can you tell me that when we will insert the records through data loader into any objects then on which field based you will sort to all records into your org's? Please tell me?

kkr.devkkr.dev

I need to sort the records with same name and then sort by some number field and then flag the record with highest number  and then get the records which got flagged and then update with some value.

 

Thanks

Damien_Damien_

I'm not completely sure I understand your requirements... but I'm pretty sure you can do what you want to with aggregate queries:

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_agg_functions.htm

kkr.devkkr.dev

First I need to insert all records to custom object through data loader.These records contain some user info as well as other info. I need to sort these records with same name and then sort by some number field...lets say there are 2 records with same name but different number field value...take the record(resultset) with highest number field value...then update user record with the info from the resultset..this has to be done for all records..let me know if you are not clear with my requirement.

Damien_Damien_

Sounds to me like you could put them in a map to do it without any of the ordering. (Unless there is something else you are doing with this order)

 

List<yourobject> obs = Trigger.new;
Map<String, yourobject> objMap = new Map<String, yourobject>();
for (yourobject obj: Trigger.new)
{
  yourobject mapObj = objMap.get(obj.Name);
  if (mapObj == null || obj.value > mapObj.value)
    objMap.put(obj.Name, obb);
}

for (yourobject mapObj = objMap.values())
{
  //Do stuff with your User here
}

 You will obviously need to do some modifications, but this should give you the core majority of what you're looking for.

 

sai.sfsai.sf

Hi Damien,

 

   I have similar scenerio.I have used  u r code.

 

trigger UpdateUserFields on ER__c (after insert) {
 
list<ER__c> obs = Trigger.new;
Map<String, ER__c> objMap = new Map<String, ER__c>();
for (ER__c obj: Trigger.new)
{
  system.debug('********obj *****'+obj);
  system.debug('******* obj.Name ******'+obj.Name);    
  ER__c mapObj = objMap.get(obj.Name);
  system.debug('****mapObj *** '+mapObj);
  if (mapObj == null || obj.App_Role_Numbered__c > mapObj.App_Role_Numbered__c)
    objMap.put(obj.Name, obj);
  }

for (ER__c mapObj : objMap.values())
{
  //Do stuff with your User here
  system.debug('inside for loop');
}
 
}

 

I am getting mapObj as null in this debug log ......system.debug('****mapObj *** '+mapObj);

 

Can you please help me

 

Thanks

Damien_Damien_

That's fine to get it null there.  It just hasn't been put into the map yet.  Thats what the check and lines after are for.

sai.sfsai.sf

Damien,

 

      Thanks for you reply.Why are you using this  'obs'  in the trigger .. list<ER__c> obs = Trigger.new;

sai.sfsai.sf

Damien,

 

  How will it sort the existing records

Damien_Damien_

Thanks for you reply.Why are you using this  'obs'  in the trigger .. list<ER__c> obs = Trigger.new;

Sorry, I just forgot to remove that.

 

It won't sort.  According to the way I read the problem, it appeared like there was no actual sort needed.

sai.sfsai.sf

Thanks Damien..How it gone sort the existing records ( not the inserting one's but also existing records).

Damien_Damien_

I'm not quite sure I understand your question.  You might need to rephrase this.

sai.sfsai.sf

I want to sort all records based on name and then based on some number field and get the highest one's..so in this requirement i may have some existing records in an object as well as the one's that  i am inserting through data loader..but it looks like this trigger works for only the inserted records not the records that already in salesforce.

Damien_Damien_

Yes, triggers only execute on the records that are being changed.  If you need to do it for other records, you will need to query for them.  You can simply add the ORDER BY keywords in order to order them in the way you want.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_orderby.htm

sai.sfsai.sf

trigger UpdateUserFields on ER__c (after insert) {
 
list<ER__c> obs = Trigger.new;
Map<String, ER__c> objMap = new Map<String, ER__c>();
for (ER__c obj: Trigger.new)
{
  system.debug('********obj *****'+obj);
  system.debug('******* obj.Name ******'+obj.Name);    
  ER__c mapObj = objMap.get(obj.Name);
  system.debug('****mapObj *** '+mapObj);
  if (mapObj == null || obj.Role_Numbered__c > mapObj.Role_Numbered__c)
    objMap.put(obj.Name, obj);
  }

 

list<ER__c>elist=[Select e.Name, e.Id, e.Role_Numbered__c From ER__c e order by e.Name,e.Role_Numbered__c];

 

for (ER__c mapObj : objMap.values())
{
  //Do stuff with your User here
  system.debug('inside for loop');
}
 
}

 

Thanks Damien, I have queried my object ....can you please help me in further.

 

Damien_Damien_

I think you want something different.  If all you want is to sort the fields that 1 line is all you need.  You don't need the rest.

sai.sfsai.sf

But how to map the records..can you please help me.

Damien_Damien_
for (ER__c obj: [SELECT Name, Role_Numbered__c FROM ER__c])
{
  system.debug('********obj *****'+obj);
  system.debug('******* obj.Name ******'+obj.Name);    
  ER__c mapObj = objMap.get(obj.Name);
  system.debug('****mapObj *** '+mapObj);
  if (mapObj == null || obj.Role_Numbered__c > mapObj.Role_Numbered__c)
    objMap.put(obj.Name, obj);
}

 

sai.sfsai.sf

Thanks Damien..I think this works fine..but for each insert it will sort all records....do you know any other alternative other than trigger..like schedule apex  as we insert recods  once in a day through Data loader not through UI.

Damien_Damien_

Yes, you could have it happen once a day in the middle of the night using batches.