You need to sign in to do that
Don't have an account?
Shruthi Ravi 8
Should be able to update the status field once the record is saved
Hi,
Im unable to save the status field on the record once the record is saved. It throws me an error "opp already exists this record :: !!!" Please help me to fix it.
Below is the triigger which is throwing the error
Im unable to save the status field on the record once the record is saved. It throws me an error "opp already exists this record :: !!!" Please help me to fix it.
Below is the triigger which is throwing the error
trigger SegmentationRequestsTrigger on Segmentation_Request__c (before insert,before Update) { Map<String,String> agencybyOppId = new Map<String,String>(); Map<Id,List<String>> agencyIdByOppId = new Map<Id,List<String>>(); String tempString = ''; Set<String> agencyId = new Set<String>(); for(Segmentation_Request__c SegmentationRequest : [SELECT Agency_ID__c, Approval_Status__c, Opportunity__c FROM Segmentation_Request__c WHERE Opportunity__c != '']){ if(String.isNotBlank(SegmentationRequest.Agency_ID__c) && String.isNotBlank(SegmentationRequest.Approval_Status__c)){ tempString = SegmentationRequest.Agency_ID__c.trim()+':'+SegmentationRequest.Approval_Status__c; if(SegmentationRequest.Approval_Status__c == 'Requested'){ if(agencyIdByOppId != null && agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c)){ List<String> temp = agencyIdByOppId.get(SegmentationRequest.Opportunity__c); temp.add(tempString); agencyIdByOppId.put(SegmentationRequest.Opportunity__c,temp); }else{ agencyIdByOppId.put(SegmentationRequest.Opportunity__c,new List<String>{tempString}); } } //segIdByAgencyId.put(SegmentationRequest.Id,SegmentationRequest.Approval_Status__c); agencybyOppId.put(SegmentationRequest.Agency_ID__c.trim(),SegmentationRequest.Opportunity__c); } } for(Segmentation_Request__c SegmentationRequest : Trigger.New){ if(String.isNotBlank(SegmentationRequest.Agency_ID__c) && String.isNotBlank(SegmentationRequest.Approval_Status__c) && (Trigger.isInsert || (Trigger.isUpdate && ((SegmentationRequest.Agency_ID__c != trigger.oldMap.get(SegmentationRequest.Id).Agency_ID__c)|| (SegmentationRequest.Approval_Status__c != trigger.oldMap.get(SegmentationRequest.Id).Approval_Status__c))))){ system.debug('@@## SegmentationRequest.Opportunity__c '+SegmentationRequest.Opportunity__c); system.debug('@@## agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c) '+agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c)); if(agencyIdByOppId != null && agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c)){ String strPreRequested = SegmentationRequest.Agency_ID__c.trim() + ':Requested'; tempString = SegmentationRequest.Agency_ID__c.trim()+':'+SegmentationRequest.Approval_Status__c; List<String> temp = agencyIdByOppId.get(SegmentationRequest.Opportunity__c); system.debug('@@## Update '+tempString); if(temp != null && temp.size() > 1 ){ for(String agencywithStatus : temp){ system.debug('@@## agencywithStatus '+agencywithStatus); system.debug('@@## strPreRequested '+strPreRequested); system.debug('@@## 1 '+(agencywithStatus == tempString)); system.debug('@@## 2 '+(strPreRequested == agencywithStatus)); if(agencywithStatus == tempString || strPreRequested == agencywithStatus){ system.debug('@@## Same Id '); system.debug('@@## '+agencybyOppId.containsKey(SegmentationRequest.Agency_ID__c.trim())); system.debug('@@## '+agencybyOppId.get(SegmentationRequest.Agency_ID__c.trim())); system.debug('@@## '+SegmentationRequest.Opportunity__c); if(agencybyOppId != null && agencybyOppId.containsKey(SegmentationRequest.Agency_ID__c.trim()) && agencybyOppId.get(SegmentationRequest.Agency_ID__c.trim()) == SegmentationRequest.Opportunity__c ){ SegmentationRequest.addError('opp already exists this record :: !!!'); } } } } } } }