1

I have a child object of Parent Account name Vendor .

I want to create multiple vendor on one account record.

I have created master detail relationship between these two.

When i want to save(insert) the account i am getting the following error. at the line c.id=idey;

System.NullPointerException: Attempt to de-reference a null object

Code snippet in which error is as follows

public PageReference saveAccount(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Vendor__c> contactList=new List<Vendor__c>();
        for(WrapperClass wc:lContacts){
            Vendor__c c=new Vendor__c();
        c.Name__c=wc.c.Name__c;
        c.Email__c=wc.c.Email__c;
        //c.Account__r.accountId =idey;
       c.id=idey;
        contactList.add(c);
        }
        insert contactList;
        PageReference p = new PageReference('/001/o');


        return p;
    }

Can you help me out thanks in advance.

  • 1
    Change c.id=idey to c.Account__c=idey and try – Vigneshwaran G Jul 9 '15 at 11:07
  • I posted the comment as Answer. If the solution worked for you please accept and upvote it. Thanks – Vigneshwaran G Jul 9 '15 at 12:34
1

Change c.id=idey to c.Account__c=idey

Reason

Assigning id of account idey to id of vendor will throw error.

Hope it helps.

2

You cannot specify ID in insert call. Since you are looking to use Parent's id. It should be something like below -

public PageReference saveAccount(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Vendor__c> contactList=new List<Vendor__c>();
        for(WrapperClass wc:lContacts){
            Vendor__c c=new Vendor__c();
        c.Name__c=wc.c.Name__c;
        c.Email__c=wc.c.Email__c;
        c.Account__c=idey;
        contactList.add(c);
        }
        insert contactList;
        PageReference p = new PageReference('/001/o');


        return p;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.