+ Start a Discussion
anyioneta1.3025054394678596E12anyioneta1.3025054394678596E12 

Triggers

--------------------
|OBJECT_1    |
|------------------|
|First Name    |
|------------------|
|Last Name    |
|------------------|

 -------------------
|OBJECT_2    |
|------------------|
|First Name    |
|------------------|
|Last Name    |
|------------------|

How do i automatically enter the same values in First Name and Last Name  fields of OBJECT_2 , as soon as a user enters values in First Name and Last Name fields of OBJECT_1?


I know i can achieve it using triggers but do not know how to go about it?


Thanks.

 

 

Thanks nkit Arora , your Trigger Code was very helpfull in geting me started about writting triggers. But I still need the fields in Object2 to update automatically as soon as i update the fields in Object2.

How do I achieve this?



ngabraningabrani

Try to follow these steps -

 

1. The trigger will run on Insert/upsert of object 1.

2. Perform a query to get the record of object 2 that needs to be updated

3. Update the fields of this record of object 2.

4. Upsert object 2.

Ankit AroraAnkit Arora

I hope you want to create a record of object2 with same firstName and lastName of object1. Here is the trigger code which do same, but written only for insert case.

 

trigger UpdateName on object1 (after insert)
{
    if(Trigger.isAfter)
    {
        List<object2> LstToInsert = new List<object2>() ;
        for(object1 obj1 : Trigger.new)
        {
            LstToInsert.add(new object2(lastName = obj1.LastName , firstName = obj1.firstName)) ;
        }
        insert LstToInsert ;
    }
}

 Just replace Object1, Object2 with the API name of your objects and firstName and lastName with your field API names.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

anyioneta1.3025054394678596E12anyioneta1.3025054394678596E12

Thanks, What of whenever I update the fields in Object1, I also want the fields in Object2 to be to be updated.

What is the Trigger code for that?

Ankit AroraAnkit Arora

It will be bit long so I can tell you the approach you can go with.

 

1) Create a lookup filed of object1 on object2, so whenever object1 is updated you can fetch the related object2 records and update there name

 

2) Just fetch the object2 record with similar name of records of object1 using a query.

 

Let me know if it is not clear to you.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

anyioneta1.3025054394678596E12anyioneta1.3025054394678596E12

Actually, thats what am trying to avoid.

I do not want to use a look up field.

I just want it to upate automatically.

 

How can I achieve this with Trigger Code?

Ankit AroraAnkit Arora

If you do not want to go with the lookup approach then you can go with the second approach. But let me tell you some concerns regarding this

 

1) User is supposed not to change the firstName and LastName of object2 records

2) All records of object2 should have unique names so when we are updating object1 records we have only one object2 record with that name.

 

I don't think this is a good approach and would suggest you to go with the lookup field approach.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

anyioneta1.3025054394678596E12anyioneta1.3025054394678596E12

Definetly, it will have a unique field. But the situation I have encounterd really needs for me to have all the fields in Object_2 updated automatically from all the fields in object_1.

 

Thanks.

sfdcbynitesfdcbynite

You could use the lookup field as a method of retrieving the appropriate object2 record to be updated. Meaning, the lookup field itself would be hidden from the users and read only but then you could use it to retrieve object2, so you won't get duplicates.

 

     Object2__c obj2 = [select id, objid,... from Object2 where obj1id = object1.id];

 

001001

山东省大道等四大四大大大方点发送到