Skip to main content
palukuru sfdc 님이 #Apex에 질문했습니다
Hi Can someone explain how to set the lookup (from account name value) to conatct name when creating contact from anonymous window.

List<contact> contactToSave = new List<contact> ();

Contact cont = new Contact();

Account acctRef = new Account(name='University of Arizona'); // need to set this name to contact which I'm creating below. Account name already available on org 

cont.FirstName='Test1';

cont.LastName='Test2';

cont.Department='Finance';

cont.name=actRef;

contactToSave.add(cont);

insert contactToSave;

please explain how this works...

답변 3개
  1. 2017년 1월 8일 오전 4:26
    Hi Palukuru,

    I think you are trying to create contacts for some existing accounts. You can do that by -

     

    //Fetch the existing Account

    List<Account> accounts = [SELECT ID FROM Account Where <PUT YOUR UNIQUE IDENTIFIER TO FETCH THE EXISTING ACCOUNT>];

    List<Contact> allContacts = new List<Contact>();

    if(accounts.size()>0){

        Contact singleContact = new Contact();

        singleContact.FirstName = 'John';

        singleContact.LastName = 'Miller';

        singleContact.AccountID = accounts.get(0).Id;

        allContacts.add(singleContact);

    }

    if(allContacts.size() > 0){

        insert allContacts;

    }

    Please accept my solution as Best Answer if my reply is helpful.
0/9000