+ Start a Discussion
Iqra TechIqra Tech 

need to devlope query on three object for inserting records in newly created object according to query

need to devlope query on Opportunity for  id,name,stage & Oppounitylineitem for id,name & OpportunityContactRole for phone,email in a single query for this objects and this fields to be inserted in other custom object

i have devlope this query 
select id,name, (select id,name from OpportunityLineItems), (Select id from OpportunityContactRoles) from Opportunity 

but unale to insert records in my custom object:-
my cutstom obejct Name :- myobject__c
fields in that :- Oppid__c,Oppname__c,Productid__c,ProductName__c,Phone__c,Email__c need to insert in this object

can anyone please help me for to achive this requiremnt

Lokesh KumarLokesh Kumar
This is not the exact code for your question, but it will guide you to get there.
 
public class EmailBrokerReminder {
    public EmailBrokerReminder() {
        Map<id, contact> contactsMap = new Map<id, contact>();
        for ( Contact c : [ SELECT Id, Email, Name, (SELECT OpportunityId, Opportunity.Email_Broker_On__c
                                                     FROM OpportunityContactRoles ) 
                            FROM Contact 
                            WHERE Email_Broker_Reminder__c = true ] ) {
                            for ( OpportunityContactRole ocr : c.OpportunityContactRoles ) {
                                  System.debug(c.email + ' - ' + c.Name + ' - ' + ocr.id );
                                for ( Opportunity o : c.OpportunityContactRoles.Opportunities ) {
                                    
                                }
                            }
                            
            contactsMap.put(c.id, c);
                            
        }    
    }
}

 
Iqra TechIqra Tech
@Lokesh Kumar thanks for the reply i have created this working fine but getting governer limits can you please chehk this how to avoaid governer limits

public class mappingclass {
    public static void Insert_Records(){ 
    List<Mapping__c> lstCO = new List<Mapping__c>();
    for(Opportunity opp : [select id,name, (select id,name from OpportunityLineItems), (Select id,phone,email from OpportunityContactRoles) from Opportunity ])
    {
    system.debug(opp.OpportunityLineItems);
    Mapping__c co = new Mapping__c();
    co.Name = opp.name;
    for(OpportunityLineItem li : opp.OpportunityLineItems)
    {
     co.Oli_name =li.name;   
    }
    for(OpportunityContactRole ocr : opp.OpportunityContactRoles)
    {
        co.Ocr_contactid__c=ocr.id;
    }
    
    lstCO.add(co);
    }
    insert lstCO;
    }
  
  
}

now this class is working fine accordig to my requiremnt now i am just getting governer limits how can i avoaid can you please help in that
Lokesh KumarLokesh Kumar
Can you please add the error which you are receiving?
Iqra TechIqra Tech
Line: 51, Column: 1
System.LimitException: Too many DML rows: 10001
Lokesh KumarLokesh Kumar
The total number of the record you can insert is 10000 in your case there are more than 10000 records in the list "lstCO". 

Two possible solutions:
Reduce the size of the list by using the where clause.
Write a Batch class.
Iqra TechIqra Tech
As i am new in devlopment can you please write that wht you said please ..
Linga_RaminLinga_Ramin
Use below Query

for(Opportunity opp : [select id,name, (select id,name from OpportunityLineItems), (Select id,phone,email from OpportunityContactRoles) from Opportunity WHERE Name= 'Test'  LIMIT 1000]) 

OR

for(Opportunity opp : [select id,name, (select id,name from OpportunityLineItems), (Select id,phone,email from OpportunityContactRoles) from Opportunity  LIMIT 1000]) 
 
Iqra TechIqra Tech
No i just want to put thi class into the batch class can you please