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
AnnaTAnnaT 

Update another object when updating multiple data with ApexDataloader

When I update one record, this trigger works, but when I update multiple records by using Apex Dataloader, it doesn't work.

This trigger updates parent object when its child object's status is updated.

 

1. Child object Trigger: it just updates parent object.

trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update) {
    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    list<Ichthys_RFT__c>msl=[SELECT id,Status__c,RFT_Category__r.Id FROM Ichthys_RFT__c WHERE Id in :Trigger.new];
    Ichthys_RFT__c newMs=null;
    if(msl.size()>0)
    {
      newMs=msl[0]; 
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id = :newMs.RFT_Category__r.Id];
      if(pdList.size()>0)
      {
        update pdList[0];
      }
    }
  }
}

2. Parent Object Trigger: it checks child object status and update parent status.

trigger RFTSetPDStatus on RFT_Category__c (before update) {
  if (Trigger.isUpdate)
  {
    List<Ichthys_RFT__c > msList= [SELECT RFT_Category__r.Id,Status__c FROM Ichthys_RFT__c WHERE RFT_Category__r.Id in :Trigger.new];
    Integer msExist=0;
    Integer msCnt=0;  

    if(msList.size()>0){
      msExist +=1;
    }
    for(RFT_Category__c pd:Trigger.new){
      if(msExist==0){
        pd.PD_Status__c ='Open';
      }else{
        for( Ichthys_RFT__c ms :msList){
          if( ms.RFT_Category__r.Id == pd.Id && ms.Status__c <>'Completed' &&  ms.Status__c <>'Not Applicable'){
            msCnt +=1;
          }
        }
        if(msCnt==0){
          pd.PD_Status__c ='Close';
        }
        else if(msCnt>0){
          pd.PD_Status__c ='Open';
        }
      }
    }
  }
}

 Thanks in advance for your help.

 

Anna

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update, after delete) {
    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    List<Id> rftIds = new List<Id>();

    for(Ichthys_RFT__c rft : trigger.new)
    {
      rftIds.add(rft.RFT_Category_ID__c);
    }

    if(rftIds.size() > 0)
    {
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id IN :rftIds];
      if(pdList.size()>0)
      {
        update pdList; // instead of pdList[0].
      }
    }
  }
}

 

All Answers

raseshtcsraseshtcs
Bulkify the code. You can find more information for the same in the apex guide.
Naidu PothiniNaidu Pothini
trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update) {
    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    list<Ichthys_RFT__c>msl=[SELECT id,Status__c,RFT_Category__r.Id FROM Ichthys_RFT__c WHERE Id in :Trigger.new];
    Ichthys_RFT__c newMs=null;
    if(msl.size()>0)
    {
      newMs=msl[0]; 
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id = :newMs.RFT_Category__r.Id];
      if(pdList.size()>0)
      {
        update pdList[0];
      }
    }
  }
}

 replace the above code with

 

 

trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update)
{    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    List<Id> rftIds = new List<Id>();

    for(Ichthys_RFT__c rft : trigger.new)
    {
      rftIds.add(rft.RFT_Category__r.Id);
    }

    if(rftIds.size() > 0)
    {
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id IN :rftIds];

      if(pdList.size()>0)
      {
        update pdList[0];
      }
    }
  }
}

 

AnnaTAnnaT

Thank you very much for your help!!

I modified my code, but it seems that pdList can't get any data.
Always pdList.size() is zero.

AnnaTAnnaT
I found that "rft.RFT_Category__r.Id" is empty.
Is something wrong with this ??
Naidu PothiniNaidu Pothini
trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update)
{    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
System.debug('------------------------------------------'+trigger.newMap);
List<Id> rftIds = new List<Id>(); for(Ichthys_RFT__c rft : trigger.new) { rftIds.add(rft.RFT_Category__r.Id); } if(rftIds.size() > 0) { RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id IN :rftIds]; if(pdList.size()>0) { update pdList; // IS there any specific reason for just updating the records?? i guess there should be some field update or something right... } } } }

 RFT_Category__r.Id should not be null... 

 

can you provide the debug log for this?

AnnaTAnnaT

Thank you for your help...

 

There is no debug log created.

 

This trigger update its parent object, and the parent object also has a trigger to update its status.

 

 

I prepared new custom field "RFT_Category_ID__c" which gets ID from another lookup field.

And I modified the code as following, but when I update multiple data, it doesn't work correctly.

 

trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update, after delete) {
    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    List<Id> rftIds = new List<Id>();

    for(Ichthys_RFT__c rft : trigger.new)
    {
      rftIds.add(rft.RFT_Category_ID__c);
    }

    if(rftIds.size() > 0)
    {
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id IN :rftIds];
      if(pdList.size()>0)
      {
        update pdList[0];
      }
    }
  }
}

 

Naidu PothiniNaidu Pothini
trigger RFTSetPDStatusFromMS on Ichthys_RFT__c(after insert, after update, after delete) {
    
  if (Trigger.isInsert||Trigger.isUpdate)
  {
    List<Id> rftIds = new List<Id>();

    for(Ichthys_RFT__c rft : trigger.new)
    {
      rftIds.add(rft.RFT_Category_ID__c);
    }

    if(rftIds.size() > 0)
    {
      RFT_Category__c[] pdList = [SELECT id, Name, PD_Status__c FROM RFT_Category__c WHERE Id IN :rftIds];
      if(pdList.size()>0)
      {
        update pdList; // instead of pdList[0].
      }
    }
  }
}

 

This was selected as the best answer
AnnaTAnnaT

Finally it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Thank you so much for your very very kind support !!!!!!!!!!!

I can't thank you enough...

 

Anna