You need to sign in to do that
Don't have an account?
Vijay 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
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
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
Regards,
All Answers
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
Regards,
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 helper class which will prevent your trigger from executing again and again.
Let me know if this helps :)
Thanks,
Amit Singh