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
vamsi krishna 106vamsi krishna 106 

While inserting List of records , is it possible to get the all the id of that records?

i have list ..after inserting that list i want get that all id's of that list records..is it possible ..please give some suggesstions...

thanks in advance

Regard's

Vamsi Krishna..
Ramon PereiraRamon Pereira
Yes,
 
List < Account > accs = new List < Account > ();
for (Integer i = 0; i < 20; i++) {
    Account ac = new Account(Name = 'Accme Corp-' + i);
    accs.add(ac);
}

insert acc;

Map < Id, Account > mapAccount = new Map < Id, Account > ();
mapAccount.putAll(accs);
system.debug('The ids: ' + mapAccount.keySet());
If the answer has been the solution, please mark it as resolved.
Thanks.

 
jyothsna reddy 5jyothsna reddy 5
Hi Vamsi Kirshna,

Yes
Please try the below sample code

Visual Force page:
<apex:page Controller="Task17Apex">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageblockSection >
            <apex:pageblockSectionItem >
                <apex:outputLabel >First Name</apex:outputLabel>
                <apex:inputField value="{!L.FirstName}" />
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:outputLabel >Last Name</apex:outputLabel>
                <apex:inputField value="{!L.LastName}" />
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:outputLabel >Company</apex:outputLabel>
                <apex:inputField value="{!L.Company}" />
            </apex:pageblockSectionItem>  
        </apex:pageblockSection>   
    </apex:pageBlock>
    <apex:pageblock >
         <apex:pageblockTable var="l" value="{!Ld}" rendered="{!pb}" >
            <apex:column headerValue="First Name">
                <apex:outputField value="{!l.FirstName}"/>
            </apex:column>  
            <apex:column headerValue="Last Name">
                <apex:outputField value="{!l.LastName}"/>
            </apex:column> 
             <apex:column headerValue="Company">
                <apex:outputField value="{!l.Company}"/>
            </apex:column>          
        </apex:pageblockTable>     
    </apex:pageblock>
</apex:form>
</apex:page>

Cotroller:
public class Task17Apex {

    public Boolean pb { get; set; }

    public List<Lead> Ld {get; set;}
    
    public Lead L {get; set;}
    public Task17Apex (){
        L= new Lead();
        Ld= new List<Lead> ();
        pb=false;
    }
    
    public PageReference saveLead() {
        Ld.add(L);
        insert Ld;
        pb=true;
        Ld=[select id, FirstName,LastName,Company from Lead];
      // PageReference  pr = new PageReference ('page.Task17SC1');
        return page.Task17SC1;   // for redirecting to the next vf Page, if to display in the same page return Null
    }


   }

I hope It will help you .

Regards,
Jyothsna  D