• Brittanie
  • NEWBIE
  • 25 Points
  • Member since 2013

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 18
    Replies

My trigger works great but...

After 80 drafts of the test I can't get it to work.... I need to make about 4 more triggers similar to this so this one test is kinda of crucial to recreate it for the others.

 

Currently getting the following error:

 

Error Message System.QueryException: List has no rows for assignment to SObject Stack Trace Class.UpdateAcctContactDecliningTestClass.myUnitTest: line 22, column 1

 

@isTest

private class UpdateAcctContactDecliningTestClass { 

   static testMethod void myUnitTest(){
    Account accts = new Account (Contact_Declining__c = Null);
      
          System.debug('Created Account with Id: ' + accts.id);

        
            // fill in all other required fields fields
   
   
    Opportunity o = new Opportunity(AccountId=accts.Id,Primary_Contact__c = '003000000001NjA');
        
   
   o.Primary_Contact__c = '003000000001NjA';
    
    Test.startTest();
    
        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c,[SELECT Contact_Declining__c FROM Account WHERE Id = :accts.Id].Contact_Declining__c ); 
       
             update accts;

       
   Test.stopTest();

    }
}

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
  update accts; //do the update

}
 }

 

I'm learning as I go with this but have successfully manipulated a good 5 triggers.  Can't seem to figure this one out and besides necessary it is also the solution to why a workflow that works fine in sadbox wont let me save without this field on the account filled in on my live sf.  I apologize after about 80 rounds and trying to manipulate off other posts the syntax my be destroyed!

 

Account

Contact_Declining__c

 

needs to be updated from the Primary_Contact__C on the Opportunity.

 

Trigger UpdateContactDeclining on Account(before insert, before update)
{

Set<id> OpportunityIds = new Set<id>();
for (Account a : Trigger.new)
{
OpportunityIds.add(a.Contact_Declining__c );
}
Map<Id, Opportunity> OpportunityContactMap = new Map<id, Opportunity>(
[SELECT Id,Primary_Contact__c FROM Opportunity WHERE Id IN :OpportunityIds]);

for (Account a: Trigger.new) 
{
if (a.Contact_Declining__c == null && OpportunityContactMap .containsKey(a.Opportunity )) 
{
a.Contact_Declining__c = OpportunityContactMap.get (Opportunity.Primary_Contact__c);
}
}
}

 

I have a tigger that works great with a variety of settings and workflows to auto convert lead on approval BUT I want it to be done without creating the opportunity.  I have a custom button to convert without a opportunity but it has to be done mannually... How do I make my trigger use my custon Convert_No_Opp button!!

 

Any help would be great!

 

trigger ConvertLead on Lead (after insert, after update) {
for (Lead lead : Trigger.new) {
if ((lead.isConverted == false)&&(lead.Status == 'Assigned')&& (lead.Exclude_from_workflow__c == false)) //to prevent recursion 
{
 Database.LeadConvert lc = new Database.LeadConvert();
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
 lc.setConvertedStatus(convertStatus.MasterLabel);
 
 if (lead.Exclude_from_workflow__c == false){
 Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());}
}
}
 }

 

I wrote a trigger that works in sandbox but have to now write a trigger test (class) for it and although I am learning through doing, I am officially lost.  please help

 

Trigger TaskBefore on Task(before insert, before update){
   
   
    Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>();
    
    for(Task t : trigger.new){
       
        if(t.WhoId != null){
            
            if(!whoIdsMap.containsKey(t.WhoId)){
                
                List<Task> temp = new List<Task>();
                
                temp.add(t);
               
                whoIdsMap.put(t.WhoId, temp);
            }else{
                
                whoIdsMap.get(t.WhoId).add(t);
            }
        }
    }
   
    
    for(Contact con : [Select Id, Name,Title from Contact where Id in :whoIdsMap.keySet()]){
        
        for(Task t :whoIdsMap.get(con.Id)){
            t.Contacts_Title__c = con.Title;
        }
    }
}

 

Trying to write (my first) trigger to pull from a picklist field on the Contact to a duplicate picklist on a Task field.

Contact field info is 

Field Label: SOI

Object Name: Contact

Field Name: SOI

Data Type: Picklist

API Name: SOI__c

 

Task Field Info is

Field Label: Contact's SOI

Object Name: Activity

Field Name: Contacts_SOI

Data Type: Picklist

API Name: Contacts_SOI__c

 

I currently receive the error message:

Error: Compile Error: Incompatible key type Schema.SObjectField for MAP<Id,LIST<Task>> at line 6 column 20
 

 

The code that I am trying to use is:

 

Trigger TaskBefore on Task(before insert, before update){
Map<Id, List<Task>> whoIds = new Map<Id, List<Task>>{};
For (Task t : trigger.new)
If(t.WhoId != null){
List<Task> tasks = whoIds.get(task.WhoId);
If (tasks == null){
tasks = new List<Task>{};
whoIds.put(t.WhoId, tasks);
}
tasks.add(t);
}
For(Contact con : [Select Id, Name,SOI__c from Contact where Id in :whoIds.keySet()])
For(Task t : whoIds.get(con.id))

t.contacts_SOI__c = con.SOI__c;}

 

