Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am working on task where where i need to create a trigger. I have two object let say objA and objB. What i need to do is write a trigger on objA that will perform the same event(update/delete/insert/undelete) on objB . fields in objB is subset of fields in objA. Thanks and advance

share|improve this question
1  
Are the fields in ObjB of the same type (text, dec, integer, etc,) as their equivalents in ObjA and do they hold the same values (in essence is ObjB a mirror of ObjA)? Do you actually need to create a map or do you simply need to update the fields on both objects with the desire to use a single trigger? I ask some of these questions because if the values are the same, you don't necessarily need a map since the updates to both may already be in trigger.new and could be done in for loops for each one. That said, traditional architecture would recommend two triggers; one for each object. –  crmprogdev 7 hours ago
1  
For the benefit of all, please edit your question with answers to the above questions as best you can. If you have any code you've already written that would be helpful too in order to get more specific feedback. Thank you. –  crmprogdev 7 hours ago
    
No they don't have same number of fields(objB is subset of ObjA). Now what exactly i need to do is .. write a trigger on objA that will do all the task(inser/delete/update/undelete records) on objB that are performed on objA . Thanks a lot for replying to this... –  user2815826 54 mins ago
    
fields in objB have same name as that of objA. –  user2815826 38 mins ago

1 Answer 1

SObjects have get and set methods which you can use to set/get the values of fields. To implement this you could use a string map to work out what goes where:

Map<String, String> AFieldToBField = new Map<String, String>
{
  'SVCMP__xyz__c' => 'xyz__ c',
  'SVCMP__abc__c' => 'abc__ c'
};

for(String key : AFieldToBField.KeySet())
{
    objA.put(key, objB.get(key));
}

Obviously this is only going to work if the fields are of the same types on each object, otherwise you're going to need to deal with conversion as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.