• Lisa French
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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;
    }   
}
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;
    }   
}