+ Start a Discussion
Vijay sidaraddiVijay sidaraddi 

Trigger to update the lookup field.

I have a  scenario-
 
There are 2 objects-
 
SalesInfluenceReportings : It is loaded with data corresponding to CMT.
Masters : It is loaded with master data for all types of OG.
 
Now, we need to have a relationship and based on MMS Id from SalesInfluenceReportings and Opportunity Id from Masters these should be related.
 
I have created a trigger on after insert/update to populate the field but it is not updating. Can you please look into the code and help me if I am missing something.
Trigger Name : updateMaster


trigger updateMaster on SalesInfluenceReporting__c (after insert, after update) {
 
  Map<Id, String> idMap = new Map<Id, String>();
  Boolean matchOnce = false;

    for(SalesInfluenceReporting__c obj :trigger.new)

    {
        idMap.put(obj.Name,obj.MMS_Id__c);
        
        system.debug('********'+idMap);
       
     }
     
     

 List<Master__c> mList =[Select Id,Opportunity_Id__c from Master__c ]; 
    
 List<SalesInfluenceReporting__c > ul = new List<SalesInfluenceReporting__c >();
 
 for(SalesInfluenceReporting__c obj :trigger.new)
{
     for(Master__c m :mList ){
        
           SalesInfluenceReporting__c  rectoUpdate = new SalesInfluenceReporting__c ();
                system.debug('+++++++++'+idMap.get(obj.id));
                if(matchOnce == false){
                if(m.Opportunity_Id__c == idMap.get(obj.id)){
                rectoUpdate.Id = obj.id;
                rectoUpdate.Master__c = m.id;
                matchOnce = true;
                ul.add(rectoUpdate );  
                system.debug('-------'+rectoUpdate );   
           }
          }
         break;
      }
   }
    system.debug('@@@@@@@@'+ul);
    update ul;
}

Can someone tell me where im wrong its not updating .

Thanks
Vijaykumar.S
Best Answer chosen by Vijay sidaraddi
Nishad KNishad K
Hi Vijay,

Here you have logical problem  that is you are trying to update same object   SalesInfluenceReporting__c   so  the trigger  will execute   recursively . you have to prevent recursive trigger  exicution or change the logic to before trigger .

see the below example:
add the below class
 
trigger updateMaster on SalesInfluenceReporting__c (after insert, after update) {
 if(updation.isfutureupdate!=true){
  Map<Id, String> idMap = new Map<Id, String>();
  Boolean matchOnce = false;

    for(SalesInfluenceReporting__c obj :trigger.new)

    {
        idMap.put(obj.Name,obj.MMS_Id__c);
        
        system.debug('********'+idMap);
       
     }
     
     

 List<Master__c> mList =[Select Id,Opportunity_Id__c from Master__c ]; 
    
 List<SalesInfluenceReporting__c > ul = new List<SalesInfluenceReporting__c >();
 
 for(SalesInfluenceReporting__c obj :trigger.new)
{
     for(Master__c m :mList ){
        
           SalesInfluenceReporting__c  rectoUpdate = new SalesInfluenceReporting__c ();
                system.debug('+++++++++'+idMap.get(obj.id));
                if(matchOnce == false){
                if(m.Opportunity_Id__c == idMap.get(obj.id)){
                rectoUpdate.Id = obj.id;
                rectoUpdate.Master__c = m.id;
                matchOnce = true;
                ul.add(rectoUpdate );  
                system.debug('-------'+rectoUpdate );   
           }
          }
         break;
      }
   }
    system.debug('@@@@@@@@'+ul);
    updation.isfutureupdate=true;
    update ul;
}
}


public class updation
{
public static boolean isfutureupdate=false;
}

Regards,

All Answers

Nishad KNishad K
Hi Vijay,

Here you have logical problem  that is you are trying to update same object   SalesInfluenceReporting__c   so  the trigger  will execute   recursively . you have to prevent recursive trigger  exicution or change the logic to before trigger .

see the below example:
add the below class
 
trigger updateMaster on SalesInfluenceReporting__c (after insert, after update) {
 if(updation.isfutureupdate!=true){
  Map<Id, String> idMap = new Map<Id, String>();
  Boolean matchOnce = false;

    for(SalesInfluenceReporting__c obj :trigger.new)

    {
        idMap.put(obj.Name,obj.MMS_Id__c);
        
        system.debug('********'+idMap);
       
     }
     
     

 List<Master__c> mList =[Select Id,Opportunity_Id__c from Master__c ]; 
    
 List<SalesInfluenceReporting__c > ul = new List<SalesInfluenceReporting__c >();
 
 for(SalesInfluenceReporting__c obj :trigger.new)
{
     for(Master__c m :mList ){
        
           SalesInfluenceReporting__c  rectoUpdate = new SalesInfluenceReporting__c ();
                system.debug('+++++++++'+idMap.get(obj.id));
                if(matchOnce == false){
                if(m.Opportunity_Id__c == idMap.get(obj.id)){
                rectoUpdate.Id = obj.id;
                rectoUpdate.Master__c = m.id;
                matchOnce = true;
                ul.add(rectoUpdate );  
                system.debug('-------'+rectoUpdate );   
           }
          }
         break;
      }
   }
    system.debug('@@@@@@@@'+ul);
    updation.isfutureupdate=true;
    update ul;
}
}


public class updation
{
public static boolean isfutureupdate=false;
}

Regards,
This was selected as the best answer
Amit Singh 1Amit Singh 1
Hi Vijay,

The record is not updating because un idMap you are putting Name as key and in the second Loop where you are matching master record you are getting the value by its id.  

Try the below code.
 
trigger updateMaster on SalesInfluenceReporting__c (after insert, after update) {
 if(preventRecursiveTrigger.isfutureupdate) return;
  Map<Id, String> idMap = new Map<Id, String>();
  Boolean matchOnce = false;

    for(SalesInfluenceReporting__c obj :trigger.new)

    {
        idMap.put(obj.Name,obj.MMS_Id__c);
        system.debug('********'+idMap);
       
     }
     
     
 List<Master__c> mList =[Select Id,Opportunity_Id__c from Master__c ]; 
    
 List<SalesInfluenceReporting__c > ul = new List<SalesInfluenceReporting__c >();
 
 for(SalesInfluenceReporting__c obj :trigger.new){
     for(Master__c m :mList ){
        
		    SalesInfluenceReporting__c  rectoUpdate = new SalesInfluenceReporting__c ();
            system.debug('+++++++++'+idMap.get(obj.id));
            if(matchOnce == false){
                if(m.Opportunity_Id__c == idMap.get(obj.Name)){
					rectoUpdate.Id = obj.id;
					rectoUpdate.Master__c = m.id;
					matchOnce = true;
					ul.add(rectoUpdate );  
					system.debug('#### -------'+rectoUpdate );   
				}
			}
         break;
    }
}
    system.debug('@@@@@@@@'+ul);
    updation.isfutureupdate=true;
    update ul;
}

Trigger helper class which will prevent your trigger from executing again and again.
 
public class preventRecursiveTrigger{
        public static boolean isfutureupdate=false;
}

 Let me know if this helps :)

Thanks,
Amit Singh
Vijay sidaraddiVijay sidaraddi
its working finet hanks lot