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
sbansalsbansal 

Apex class

Hi

Salesforce Community
I am new to Apex & trigger

I have created two custom objects Member & Member2, and same fields for both the object, now i want to insert the values of member fields into Member2 fields, such as Firstname and lastname,

Help me to get code for this


Chamil MadusankaChamil Madusanka

Hi,

 

At what time do you want to insert values to Member2?

 

Assuming you want to insert valses to Member2 when inserting to Member;

 

You can write a trigger for Member to insert values to Member2.

 

trigger insertMember2 on Member__c (after insert) {
 Set<Id> Ids= new Set<Id>();
    for (Member__c member : Trigger.new)
    {
        Ids.add(member.Id);        
    }
 List<Member__c> memberList = new List<Member__c>([Select Id,FirstName__c,LastName__c  From Member__c e where Id in :Ids]);

	for(Member__c temp : memberList )
	{
		Member2__c member2 = new Member2();
		member2.FirstName__c = temp.FirstName__c;
		member2.LastName__c = temp.LastName__c;
		insert member2;

	}


 }

 

 Above is a example for get an idea.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

Shashikant SharmaShashikant Sharma

This is what your trigger should be like , And this will work for bulk data as well as insert statement is used out side the foor loop

trigger insertMember2 on Member__c (after insert) {

 List<Member2__c> listMember2ObjToInsert = new List<Member2__c>();
 for (Member__c memberObj : Trigger.new)
    {
        Member2__c member2Obj = new Member2();
	member2Obj.FirstName__c = memberObj.FirstName__c;
	member2Obj.LastName__c = memberObj.LastName__c;
        // If you have any refernce of member in member2 you should fill that as well
	listMember2ObjToInsert.add(member2Obj);
    }
insert listMember2ObjToInsert;
}

 

kzarkzar

Hi Shashikant,

 

I have a similar request and tried the trigger you replied with using the same details as the original problem (just for testing) but this does not run. It compiles without errors, but the result is not there. The fields do not update.

 

My trigger needs to apply to a custom field in the Quotes Line Item object checking the same (named) custom field in the Opportunity Line Items object, and inserting this record.

 

Our Sales don't want to double handle a picklist field and want this autocompleted when it comed to generating quotes.

 

Any assistance appreciated.

rajeshkarrirajeshkarri

I tried above trigger but it is not working please give me updates

I am getting below error message

 

kindly give me your inputs

Error: Compile Error: Incompatible element type SOBJECT:Member2__c for collection of SOBJECT:Member1__c at line 15 column 3