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
RainerRichterRainerRichter 

Need Support with an apex trigger

Hello all, 
I'm trying to build a trigger which updates field in a ralted custom object. 
The objects are:
- EDWPM__ITRQ_Positions__c
   -field: EDWPM__EmpID__c

should be inserted in 

- EDWPM__Hardware_CI__c
  -field: EDWPM__ci_employee__c

after Update.

The trigger shows no syntax errors in developer console, but doesn't do anything. Maybe someone has an idea and can help?
Here is my code so far:
Thx! Rainer
trigger HardwareassignProcess on EDWPM__ITRQ_Positions__c (after update) {

	List<EDWPM__ITRQ_Positions__c> posList = new List<EDWPM__ITRQ_Positions__c>();
    
    for(EDWPM__ITRQ_Positions__c pos : posList){

     if(trigger.newMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c != trigger.oldMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c){
                
         EDWPM__Hardware_CI__c CI = [SELECT ID from EDWPM__Hardware_CI__c where ID=:pos.EDWPM__Hardware_CI_dispatching__c];
         
         CI.EDWPM__ci_employee__c = pos.EDWPM__EmpID__c ;   
             
         update CI;
     } 
  }
}

 
Best Answer chosen by RainerRichter
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger HardwareassignProcess on EDWPM__ITRQ_Positions__c (after update) 
{

	Set<Id> setHardwareId = new Set<Id>();	
    for(EDWPM__ITRQ_Positions__c pos : Trigger.new )
	{
		if( pos.EDWPM__Hardware_CI_dispatching__c != trigger.oldMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c )
		{
			setHardwareId.add(pos.EDWPM__Hardware_CI_dispatching__c);
		}
	}
	
	if(setHardwareId.size() >0 )
	{	
		Map<Id,EDWPM__Hardware_CI__c> mapCI = new Map<Id,EDWPM__Hardware_CI__c>( [SELECT ID,EDWPM__ci_employee__c from EDWPM__Hardware_CI__c where ID in :setHardwareId ] );
		List<EDWPM__Hardware_CI__c> lstHard = new List<EDWPM__Hardware_CI__c>();
		for(EDWPM__ITRQ_Positions__c pos : Trigger.new )
		{
			if( pos.EDWPM__Hardware_CI_dispatching__c != trigger.oldMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c )
			{
				if( mapCI.containsKey(pos.EDWPM__Hardware_CI_dispatching__c) )
				{
					EDWPM__Hardware_CI__c CI = mapCI.get(pos.EDWPM__Hardware_CI_dispatching__c);
					
					CI.EDWPM__ci_employee__c = pos.EDWPM__EmpID__c ; 
					lstHard.add(CI);
				}	
			}
		}		
		if(lstHard.size() > 0)
		{
			update lstHard;
		}
	}
}

Let us know if this will help you
 

All Answers

RainerRichterRainerRichter
I forgot to say. It should only update, when the field EDWPM__Hardware_CI_dispatching__c from object EDWPM__ITRQ_Positions__c has changed.
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger HardwareassignProcess on EDWPM__ITRQ_Positions__c (after update) 
{

	Set<Id> setHardwareId = new Set<Id>();	
    for(EDWPM__ITRQ_Positions__c pos : Trigger.new )
	{
		if( pos.EDWPM__Hardware_CI_dispatching__c != trigger.oldMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c )
		{
			setHardwareId.add(pos.EDWPM__Hardware_CI_dispatching__c);
		}
	}
	
	if(setHardwareId.size() >0 )
	{	
		Map<Id,EDWPM__Hardware_CI__c> mapCI = new Map<Id,EDWPM__Hardware_CI__c>( [SELECT ID,EDWPM__ci_employee__c from EDWPM__Hardware_CI__c where ID in :setHardwareId ] );
		List<EDWPM__Hardware_CI__c> lstHard = new List<EDWPM__Hardware_CI__c>();
		for(EDWPM__ITRQ_Positions__c pos : Trigger.new )
		{
			if( pos.EDWPM__Hardware_CI_dispatching__c != trigger.oldMap.get(pos.Id).EDWPM__Hardware_CI_dispatching__c )
			{
				if( mapCI.containsKey(pos.EDWPM__Hardware_CI_dispatching__c) )
				{
					EDWPM__Hardware_CI__c CI = mapCI.get(pos.EDWPM__Hardware_CI_dispatching__c);
					
					CI.EDWPM__ci_employee__c = pos.EDWPM__EmpID__c ; 
					lstHard.add(CI);
				}	
			}
		}		
		if(lstHard.size() > 0)
		{
			update lstHard;
		}
	}
}

Let us know if this will help you
 
This was selected as the best answer
RainerRichterRainerRichter
Hello Amit, this works perfect. Great help. Thanks a lot! Rainer