+ 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);
}