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
Arun Deepan LJArun Deepan LJ 

IDs not available in Before insert. How to uniquely identify the records

Hi, 
In the before insert context, the IDs are not yet assigned to the list. In such scenario, If I need to create a map to uniquely access each record. What is the best way. Cannot depend on the field values.
Best Answer chosen by Arun Deepan LJ
sharathchandra thukkanisharathchandra thukkani
trigger youtriggerName on yourObject__c(before insert) {
    Integer size = trigger.new.size();
    Map<Integer,yourObject__c> myMap = new Map<Integer,yourObject__c>();
    for(yourObject__c c : trigger.new){
        for(Integer i=0;i<size; i++){
            myMap.put(i,c);
        }
    }
    System.debug(myMap);
}

All Answers

sharathchandra thukkanisharathchandra thukkani
trigger youtriggerName on yourObject__c(before insert) {
    Integer size = trigger.new.size();
    Map<Integer,yourObject__c> myMap = new Map<Integer,yourObject__c>();
    for(yourObject__c c : trigger.new){
        for(Integer i=0;i<size; i++){
            myMap.put(i,c);
        }
    }
    System.debug(myMap);
}
This was selected as the best answer
Arun Deepan LJArun Deepan LJ
I Don't think, i need inner for loop fot this
trigger youtriggerName on yourObject__c(before insert) {
    Integer i= 0;
    Map<Integer,yourObject__c> myMap = new Map<Integer,yourObject__c>();
    for(yourObject__c c : trigger.new){
         
        myMap.put(i,c);
        i++;
    }
    System.debug(myMap);
}