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
Cris9931Cris9931 

how can i access the fields from an Sobject with map?

Hello,

I have a small trigger:
 

trigger SetTitleToAttachment on Attachment (after insert) { 

    
    //store the ID of the new attachment
    ID attachment;
    ID workOrder;

    
    
    for (Attachment att : Trigger.new) {
        attachment = att.ID;
        workOrder = att.ParentId;
       
        
    }
   Map<ID, SVMXC__Service_Order__c> wo =new Map<ID, SVMXC__Service_Order__c>( [SELECT ID, NAME, SVMX_PS_Ship_To_Name__c from SVMXC__Service_Order__c WHERE ID =: workOrder ]);


}


My question is: how can I access the fieds: Name and SVMX_PS_Ship_To_Name__c from this map?

I'm confused. 

Best Answer chosen by Cris9931
Agustin BAgustin B
Hi Sarah, after that query(line 17) you use
for(SVMXC__Service_Order__c w: wo.values()){
//here you have the fields w.Name and w.SVMX_PS_Ship_To_Name__c 
}
If it helps please mark as correct, it may help others with the same question.
 

All Answers

Agustin BAgustin B
Hi Sarah, after that query(line 17) you use
for(SVMXC__Service_Order__c w: wo.values()){
//here you have the fields w.Name and w.SVMX_PS_Ship_To_Name__c 
}
If it helps please mark as correct, it may help others with the same question.
 
This was selected as the best answer
Cris9931Cris9931

Hi Agustin,

But if I want to make a new variable called woName and I want to assign the value from the map should be something like this?:

trigger SetTitleToAttachment on Attachment (after insert) { 

    
    //store the ID of the new attachment
    ID attachment;
    ID workOrder;
    String woName;
    
    
    for (Attachment att : Trigger.new) {
        attachment = att.ID;
        workOrder = att.ParentId;
       
        
    }
   Map<ID, SVMXC__Service_Order__c> wo =new Map<ID, SVMXC__Service_Order__c>( [SELECT ID, NAME from SVMXC__Service_Order__c WHERE ID =: workOrder ]);

   for(SVMXC__Service_Order__c w: wo.values()){
         woName = w.Name;
    }
  
   
   System.debug('woName' + woName);

}
Agustin BAgustin B
HI, yes you can but if it is only one record you can just do this to be more efficient.
SVMXC__Service_Order__c> w =[SELECT ID, NAME from SVMXC__Service_Order__c WHERE ID =: workOrder LIMIT 1];

  woName = w.Name;
Hope it is what you need.
If it does please like an choose best answer to get the question closed.

Good luck
Cris9931Cris9931
uff.  I have an error:
Error: Apex trigger SetTitleToAttachment caused an unexpected exception, contact your administrator: SetTitleToAttachment: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SetTitleToAttachment: line 28, column 1
 
trigger SetTitleToAttachment on Attachment (after insert) { 

    
    //store the ID of the new attachment
    ID attachment;
    ID workOrder;
    String woName;
    Boolean ServiceReportGenerated;
    
    for (Attachment att : Trigger.new) {
        attachment = att.ID;
        workOrder = att.ParentId;
       
        
    }
   Map<ID, SVMXC__Service_Order__c> wo =new Map<ID, SVMXC__Service_Order__c>( [SELECT ID, NAME,Service_Report_Generated__c from SVMXC__Service_Order__c WHERE ID =: workOrder ]);
    System.debug('wo ' + wo );
   for(SVMXC__Service_Order__c w: wo.values()){
         woName = w.Name;
         ServiceReportGenerated = w.Service_Report_Generated__c;
    }
  
   
   System.debug('woName' + woName);
   
   
   for(Attachment att: Trigger.new){       
             att.Name = woName+'.pdf';
   }

Is there any workaround on this? :(
Cris9931Cris9931

I had to put before insert. :) 

Done :)
 

Thanks a lot!

Agustin BAgustin B
Hi Sarah thats because you are on after insert, the record has been already inserted.
You could try something like this:
List<Attachment> toBeUpdatedAttachment = new List<Attachment>(); 
for(Attachment att: Trigger.new){
att.Name = woName + '.pdf';
toBeUpdatedAttachment.add(att);
     }
      update toBeUpdatedAttachment;
Also consider this trigger you wrote is not prepare for inserting several Attachments with different parentId at the same time you would have to write something a little more complex for that.

If your question is solved please mark as best, if you need to make it bulk you could open a new question and I will try to provide you with the code there.

Good luck
 
Cris9931Cris9931

Hi Agustin,

I have this error.

User-added image


this field CheckerSFM exists in my object...