+ Start a Discussion
sbansalsbansal 

copy field values of one object to another object

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


Best Answer chosen by Admin (Salesforce Developers) 
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

All Answers

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

This was selected as the best answer
sessusessu
Hi I want to do update trigger, i have written a code but its not working please tell me where i am doing mistake, I have 2 custom obj if i update records of one obj next obj records will automatically get updated, i have created a look up that's api name is Name__c to member name in another object. below is my code, please help trigger updt on Member__c (before update) { Map mem= new Map(); for (integer i=0; ilstmem=new List(); for(Member2__c x : [select id, Name__c, FirstName__c from Member2__c where Name__c in : mem.keyset()]) { Member__c member = mem.get(x.Name__c); x.FirstName__c = member.FirstName__c; lstmem.add(x); } update lstmem; }
BradleeBradlee

Thanks Chamil,

This is helpful and I think I've adapted this to my two objects, but I don't know how to create a class that can work with this trigger. Could you offer help there too?

Chamil MadusankaChamil Madusanka

You can create class as normal way. then u can access methods of that class by using a instance of that class.

Red2678Red2678

Thanks this was super helpful!

 

~Red

BradleeBradlee

Thanks Chamil, but I don't know how to create one the normal way. Could you point me to more information about creating that? I'm not a developer but do understand the process however and can adapt blocks of code to my application fairly well if I have a good starting point.

sgm_force1sgm_force1

Hi,

 

Need some help urgently.

 

This solution is ok if there 1-2 fields, but what if there are 40 fields and I want to copy their value.

 

Is there a good way to do that?

 

Thanks in advance,

Siddharth

E-jazE-jaz

Hi All ,

 

I am new in Development , I need your help.

My Requirement is like that.

I have 2 custom Object Member and Member Location ,In Member Location there is 5 fields Country , Zip code ,State ,District and charecter Lenght .

And in Member Object there are 5 field Member Name ,Country ,Zip Code State and District .

what i have to do is when i create any New Member i need to fetch district and state based on Zip code from Member Location Object.

In Member Location there is already these data are present .

I need to fetch State and district  based on country and zip code .which are present in member location.

Hope u guys understand my Requirement.

Please help me out

Thanks in Advance

Alex van der MerweAlex van der Merwe
Hey Salesforce Community.

I have a field on the opportunity called DeliveryInstallationStatus__c (text field) and the same field DeliveryInstallationStatus__c on the quote object. I created a Process Builder which copies the info from the opportunity and places it on the quote. It works at the start, but then when I update the field on the opportunity and hit save I get an error. Basically the process builder is failing. 

Is the a way for this to be done in Apex so when the field is edited after the initial entry and then saved the field on the quote object will update and not throw me an error.

Thanks in advance for this help