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
Salesforce@322Salesforce@322 

how to insert record by using wrapper class

Hi All,

am new to wrapper class.I got an urgent requirement like i have 3 objects
as object 1
object 2
object 3
i need to get 3 objects data and with some calculations on the field i need to insert the
record in object 2 with the remaining objects data in apex class.
I used for loops for the above requirement but it is throwing "CPU TIME LIMIT EXCEEDED" exception
so, i thought it is better to take help of wrapper class.
Is it my idea is correct ?
Can we insert record by using wrapper class which contains 3 custom objects data ?
If it is possible means please provide any sample code or links whatever.

Thanks ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Deepak Kumar ShyoranDeepak Kumar Shyoran
Hope below code will help to to understand the concept the Wrapper class in may solve your problem.
public class MainClass {
	
	// Wrapper class we need to display info for all three object
	public List<WrapperClass> wrapperList { get; set;}

	// main class constructer
	public MainClass() {
                wrapperList = new List<WrapperClass>() ;
		List<Account> accList = [Select id,Name from Account limit ] ;
		List<Contact> conList = [Select id,LastName  from Contact ] ;
		 
		// list of custom obj we need to insert by using data from two native object
		List<Custom_Obj__c> cusList = new List<Custom_Obj__c>() ;

		for(Integer count = 0 ; count <20 ; count ++ ) {
			cusList.add( new Custom_Obj__c(Name = accList.get(count).Name,PrentCon__c = conList.get(count).LastName) );
                        wrapperList.add(accList.get(count),conList.get(count),cusList.get(count);
		}

		insert cusList ;
	}
	
	//Wrapper class
	class WrapperClass {
		public Account object1 {get; set;}
		public Contact object 2 {get; set;}
		public Custom_Obj__c {get; set;}

		//Wrapper constructer
		public WrapperClass(Account object1,object2,Custom_Obj__c object3 ) {
			this.object1 = object1 ;
			this.object2 = object2 ;
			this.object3 = object3;
	 
		}
	}
}

AshlekhAshlekh
Hi,

I've mention some good Links here that will helps you and also help to you to understand the concept of Wrapper class in Apex.

https://developer.salesforce.com/page/Wrapper_Class

https://success.salesforce.com/answers?id=90630000000hSoLAAU (This is the same question like your question)


If this links help you than please mark it as solution and help other. Enjoy APEX
Salesforce@322Salesforce@322
Hi Deepak,

Thank you soo much for your fast reply

Actually my requirement is :
1.Iam using 3 objects
2.OBJECT 1,2,3  's data should be copied to OBJECT 1
by doing some calculations for every field
3.Every field calculation are in seperate methods in the same class which are taking the object 1,object2,object3 field values and then calculating and finally returning the value
4.so, i need to call those methods when ever am assigning the values to object 1 fields
and then finally insert

This is urgent i have to fix this issue today itself ,So am hoping that i will get the reply from you soon
Thanks,,,,,,,,,,,,,,,
Salesforce@322Salesforce@322
Hi Ashlekh,

Thank you soooo much for your links,
defenetly i will look into that links so that will help for me to know about the wrapper class
Deepak Kumar ShyoranDeepak Kumar Shyoran
Modify your method (in which you are doing calculation ) so that it'll take two Objects as a parementer on whihch you need to do some calculation and will return the third Object (on which you have performed the Calculation) . Now call that method by passing parameter from for loop.
for(Integer count = 0 ; count <20 ; count ++ ) {
 cusList.add( new Custom_Obj__c(accList.get(count),conList.get(count),calculationMthod(accList.get(count),conList.get(count)) );
                        wrapperList.add(accList.get(count),conList.get(count),cusList.get(count);
  }

private Custom_Obj__c3 calculationMthod(Account acc, Contact con) {

Custom_Obj__c3 cusObj3 = new Custom_Obj__c() ;

...
..
return cusObj3
}



Cloud_forceCloud_force
see if this helps you:
http://www.forcexplore.com/2014/07/wrapper-class-in-salesforce.html