function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cl0s3rcl0s3r 

List and relationship manipulation

I need some help understanding how to capture related data.  I have a list that contains all the line items of an opportunity. I would like to create a case for every line item.  I dont understand how to assign the elements of the list to the case fields.  I would like the cas.Implementation_Name__c field to equal the Opportunity.Name field but since the Opportunity is a lookup via the Opportunity.Id field I dont understand. I have attached the code to better describe what I am attempting to accomplish.

 

Thanks for the help!

 

Carlos

 

 

public with sharing class ProdCase {
//capture the opportunity id from the button
	public static void prodDetails(String oppId){

//Create the list of lineitems owned by the Opportuntity 
		List<OpportunityLineItem> lin = [Select o.UnitPrice, o.TotalPrice, 
				o.Quantity, o.OpportunityId, o.ListPrice, o.Id 
				From OpportunityLineItem o where o.OpportunityId =:oppId];	

//Iterate through the list parsing out the field to the Case object fields until completed.				
			for(Integer I = 0; I < lin.size(); I++){
 			
//For every Line Item create case and populate the fields specified
				Case cas = new Case();
					cas.RecordTypeId='';
					cas.OwnerId='';
					cas.Implementation_Name__c=lin[I].OpportunityId;
					
//Insert Case record into Salesforce					
					Insert cas;
 		}
	}
}

 

 

 

grigri9grigri9

You can reference related records by <<lookup/master-detail field name>>.<<fieldname>>. Also, you want to avoid using extra DML statements so instead of inserting the records one by one you can add them all to a list and then insert the list.

 

 

public static void prodDetails(String oppId){

 List<OpportunityLineItem> lin = [Select o.UnitPrice, o.TotalPrice, 
				o.Quantity, o.Opportunity.Name, o.ListPrice, o.Id 
				From OpportunityLineItem o where o.OpportunityId =:oppId];	

list<Case> ToInsert = new list<Case>();
 for(Integer I = 0; I < lin.size(); I++){
 			
    Case cas = new Case();
    cas.RecordTypeId='';
    cas.OwnerId='';
    cas.Implementation_Name__c=lin[I].Opportunity.Name;
					
    ToInsert.add(cas);
  }
  insert ToInsert;
}