0

I want to create a chain record like contact is related list of account and opportunity is related list of that contact which is connect to account(same account). i am able to insert them individually but not in a chain. i tried this so far -

    public with sharing class CreateOppContact {
  public static String campaignId = '7017F000000M5U3';
    public CreateOppContact() {

    }
  public static void createOpp(){
      List<Account> listOfAccount = new List<Account>();
      List<Contact> listOfContact = new List<Contact>();
      List<Opportunity> listOfOpp = new List<Opportunity>();
      List<campaign>  listOfcamp = [select Id,Name from Campaign where id =: campaignId];
      List<Customer__c> listOfCustomer = [select id,Name from Customer__c limit 1];
      for(Customer__c c : listOfCustomer){
              Account a = new Account();
              a.Name = 'Test account' + Date.today();
             listOfAccount.add(a);
        }
         if(!listOfAccount.isEmpty()){
              insert listOfAccount;
        }
        System.debug('list of account-->>' + listOfAccount);
        for(Customer__c c : listOfCustomer){
          for(Account acc : listOfAccount){
              if(!listOfAccount.isEmpty() && listOfAccount != Null){

                Contact con = new Contact();
                con.LastName = 'Test con' + Date.today();
                con.AccountId = acc.id;
                listOfContact.add(con);
              }
          }
      }
        if(!listOfContact.isEmpty()){
              insert listOfContact;
        }
    }
  }

and want to do this by anonnymous window, basically my motive is to create multiple record from code.

  • what do you mean inserting multiple record? can you clear little bit more? – sanket kumar Aug 13 '18 at 12:36
  • when i say multiple records that mean i am inserting records on the basis of customers , so for example if 34 customer then 34 account,contact and opportunity should be inserted when i run this class in one click and they all are connected. – SalesforceFolks Aug 13 '18 at 12:42
  • what is the relation between the customer and account? – sanket kumar Aug 13 '18 at 12:46
  • no relation..i just want record on that basis – SalesforceFolks Aug 13 '18 at 12:47
1

You need to insert OpportunityContactRole to make your opportunity associated with Contact and an account.

This will complete your chaining. For every customer, there will be one account and one contact associated with that particular account. Both Account and opportunity will have the same Opportunity in their related list.

As you mentioned in the comment, if you have 34 customers record then it will create 34 accounts, contacts and opportunities connected to each other.

List<Account> listOfAccount = new List<Account>();
    List<Contact> listOfContact = new List<Contact>();
    List<Opportunity> listOfOpp = new List<Opportunity>();
    List<OpportunityContactRole> listOppRole = new List<OpportunityContactRole>();
    List<Customer__c> listOfCustomer = [select id,Name from Customer__c limit 1];
    for(Customer__c c : listOfCustomer){
        Account a = new Account();
        a.Name = 'Test account' + Date.today();
        listOfAccount.add(a);
    }
    system.debug(listOfAccount);
    if(!listOfAccount.isEmpty()){
        insert listOfAccount;
    }
    if(!listOfAccount.isEmpty() && listOfAccount != Null){
    for(Account acc : listOfAccount){
            Contact con = new Contact();
            con.LastName = 'Test con' + Date.today();
            con.AccountId = acc.id;
            listOfContact.add(con);
    }
        if(!listOfContact.isEmpty()){
            insert listOfContact;
        }
    Map<id,id> mapacntwithcon= new Map<id,id>();
    for(Contact con :listOfContact){
        mapacntwithcon.put(con.Accountid,con.id);
        Opportunity opp = new Opportunity();
        opp.Name='Test opp' + Date.today();
        opp.Closedate=Date.today();
        opp.stageName='Pre-Qualification';
        opp.AccountId=con.Accountid;
        listOfOpp.add(opp);
    }
    if(!listOfOpp.isEmpty()){
            insert listOfOpp;
    }
    for(Opportunity op:listOfOpp){
        OpportunityContactRole opprole = new OpportunityContactRole();
        opprole.ContactId=mapacntwithcon.get(op.Accountid);
        opprole.OpportunityId=op.id;
        opprole.Role='Decision Maker';
        opprole.IsPrimary=true;
        listOppRole.add(opprole);
    } 
    if(!listOppRole.isEmpty()){
            insert listOppRole;
        }    
    }

Hope it helps you.

  • Thank you @sanket will give a try and let you know.One doubt i have here that when we insert contact related to account the according to this code it will create all the contact related to one account. which i don't want. what i want is one account have one contact and one contact have one opportunity. – SalesforceFolks Aug 14 '18 at 5:58
  • No 1:1:1 ratio will be there.. Try once.. For one account there will be one contact and one opportunity related to both account and contact – sanket kumar Aug 14 '18 at 6:21
  • welcome @SalesforceFolks 😊 – sanket kumar Aug 14 '18 at 12:06

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.