Please help!

 

 

My trigger works great but...

After 80 drafts of the test I can't get it to work.... I need to make about 4 more triggers similar to this so this one test is kinda of crucial to recreate it for the others.

 

Currently getting the following error:

 

Error Message System.QueryException: List has no rows for assignment to SObject Stack Trace Class.UpdateAcctContactDecliningTestClass.myUnitTest: line 22, column 1

 

@isTest

private class UpdateAcctContactDecliningTestClass { 

   static testMethod void myUnitTest(){
    Account accts = new Account (Contact_Declining__c = Null);
      
          System.debug('Created Account with Id: ' + accts.id);

        
            // fill in all other required fields fields
   
   
    Opportunity o = new Opportunity(AccountId=accts.Id,Primary_Contact__c = '003000000001NjA');
        
   
   o.Primary_Contact__c = '003000000001NjA';
    
    Test.startTest();
    
        //verify if the trigger did the job  
        System.assertEquals(o.Primary_Contact__c,[SELECT Contact_Declining__c FROM Account WHERE Id = :accts.Id].Contact_Declining__c ); 
       
             update accts;

       
   Test.stopTest();

    }
}

 

trigger UpdateAcctContactDeclining on Opportunity (before insert, before update)
{
   //map to hold the account id and the corresponding related opportunity
    Map<Id,Opportunity> mapAccIdToOpp=new Map<Id,Opportunity>();
    List<Id> AccountsToUpdate = new List<Id>{};

  // Find accounts to update
  for(Opportunity o: Trigger.new){
    if (o.AccountId != Null)  
    {
      AccountsToUpdate.add(o.AccountId);
    }

  // Update the accounts
  Account[] accts = [SELECT Id, Contact_Declining__c FROM Account WHERE Id IN :AccountsToUpdate];

  for(Account a: accts){
    a.Contact_Declining__c = o.Primary_Contact__c ;
  }
  update accts; //do the update

}
 }

 

I'm learning as I go with this but have successfully manipulated a good 5 triggers.  Can't seem to figure this one out and besides necessary it is also the solution to why a workflow that works fine in sadbox wont let me save without this field on the account filled in on my live sf.  I apologize after about 80 rounds and trying to manipulate off other posts the syntax my be destroyed!

 

Account

Contact_Declining__c

 

needs to be updated from the Primary_Contact__C on the Opportunity.

 

Trigger UpdateContactDeclining on Account(before insert, before update)
{

Set<id> OpportunityIds = new Set<id>();
for (Account a : Trigger.new)
{
OpportunityIds.add(a.Contact_Declining__c );
}
Map<Id, Opportunity> OpportunityContactMap = new Map<id, Opportunity>(
[SELECT Id,Primary_Contact__c FROM Opportunity WHERE Id IN :OpportunityIds]);

for (Account a: Trigger.new) 
{
if (a.Contact_Declining__c == null && OpportunityContactMap .containsKey(a.Opportunity )) 
{
a.Contact_Declining__c = OpportunityContactMap.get (Opportunity.Primary_Contact__c);
}
}
}

 

I wrote a trigger that works in sandbox but have to now write a trigger test (class) for it and although I am learning through doing, I am officially lost.  please help

 

Trigger TaskBefore on Task(before insert, before update){
   
   
    Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>();
    
    for(Task t : trigger.new){
       
        if(t.WhoId != null){
            
            if(!whoIdsMap.containsKey(t.WhoId)){
                
                List<Task> temp = new List<Task>();
                
                temp.add(t);
               
                whoIdsMap.put(t.WhoId, temp);
            }else{
                
                whoIdsMap.get(t.WhoId).add(t);
            }
        }
    }
   
    
    for(Contact con : [Select Id, Name,Title from Contact where Id in :whoIdsMap.keySet()]){
        
        for(Task t :whoIdsMap.get(con.Id)){
            t.Contacts_Title__c = con.Title;
        }
    }
}

 

Trying to write (my first) trigger to pull from a picklist field on the Contact to a duplicate picklist on a Task field.

Contact field info is 

Field Label: SOI

Object Name: Contact

Field Name: SOI

Data Type: Picklist

API Name: SOI__c

 

Task Field Info is

Field Label: Contact's SOI

Object Name: Activity

Field Name: Contacts_SOI

Data Type: Picklist

API Name: Contacts_SOI__c

 

I currently receive the error message:

Error: Compile Error: Incompatible key type Schema.SObjectField for MAP<Id,LIST<Task>> at line 6 column 20
 

 

The code that I am trying to use is:

 

Trigger TaskBefore on Task(before insert, before update){
Map<Id, List<Task>> whoIds = new Map<Id, List<Task>>{};
For (Task t : trigger.new)
If(t.WhoId != null){
List<Task> tasks = whoIds.get(task.WhoId);
If (tasks == null){
tasks = new List<Task>{};
whoIds.put(t.WhoId, tasks);
}
tasks.add(t);
}
For(Contact con : [Select Id, Name,SOI__c from Contact where Id in :whoIds.keySet()])
For(Task t : whoIds.get(con.id))

t.contacts_SOI__c = con.SOI__c;}

 

Please help!