Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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
3 answers
  1. Jan 23, 2017, 8:46 AM
    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,
Loading
0/9000