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
Lisa FrenchLisa French 

Help with Opportunity Trigger

I had some help writing a trigger to pull information from a description field into account lookup fields so that we can click on the name and have quick access to the associated account record.  The problem is that now when we try to change that field, it will not let us.  We can change it, but as soon as we hit save, it goes back to the auto-populated data pulled in from the trigger.  We need to be able to change that and I am not a Salesforce Developer, I am an admin and have been puzzling over this way too long.  Any help would be greatly appreciated.  Please see the code below:

Trigger OpportunityTrigger on Opportunity (after insert, after update) {
    public List<String> contactTypeList = New List<String>{'General Contractor','Architect','Developer','Owner','Consultant'}; 
    LargoDataTest__c ltd = LargoDataTest__c.getOrgDefaults();
  
    if(trigger.isAfter && APEXUtil.IsNewTrigger==false && !ltd.StopTrigger__c){
        if(trigger.isInsert||trigger.isUpdate){
            List<Opportunity> AllOppList = New List<Opportunity>();             
            List<Opportunity> OpptoUpdateList = New List<Opportunity>(); 
            List<Account> AcctoInsertList = New List<Account>(); 
            List<String> AccNames = New List<String>();
            MAP<String,String> contactTypeMAP = New MAP<String,String>(); 
            MAP<String,Account> accMAP = New MAP<String,Account>();
            MAP<Id,MAP<String,String>> OpportunityMAP = New MAP<Id,MAP<String,String>>();
                        
            for(Account acc: [Select Name From Account]){accMAP.put(acc.Name, acc);} 
            
            for(Opportunity opp : trigger.new){AllOppList.add(New Opportunity(Id=opp.ID, Description = opp.Description));} 
            
            for(Opportunity opp : AllOppList){ 
                if(opp.Description!=null?opp.Description.contains('Contact Information')?true:false:false){               
                    contactTypeMAP = getContactType(opp.Description, contactTypeList);
                    for(String AccName: contactTypeMAP.values()){if(!AccNames.Contains(AccName)?(AccName!=null?true:false):false){AccNames.add(AccName);}}                    
                    for(String AccName: AccNames){if(!accMAP.Containskey(AccName)){AcctoInsertList.add(New Account(Name=AccName));}}                   
                    OpportunityMAP.put(opp.Id,contactTypeMAP);
                }          
            } 
            
            if(!AcctoInsertList.isEmpty()){                
                Database.SaveResult[] rsResult = Database.Insert(AcctoInsertList,false);
                for(Database.SaveResult database: rsResult){if(!database.isSuccess()){for(Database.Error err : database.getErrors()) {System.debug('Insert Error-->'+err.getMessage());}}}
                for(Account acc: AcctoInsertList){if(acc.Id != null) accMAP.put(acc.Name, acc);}
            }
            
            
            for(Opportunity oppT : AllOppList){
                for(Id oppId : OpportunityMAP.keySet()){
                    if(oppT.Id == oppId){
                        System.debug('oppT.Id-->'+oppT.Id);
                        oppT.General_Contractor__c=OpportunityMAP.get(oppId).Containskey('General Contractor')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('General Contractor'))?accMAP.get(OpportunityMAP.get(oppId).get('General Contractor')).Id:null):null;
                        oppT.Architect__c=OpportunityMAP.get(oppId).Containskey('Architect')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('Architect'))?accMAP.get(OpportunityMAP.get(oppId).get('Architect')).Id:null):null;
                        oppT.Architect_2__c=OpportunityMAP.get(oppId).Containskey('Architect2')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('Architect2'))?accMAP.get(OpportunityMAP.get(oppId).get('Architect2')).Id:null):null;
                        oppT.Developer__c=OpportunityMAP.get(oppId).Containskey('Developer')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('Developer'))?accMAP.get(OpportunityMAP.get(oppId).get('Developer')).Id:null):null;
                        oppT.Owner__c=OpportunityMAP.get(oppId).Containskey('Owner')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('Owner'))?accMAP.get(OpportunityMAP.get(oppId).get('Owner')).Id:null):null;
                        oppT.Consultant__c=OpportunityMAP.get(oppId).Containskey('Consultant')?(accMAP.containsKey(OpportunityMAP.get(oppId).get('Consultant'))?accMAP.get(OpportunityMAP.get(oppId).get('Consultant')).Id:null):null;
                        OpptoUpdateList.add(oppT);                             
                    }                           
                }                
            }           
            if(!OpptoUpdateList.isEmpty()){
                APEXUtil.IsNewTrigger = true;                
                Database.SaveResult[] rsResult = Database.update(OpptoUpdateList,false);
                for(Database.SaveResult database: rsResult){if(!database.isSuccess()){for(Database.Error err : database.getErrors()) {System.debug('Update Error-->'+err.getMessage());}}}          
            }           
        } 
    }
    
    public MAP<String,String> getContactType (String Description, List<String> contactTypeListAux){
        MAP<String,String> contactTypeMAP = New MAP<String,String>();
        Integer indexCTAux0 = Description.indexOf('Contact Information'), indexCTAux1 = -1,indexCTAux2 = -1;     
            
        for(String ct: contactTypeListAux){            
            if(Description.contains(ct)){
                indexCTAux1 = Description.indexOf(ct+':',indexCTAux0)!=-1?Description.indexOf(ct+':',indexCTAux0):Description.indexOf(ct+',',indexCTAux0)!=-1?Description.indexOf(ct+',',indexCTAux0):-1;
                System.debug('ct1-->indexCTAux1:' + ct+'-->'+indexCTAux1);
                if(indexCTAux1!=-1?(Description.charAt(indexCTAux1-1)==10 || Description.charAt(indexCTAux1-1)==32?true:false):false){                        
                    contactTypeMAP.put(ct,getAccount(Description,indexCTAux1));
                    if(ct=='Architect'){
                        indexCTAux2=indexCTAux1;
                        indexCTAux1 = Description.indexOf(ct+':',indexCTAux2+1)!=-1?Description.indexOf(ct+':',indexCTAux2+1):Description.indexOf(ct+',',indexCTAux2+1)!=-1?Description.indexOf(ct+',',indexCTAux2+1):-1;
                        if(indexCTAux1!=-1?(Description.charAt(indexCTAux1-1)==10 || Description.charAt(indexCTAux1-1)==32?true:false):false){
                            contactTypeMAP.put('Architect2',getAccount(Description,indexCTAux1));                      
                        }                             
                    }
                }                
              
            }
        }        
        return contactTypeMAP;
    }
    
    public String getAccount (String Description, Integer indexCT){
        Integer StartNA = Description.indexOfChar(10,indexCT); 
        Integer EndNA = StartNA!=-1?Description.indexOfChar(10,StartNA+1):-1;
        return StartNA!=-1 && StartNA!=-1?Description.substring(StartNA+1, EndNA-1):Null;
    }   
}
Best Answer chosen by Lisa French
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/25320/how-to-write-a-trigger-on-opportunity

Use the code snippet as mentioned suggested in the above blog.


For further reference check this too (https://trailblazers.salesforce.com/answers?id=9063A000000pOAvQAM).

All Answers

RituSharmaRituSharma
I really could not understand the problem statement and which part of code is related to that. Please elaborate.
Lisa FrenchLisa French
The problem is that we have custom fields called General Contractor, Architect1, Architect 2, Developer...etc...and those fields are being populated by grabbing the text that lives in the description box which is placed there by another app integration called BuildCentral, under Contact Information, if there is a person listed as an Architect, it is mapped into the Architect field. If there is a 2nd Architect, that name is mapped into the Architect 2 field. So when we try to overwrite that information because we have decided to use a different Architect, it will not let us. As soon as we hit save, the information pulled using the trigger repopulates. We need to be able to edit those fields.
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/25320/how-to-write-a-trigger-on-opportunity

Use the code snippet as mentioned suggested in the above blog.


For further reference check this too (https://trailblazers.salesforce.com/answers?id=9063A000000pOAvQAM).
This was selected as the best answer
Lisa FrenchLisa French
Thank you.