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
Parth SrivastavaParth Srivastava 

please help in trigger on insert event

There are two custom objects A and B.B is a parent of A object.
Now on insert event, i want to write a trigger that matches name field on both objects and then fill lookup field on basis of the name match
GauravendraGauravendra
Hi Parth,
You can try something like this
trigger testTrigger on  objA (before insert) {
	
	List<String> nameObjB = new List<String>();
	
	for(objA a : Trigger.New) {
		nameObjB.add(a.Name);
	}
	
	List<objB> listB = [Select Id,Name from objB where Name IN : nameObjB ];
	Map<String,Id> mapObjB = new Map<String,Id>();
	for(objB ob : listB) {
		if(!mapObjB.containsKey(ob.Name))
			mapObjB.put(ob.Name,ob.Id);
	}
	
	
	for(objA a : Trigger.New) {
		if(mapObjB.containsKey(a.Name)) {
			a.ObjectB__c = mapObjB.get(a.Name);
		}
	}
	
}
Hope this helps!