• Srikanth Dorepalli
  • NEWBIE
  • 34 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
LOOK AT THIS ONE:-
Account acct = new Account(Name='SFDC Account');
insert acct;

// Once the account is inserted, the sObject will be 
// populated with an ID.
// Get this ID.
ID acctID = acct.ID;

// Add a contact to this account.
Contact mario = new Contact(
    FirstName='Mario',
    LastName='Ruiz',
    Phone='415.555.1212',
    AccountId=acctID);
insert mario;

MY QUESTION:-
----------------------------
If i want to append the ID like above snippet for custom objects how can i achieve this
 
APEX CLASS:

public class fetch1 {
    public list<account> acc{set;get;}
    public map<string,list<contact>> mp{set;get;}
    public fetch1(){
        acc=[select name,(select lastname,firstname from contacts) from account ];
        list<contact> con=new list<contact>();
        mp=new map<string,list<contact>>();
        for(account a:acc){
            for(contact c:a.contacts){
                con.add(c);
                 }
            mp.put(a.name,con);
        }
        }
        }

VF:

<apex:page controller="fetch1" >
    <apex:form >
    <apex:pageBlock >
       <apex:pageBlockTable value="{!acc}" var="a">
        <apex:column headerValue="ACCOUNTS" value="{!a.name}"/>
           <apex:column headerValue="RELATED CONTACTS" >
               <apex:pageBlockTable value="{!mp}" var="cont">
               <apex:column value="{!mp[cont].firstname}"/>
                   <apex:column value="{!mp[cont].lastname}"/>
               </apex:pageBlockTable>
           </apex:column>
        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

ERROR:
There are no errors in code.But when i am going for a preview of vf page i am getting this error "Incorrect parameter type for subscript. Expected Number, received Text "

 
Getting an error The matching field you chose (Name) is not mapped and is required for an Update and Insert operation.
Any help how to fix this error Thankful  for any guidance
APEX CLASS:

public class fetch1 {
    public list<account> acc{set;get;}
    public map<string,list<contact>> mp{set;get;}
    public fetch1(){
        acc=[select name,(select lastname,firstname from contacts) from account ];
        list<contact> con=new list<contact>();
        mp=new map<string,list<contact>>();
        for(account a:acc){
            for(contact c:a.contacts){
                con.add(c);
                 }
            mp.put(a.name,con);
        }
        }
        }

VF:

<apex:page controller="fetch1" >
    <apex:form >
    <apex:pageBlock >
       <apex:pageBlockTable value="{!acc}" var="a">
        <apex:column headerValue="ACCOUNTS" value="{!a.name}"/>
           <apex:column headerValue="RELATED CONTACTS" >
               <apex:pageBlockTable value="{!mp}" var="cont">
               <apex:column value="{!mp[cont].firstname}"/>
                   <apex:column value="{!mp[cont].lastname}"/>
               </apex:pageBlockTable>
           </apex:column>
        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

ERROR:
There are no errors in code.But when i am going for a preview of vf page i am getting this error "Incorrect parameter type for subscript. Expected Number, received Text "