+ Start a Discussion
AnnaTAnnaT 

Can't get Map data

When I update data, the following trigger can't get "Item_No_seq__c" value.

Please tell me what is a problem...

 

trigger IchthysRFT on RFT_Category__c (before insert, before update) {
  Map<String,Map<Double,RFT_Category__c>> rft =
    new Map<String,Map<Double,RFT_Category__c>>();
  
  Set<String> objNos = new Set<String>();
  Set<Double> itemSeq = new Set<Double>();
  for(RFT_Category__c record:trigger.new) {
    objNos.add(record.Object_No__c);
    itemSeq.add(record.Item_No_seq__c);
  }
  for(RFT_Category__c record:[SELECT Id, Item_No_seq__c, Object_No__c
                                FROM RFT_Category__c
                               WHERE Object_No__c IN :objNos AND Item_No_seq__c!=null 
                               ORDER BY Item_No_seq__c DESC limit 1]) {
    if(!rft.containsKey(record.Object_No__c)) {
      rft.put(record.Object_No__c,new Map<Double,RFT_Category__c>());
    }
    rft.get(record.Object_No__c).put(record.Item_No_seq__c,record);
  }
  for(RFT_Category__c record:Trigger.new) {
    if(rft.containsKey(record.Object_No__c)&& rft.get(record.Object_No__c).containsKey(record.Item_No_seq__c)) {
      RFT_Category__c tmp = rft.get(record.Object_No__c).get(record.Item_No_seq__c);
      if(Trigger.isUpdate){
        RFT_Category__c oldpd=Trigger.oldMap.get(record.id);
        if(record.Object_No__c!= oldpd.Object_No__c){
          record.Item_No_seq__c = tmp.Item_No_seq__c +1;
        }
      }
    }else {
      record.Item_No_seq__c = 1;
    }
  }
}

 

Thank you,

Anna

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi,

 

I see the reason for the same is your query :-

 

SELECT Id, Item_No_seq__c, Object_No__c
                                FROM RFT_Category__c
                               WHERE Object_No__c IN :objNos AND Item_No_seq__c!=null
                               ORDER BY Item_No_seq__c DESC limit 1

 

As you are doing limit 1,I won't expecty it to update for bulk records.

All Answers

Vinit_KumarVinit_Kumar

Can you be more clear,which line is not getting the value.

AnnaTAnnaT

Dear Vinit_Kumar

 

Thank you for your reply.

My trigger jumps the following code.

Object_No__c is not null but Item_No_seq__c is always null.

 

if(rft.containsKey(record.Object_No__c)&& rft.get(record.Object_No__c).containsKey(record.Item_No_seq__c))

 So I modified the last sentence in the following code.

  for(RFT_Category__c record:[SELECT Id, Item_No_seq__c, Object_No__c
                                FROM RFT_Category__c
                               WHERE Object_No__c IN :objNos AND Item_No_seq__c!=null 
                               ORDER BY Item_No_seq__c DESC limit 1]) {
    if(!rft.containsKey(record.Object_No__c)) {
      rft.put(record.Object_No__c,new Map<Double,RFT_Category__c>());
      rft.get(record.Object_No__c).put(record.Item_No_seq__c,record);

 Then I could update single data, but when in bulk mode, this trigger doesn't work properly. 

 

Thank you,

Anna Tanaka

 

 

 

 

 

 

Vinit_KumarVinit_Kumar

Hi,

 

I see the reason for the same is your query :-

 

SELECT Id, Item_No_seq__c, Object_No__c
                                FROM RFT_Category__c
                               WHERE Object_No__c IN :objNos AND Item_No_seq__c!=null
                               ORDER BY Item_No_seq__c DESC limit 1

 

As you are doing limit 1,I won't expecty it to update for bulk records.

This was selected as the best answer
AnnaTAnnaT
Hi Vinit_Kumar,
Thank you for your support!
As I'd like to get max Item_No_seq__c value, I used "limit 1".
Is it impossible to get max value in bulk mode?
Vinit_KumarVinit_Kumar

Not sure what you mean.