• Shruthi Javare Gowda
  • NEWBIE
  • 55 Points
  • Member since 2014

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
Hey

I have created a trigger to map my custom lead "Type" field to the AccountType field. I finally got the trigger to save, however when I try to convert a lead record and create a new account record I recevie the following error:

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, accountType: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Lead.ConvertedAccountId Trigger.accountType: line 9, column 1: [] (System Code)

Coul really use some help solving. Also the trigger is below:

trigger accountType on Account (before insert){
  Set<Id> accts = new Set<Id>();
  FOR(Account a: trigger.new){
     accts.add(a.Id);
  }
   List<Lead> leads=[SELECT Id, Type__c FROM Lead WHERE convertedAccountId IN: accts];
  Map<Id,String> leadMap=new Map<Id,String>();
  FOR(Lead l: leads){
   leadMap.put(l.ConvertedAccountID,l.Type__c);
  }
  FOR(Account a2: trigger.new){
    IF(leadMap.containsKey(a2.Id)){
      a2.Type=leadMap.get(a2.Id); 
    }
  }
}
I am fairly new to Apex.

I broke into 2 triggers, to get it to save .
They are supposed to look at the Opportunity Stage field and then update a custom field on Contacts -- Active_Opportunity__C (a checkbox).  

I am getting 0% code coverage on both and when I enter contact and new opportunity, the contact field is not populating?   What am I doing wrong?

Active_Opportunity__c (Contacts)
False 

Stage (Opportunities)
Closed Won
Closed Lost

Active_Opportunity__c (Contacts)
True

Stage (Opportunities)
Prospecting
Qualified Interest
Needs Analysis
Value Proposition
Proposal/Price Quote
Negotiation/Review
Verbal Commit
Closed Pending

Can I put these 2 into 1 and then would I just mock up an opporunity for the Unit Test?

trigger activeOpportunity on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Closed Won' 
        && optyObj.StageName == 'Closed Lost'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = False;
            }
            update(conList);
        }   
    }
}

trigger activeOpportunity2 on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Prospecting' 
        && optyObj.StageName == 'Qualified Interest'
        && optyObj.StageName == 'Needs Analysis'
        && optyObj.StageName == 'Value Proposition'
        && optyObj.StageName == 'Proposal/Price Quote'
        && optyObj.StageName == 'Negotiation/Review'
        && optyObj.StageName == 'Verbal Commit'
        && optyObj.StageName == 'Closed Pending'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = True;
            }
            update(conList);
        }   
    }
}
 
Can anybody share the KPI's to follow/track for successful implementation of salesforce projects
Hey

I have created a trigger to map my custom lead "Type" field to the AccountType field. I finally got the trigger to save, however when I try to convert a lead record and create a new account record I recevie the following error:

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, accountType: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Lead.ConvertedAccountId Trigger.accountType: line 9, column 1: [] (System Code)

Coul really use some help solving. Also the trigger is below:

trigger accountType on Account (before insert){
  Set<Id> accts = new Set<Id>();
  FOR(Account a: trigger.new){
     accts.add(a.Id);
  }
   List<Lead> leads=[SELECT Id, Type__c FROM Lead WHERE convertedAccountId IN: accts];
  Map<Id,String> leadMap=new Map<Id,String>();
  FOR(Lead l: leads){
   leadMap.put(l.ConvertedAccountID,l.Type__c);
  }
  FOR(Account a2: trigger.new){
    IF(leadMap.containsKey(a2.Id)){
      a2.Type=leadMap.get(a2.Id); 
    }
  }
}
I am fairly new to Apex.

I broke into 2 triggers, to get it to save .
They are supposed to look at the Opportunity Stage field and then update a custom field on Contacts -- Active_Opportunity__C (a checkbox).  

I am getting 0% code coverage on both and when I enter contact and new opportunity, the contact field is not populating?   What am I doing wrong?

Active_Opportunity__c (Contacts)
False 

Stage (Opportunities)
Closed Won
Closed Lost

Active_Opportunity__c (Contacts)
True

Stage (Opportunities)
Prospecting
Qualified Interest
Needs Analysis
Value Proposition
Proposal/Price Quote
Negotiation/Review
Verbal Commit
Closed Pending

Can I put these 2 into 1 and then would I just mock up an opporunity for the Unit Test?

trigger activeOpportunity on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Closed Won' 
        && optyObj.StageName == 'Closed Lost'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = False;
            }
            update(conList);
        }   
    }
}

trigger activeOpportunity2 on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Prospecting' 
        && optyObj.StageName == 'Qualified Interest'
        && optyObj.StageName == 'Needs Analysis'
        && optyObj.StageName == 'Value Proposition'
        && optyObj.StageName == 'Proposal/Price Quote'
        && optyObj.StageName == 'Negotiation/Review'
        && optyObj.StageName == 'Verbal Commit'
        && optyObj.StageName == 'Closed Pending'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = True;
            }
            update(conList);
        }   
    }
}