• Vijay Zutshi 2
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 7
    Replies
Hi,

I want to display related contacts of an Account using Visualforce page for which my Apex Page and Apex Class is listed below.

My Apex page is:

<apex:page controller="DisplayRelatedContactAccount" sidebar="false">
    <apex:form >        
        <apex:pageBlock >
            <apex:pageBlockSection > 
                <apex:outputText value="Enter Account Name">                    
                </apex:outputText>                
                <apex:inputText value="{! acctName}"/> 
                <apex:commandButton value="Show Contacts" action="{! showContacts}" reRender="out" status="mystatus"/>
                <apex:actionStatus id="mystatus" startText="Please wait related account contacts are loading....">
                </apex:actionStatus>
                <apex:pageBlockTable value="{! Contacts}" var="ct" rendered="{! acctName !=Null}">                
                    <apex:column headerValue="Name">
                        {! ct.Name}
                    </apex:column>
                    <apex:column headerValue="Phone">
                        {! ct.Phone}
                    </apex:column>
                    <apex:column headerValue="Email">
                        {! ct.Email}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

For this my Apex class is :

public class DisplayRelatedContactAccount {
    public string acctName {get; set;}
    SET<string> accIds = new SET<string>();
    //public List<Contact> Contacts {get; set;}
    List<Account> lstacc = new List<Account>();
    List<contact> lstcon = new List<contact>();
    public List<Contact> showContacts(){
        lstacc = [SELECT Id, Name
                  FROM Account
                  WHERE name =:acctName];
        for(integer i=0; i<lstacc.size(); i++) {
            accIds.add(lstacc[i].id);
        }
        lstcon = [SELECT Id, Name, Phone, Email, AccountId
                  FROM Contact
                  WHERE AccountId IN :accIds];
        return lstcon;        
    }    
    public Pagereference showContacts() {
        return null;
    }
}

I am getting following error msg :

Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList

Please advise what needs to be done to remove the above mentioned error msg.

Thanks
Vijay Zutshi
My Trigger is as follows:- 

trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) { 
                    if(String.isEmpty(newOpp.AccountId)) {
                    newOpp.addError('Account name cannot be blank');
                    return;    
                    }
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);               
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;                 
                    newAccMember.UserId = newOpp.OwnerId;
                    newAccMember.Account = newOpp.Account;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    system.debug('team' + listAccTeamMember);
                    //listShare.add(newAccShare);        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

My test class is as follows:-

@isTest
public class oppAccTeamMember {
    static testMethod Void testOppAccTeamMember() {
        LIST<Opportunity> newOpp = new LIST<Opportunity>();
        LIST<AccountTeamMember> newTeamMember = new LIST<AccountTeamMember>();
        LIST<Account> listAcc = new LIST<ACCOUNT>();
        SET<ID> allIds = new SET<ID>();
        Profile pld= [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        User u= new User();
        u.LastName = 'ttt';
        u.ProfileId = Pld.Id;        
        insert u;                   
        
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        newAcc.OwnerId = u.Id;        
        listAcc.add(newAcc);        
        insert listAcc;        
                
        Opportunity createOpp = new Opportunity();       
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        allIds.add(createOpp.AccountId);
        if(String.isEmpty(createOpp.AccountId)) {
        createOpp.addError('Account name cannot be blank');
        return; 
        }       
       newOpp.add(createOpp);       
       insert newOpp;       
        
        AccountTeamMember createTeam = new AccountTeamMember();
        createTeam.accountId = createOpp.AccountId;
        createTeam.AccountAccessLevel = 'Read';
        createTeam.UserId = createOpp.OwnerId;
        newTeamMember.add(createTeam);        
        insert newTeamMember;       
    }
}

My test class is failing and it is giving me System.DMLException Insert failed

Please provide assistance as I am stuck with for many days and I do not just want to forget about it because by doing that I am not going to learn for the next time.

Thanks
Vijay
My trigger is as follows:-

//Trigger to create the Opportunity with the Probability=20, 
//then the opportunity owner will be automatically added to Account Team 
//of the associated account for that Opportunity.
trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) { 
                    if(String.isEmpty(newOpp.AccountId)) {
                    newOpp.addError('Account name cannot be blank');
                    return;    
                    }
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);               
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;                 
                    newAccMember.UserId = newOpp.OwnerId;
                    newAccMember.Account = newOpp.Account;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    system.debug('team' + listAccTeamMember);
                    //listShare.add(newAccShare);        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

My test Class is as follows:-

@isTest
public class oppAccTeamMember {
    static testMethod Void testOppAccTeamMember() {
        LIST<Opportunity> newOpp = new LIST<Opportunity>();
        LIST<AccountTeamMember> newTeamMember = new LIST<AccountTeamMember>();
        LIST<Account> listAcc = new LIST<ACCOUNT>();
        SET<ID> allIds = new SET<ID>();
        Opportunity createOpp = new Opportunity();
        AccountTeamMember createTeam = new AccountTeamMember();
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        insert newAcc;
        
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        allIds.add(createOpp.AccountId);
        
            if(String.isEmpty(createOpp.AccountId)) {
          createOpp.addError('Account name cannot be blank');
          return; 
            }
        if(!allIds.isEmpty()) {
                       AccountTeamMember testClassAtm = new AccountTeamMember();
                    testClassAtm.AccountAccessLevel = 'Edit';
                    testClassAtm.OpportunityAccessLevel = 'Read';
                    testClassAtm.TeamMemberRole = 'Account Manager';
                    testClassAtm.AccountId = createOpp.AccountId;                 
                    testClassAtm.UserId = createOpp.OwnerId;
                    testClassAtm.Account = createOpp.Account; 
                      newTeamMember.add(testClassAtm);
                    //insert newTeamMember;
        }    
       //listAcc.add(newAcc);
       newOpp.add(createOpp);
       insert newOpp;  
       //insert listAcc; 
    }
}

I am only getting 28% coverage. The test class is not covering the Account Team Member code in the trigger. Kindly help.

Thanks
Vijay Zutshi
Hi, 

When I create the Opportunity with the Probability=20, then the opportunity owner will be automatically added to Account Team of the associated account for that Opportunity. My tigger is as follows:- 

trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) {  
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;
                    newAccMember.UserId = newOpp.OwnerId;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    //listShare.add(newAccShare);
        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

I am getting following error:-

opportunityAcountTeam: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account]: [Account]: Trigger.opportunityAcountTeam: line 27, column 1

Kindly advise as I am not able to solve the problem.

Thanks
Vijay Zutshi
I have a trigger to create Cutomer Record for the Account Record. When this happens the user in Account Manager field will be automatically added to Account Team of that associated Account. The trigger is just creating 1 team member. So when I want to add another one it does not get added to the Team Member. My trigger is as follows:-

trigger customerAccount on Customer__c (after insert) {
    if(Trigger.IsAfter && Trigger.IsInsert) {
        SET<ID> customerId = new SET<ID>();
        SET<ID> accountManager = new SET<ID>();
        LIST<Account> newAccount = new LIST<Account>();
        LIST<AccountTeamMember> teamAccount = new LIST<AccountTeamMember>();
        MAP<ID, Account> mapIdAccount = new MAP<ID, Account>();
        MAP<ID, User> mapIdUser = new MAP<ID, User>();
        for(Customer__c newCustomer : Trigger.new) {
        customerId.add(newCustomer.Account__c);
        accountManager.add(newCustomer.Account_Manager__c);
        }       
        LIST<Account> searchAcc = [SELECT Id, Name
                                   FROM Account
                                   WHERE ID IN :customerId];
        for(Account newAccountSearch :searchAcc) {
         mapIdAccount.put(newAccountSearch.Id, newAccountSearch);   
        }
        LIST<User> searchUser = [SELECT Id, name,AccountId
                                 FROM User
                                 WHERE Id IN :accountManager];
        for(User addUser :searchUser) {
          mapIdUser.put(addUser.Id, addUser);  
        }        
        //for(Account addTeam :searchAcc) {            
        //for(User adduser1 :searchUser) {
        for(Customer__c addCustomerAcc : Trigger.new) {
            if(addCustomerAcc.Account_Manager__c != NULL) {
            accountTeamMember team = new accountTeamMember();
            team.AccountAccessLevel = 'Edit';
            team.AccountId = addCustomerAcc.Account__c;
            team.UserId = addCustomerAcc.Account_Manager__c;
            team.TeamMemberRole = 'Account Manager';    
        //team.Id = mapIdUser.get(adduser1.Id).AccountId;
        //team.UserId = Trigger.new[0].Id;
        //team.AccountId = mapIdUser.get(addteam.Name).Id; 
        teamAccount.add(team);
        } 
        }
        if(teamAccount != NULL && teamAccount.size()>0) {         
        insert teamAccount;   
        //team.UserId = mapIdUser.get(addTeam.OwnerId);
        //team.AccountId = mapIdUser.get(addTeam.Id).Name;         
        }   
    }
}

Please advise where I am going wrong.

Thanks
Vijay Zutshi
My Trigger is as follows:
trigger contactRelationship on Contact (after insert, before delete) {
    LIST<Contact> newContact = new LIST<Contact>();
    LIST<Contact_Relationship__c> createRelationship = new  LIST<Contact_Relationship__c>();
    SET<ID> contactRelId = new SET<ID>();
    if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Contact createContact : Trigger.new) {
                newContact.add(createContact);
                if(createContact.Contact_Relationship__c == TRUE) {
                    Contact_Relationship__c newContactRelationship = new                      Contact_Relationship__c();
                    newContactRelationship.Contact_Relationship__c =                            createContact.Id;
                    newContactRelationship.Name =                                                           createContact.LastName;
                    createRelationship.add(newContactRelationship);
                    contactRelId.add(newContactRelationship.Id);                                            
                }
            }
            insert createRelationship;
        }
    }
    LIST<Contact> deleteContact = new LIST<Contact>();
    SET<ID> contactDeleteId = new SET<ID>();
    SET<ID> allContactId = new SET<ID>();    
    if(Trigger.IsBefore && Trigger.IsDelete) {
        //if(Trigger.IsDelete) {
            for(Contact allContact : Trigger.old) {
                deleteContact.add(allContact); 
                contactDeleteId.add(allContact.Id);
                 //Contact_Relationship__c delContRel = new Contact_Relationship__c();   
            }
            LIST<Contact> listContactDelete = [SELECT Id, LastName
                                               FROM Contact
                                               WHERE Id IN :contactDeleteId];
            system.debug('contact' + listContactDelete);
            //if(listContactDelete != NULL && listContactDelete.size()>0) {
              //delete listContactDelete;  
            //}
            
            LIST<Contact_Relationship__c> deleteContactRelationship = [SELECT Id, Name
                                                                       FROM Contact_Relationship__c 
                                                                       WHERE Contact_Relationship__c IN :contactDeleteId];
            system.debug('relation' + deleteContactRelationship);
            for(Contact_Relationship__c delRel :deleteContactRelationship) {
            update deleteContactRelationship;
            }            
            //if(deleteContactRelationship != NULL && deleteContactRelationship.size()>0) {
                //delete deleteContactRelationship;
            //}             
        //}
    }
}


I have Contact object under which there is a Contact Relationship custom object. When I add a contact it gets also added to the custom object. This part is working ok. Now I add another record to the custom object which is under the same Contact. So there are multiple records in the custom object. So when I delete a Contact then it should also delete all records in the custom object which is related to the same contact. This is not happening and I would appreciate help on this as I am new to this.
Thanks
Vijay Zutshi
The trigger senario is as follows:-

Create “Top X Designation” custom object which is the related list to Opportunity (Look up Relationship). The Top X Designation object, has fields Type (Picklist), Document Attached (Checkbox). The opportunity object has  one field Handoff Attached with pick list type with values are Yes, No. The logic that the trigger has to follow is 
//If Type (Top X Designation) = "Contract Flow Down/Handoff"
// and "Document Attached” = True then "Handoff Attached" = True, otherwise false.

My trigger is as follows. After insert is working fine but after update trigger is no performing as required
trigger topDesignation on Top_X_Designation__c (After insert, After Update, after delete) {
    LIST<Top_X_Designation__c> newTopDesignation = new LIST<Top_X_Designation__c>();
    LIST<Opportunity> oppList = new LIST<Opportunity>();
    LIST<Opportunity> totalOpp = new LIST<Opportunity>();
    SET<ID> newId = new SET<ID>();
       if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Top_X_Designation__c top : Trigger.new) {
                newTopDesignation.add(top);
                newId.add(top.OwnerId); 
                system.debug('top' + top.OwnerId);
                system.debug(top.Id);
                Opportunity newOpp = new Opportunity();
                newOpp.OwnerId = top.Id;
                system.debug('opportunity' + newOpp.OwnerId);
                if(top.Type__c == 'Contract Flow Down/Handoff' && top.Document_Attached__c == True) {
                newOpp.Handoff_Attached__c = 'Yes';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);
                } 
                else {
                newOpp.Handoff_Attached__c = 'No';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);    
                }
              }
        }
        //system.debug('opp' + totalOpp);
        if(Trigger.IsUpdate) {
            LIST<Top_X_Designation__c> updateTopDesg = new LIST<Top_X_Designation__c>();
            LIST<Opportunity> upOpp = new LIST<Opportunity>();
            SET<ID> topId = new SET<ID>();            
            for(Top_X_Designation__c updateDesg : Trigger.new) {
                Id oldId = Trigger.oldMap.get(updateDesg.Id).OwnerId;
                Id newId = updateDesg.Id;
                system.debug('old' + oldId);
                system.debug('new' + newId);
                if(oldId != newId) {
                    topId.add(updateDesg.Id);
                    updateTopDesg.add(updateDesg);
                }
            }
            system.debug('set' +topId);
            LIST<Opportunity> listOpp = [SELECT OwnerId, Name, Handoff_Attached__c    FROM Opportunity
                                         WHERE Id IN:topId];
            system.debug('opp' + listOpp);
            for(Top_X_Designation__c dc : Trigger.new) {
                for(Opportunity oppUpdate :listOpp) {
                    if(dc.Id  == oppUpdate.Id) {
                        oppUpdate.Handoff_Attached__c = 'Yes';
                        upOpp.add(oppUpdate);
                    }
                        else 
                        {
                            oppUpdate.Handoff_Attached__c = 'No';
                            upOpp.add(oppUpdate);
                        }                    
                    }
                }
            if(upOpp != NULL && upOpp.size()>0) {
                update upOpp;
            }               
        }
 }
}

I am new to this field and would appreciate help to solve my problem.
Thanks
Vijay Zutshi
My Trigger is as follows:- 

trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) { 
                    if(String.isEmpty(newOpp.AccountId)) {
                    newOpp.addError('Account name cannot be blank');
                    return;    
                    }
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);               
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;                 
                    newAccMember.UserId = newOpp.OwnerId;
                    newAccMember.Account = newOpp.Account;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    system.debug('team' + listAccTeamMember);
                    //listShare.add(newAccShare);        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

My test class is as follows:-

@isTest
public class oppAccTeamMember {
    static testMethod Void testOppAccTeamMember() {
        LIST<Opportunity> newOpp = new LIST<Opportunity>();
        LIST<AccountTeamMember> newTeamMember = new LIST<AccountTeamMember>();
        LIST<Account> listAcc = new LIST<ACCOUNT>();
        SET<ID> allIds = new SET<ID>();
        Profile pld= [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        User u= new User();
        u.LastName = 'ttt';
        u.ProfileId = Pld.Id;        
        insert u;                   
        
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        newAcc.OwnerId = u.Id;        
        listAcc.add(newAcc);        
        insert listAcc;        
                
        Opportunity createOpp = new Opportunity();       
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        allIds.add(createOpp.AccountId);
        if(String.isEmpty(createOpp.AccountId)) {
        createOpp.addError('Account name cannot be blank');
        return; 
        }       
       newOpp.add(createOpp);       
       insert newOpp;       
        
        AccountTeamMember createTeam = new AccountTeamMember();
        createTeam.accountId = createOpp.AccountId;
        createTeam.AccountAccessLevel = 'Read';
        createTeam.UserId = createOpp.OwnerId;
        newTeamMember.add(createTeam);        
        insert newTeamMember;       
    }
}

My test class is failing and it is giving me System.DMLException Insert failed

Please provide assistance as I am stuck with for many days and I do not just want to forget about it because by doing that I am not going to learn for the next time.

Thanks
Vijay
My trigger is as follows:-

//Trigger to create the Opportunity with the Probability=20, 
//then the opportunity owner will be automatically added to Account Team 
//of the associated account for that Opportunity.
trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) { 
                    if(String.isEmpty(newOpp.AccountId)) {
                    newOpp.addError('Account name cannot be blank');
                    return;    
                    }
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);               
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;                 
                    newAccMember.UserId = newOpp.OwnerId;
                    newAccMember.Account = newOpp.Account;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    system.debug('team' + listAccTeamMember);
                    //listShare.add(newAccShare);        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

My test Class is as follows:-

@isTest
public class oppAccTeamMember {
    static testMethod Void testOppAccTeamMember() {
        LIST<Opportunity> newOpp = new LIST<Opportunity>();
        LIST<AccountTeamMember> newTeamMember = new LIST<AccountTeamMember>();
        LIST<Account> listAcc = new LIST<ACCOUNT>();
        SET<ID> allIds = new SET<ID>();
        Opportunity createOpp = new Opportunity();
        AccountTeamMember createTeam = new AccountTeamMember();
        Account newAcc = new Account();
        newAcc.Name = 'Starting';
        insert newAcc;
        
        createOpp.Name = 'First';
        createOpp.CloseDate = Date.today();
        createOpp.StageName = 'Prospecting';
        createOpp.AccountId = newAcc.Id;
        allIds.add(createOpp.AccountId);
        
            if(String.isEmpty(createOpp.AccountId)) {
          createOpp.addError('Account name cannot be blank');
          return; 
            }
        if(!allIds.isEmpty()) {
                       AccountTeamMember testClassAtm = new AccountTeamMember();
                    testClassAtm.AccountAccessLevel = 'Edit';
                    testClassAtm.OpportunityAccessLevel = 'Read';
                    testClassAtm.TeamMemberRole = 'Account Manager';
                    testClassAtm.AccountId = createOpp.AccountId;                 
                    testClassAtm.UserId = createOpp.OwnerId;
                    testClassAtm.Account = createOpp.Account; 
                      newTeamMember.add(testClassAtm);
                    //insert newTeamMember;
        }    
       //listAcc.add(newAcc);
       newOpp.add(createOpp);
       insert newOpp;  
       //insert listAcc; 
    }
}

I am only getting 28% coverage. The test class is not covering the Account Team Member code in the trigger. Kindly help.

Thanks
Vijay Zutshi
My Trigger is as follows:
trigger contactRelationship on Contact (after insert, before delete) {
    LIST<Contact> newContact = new LIST<Contact>();
    LIST<Contact_Relationship__c> createRelationship = new  LIST<Contact_Relationship__c>();
    SET<ID> contactRelId = new SET<ID>();
    if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Contact createContact : Trigger.new) {
                newContact.add(createContact);
                if(createContact.Contact_Relationship__c == TRUE) {
                    Contact_Relationship__c newContactRelationship = new                      Contact_Relationship__c();
                    newContactRelationship.Contact_Relationship__c =                            createContact.Id;
                    newContactRelationship.Name =                                                           createContact.LastName;
                    createRelationship.add(newContactRelationship);
                    contactRelId.add(newContactRelationship.Id);                                            
                }
            }
            insert createRelationship;
        }
    }
    LIST<Contact> deleteContact = new LIST<Contact>();
    SET<ID> contactDeleteId = new SET<ID>();
    SET<ID> allContactId = new SET<ID>();    
    if(Trigger.IsBefore && Trigger.IsDelete) {
        //if(Trigger.IsDelete) {
            for(Contact allContact : Trigger.old) {
                deleteContact.add(allContact); 
                contactDeleteId.add(allContact.Id);
                 //Contact_Relationship__c delContRel = new Contact_Relationship__c();   
            }
            LIST<Contact> listContactDelete = [SELECT Id, LastName
                                               FROM Contact
                                               WHERE Id IN :contactDeleteId];
            system.debug('contact' + listContactDelete);
            //if(listContactDelete != NULL && listContactDelete.size()>0) {
              //delete listContactDelete;  
            //}
            
            LIST<Contact_Relationship__c> deleteContactRelationship = [SELECT Id, Name
                                                                       FROM Contact_Relationship__c 
                                                                       WHERE Contact_Relationship__c IN :contactDeleteId];
            system.debug('relation' + deleteContactRelationship);
            for(Contact_Relationship__c delRel :deleteContactRelationship) {
            update deleteContactRelationship;
            }            
            //if(deleteContactRelationship != NULL && deleteContactRelationship.size()>0) {
                //delete deleteContactRelationship;
            //}             
        //}
    }
}


I have Contact object under which there is a Contact Relationship custom object. When I add a contact it gets also added to the custom object. This part is working ok. Now I add another record to the custom object which is under the same Contact. So there are multiple records in the custom object. So when I delete a Contact then it should also delete all records in the custom object which is related to the same contact. This is not happening and I would appreciate help on this as I am new to this.
Thanks
Vijay Zutshi
The trigger senario is as follows:-

Create “Top X Designation” custom object which is the related list to Opportunity (Look up Relationship). The Top X Designation object, has fields Type (Picklist), Document Attached (Checkbox). The opportunity object has  one field Handoff Attached with pick list type with values are Yes, No. The logic that the trigger has to follow is 
//If Type (Top X Designation) = "Contract Flow Down/Handoff"
// and "Document Attached” = True then "Handoff Attached" = True, otherwise false.

My trigger is as follows. After insert is working fine but after update trigger is no performing as required
trigger topDesignation on Top_X_Designation__c (After insert, After Update, after delete) {
    LIST<Top_X_Designation__c> newTopDesignation = new LIST<Top_X_Designation__c>();
    LIST<Opportunity> oppList = new LIST<Opportunity>();
    LIST<Opportunity> totalOpp = new LIST<Opportunity>();
    SET<ID> newId = new SET<ID>();
       if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Top_X_Designation__c top : Trigger.new) {
                newTopDesignation.add(top);
                newId.add(top.OwnerId); 
                system.debug('top' + top.OwnerId);
                system.debug(top.Id);
                Opportunity newOpp = new Opportunity();
                newOpp.OwnerId = top.Id;
                system.debug('opportunity' + newOpp.OwnerId);
                if(top.Type__c == 'Contract Flow Down/Handoff' && top.Document_Attached__c == True) {
                newOpp.Handoff_Attached__c = 'Yes';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);
                } 
                else {
                newOpp.Handoff_Attached__c = 'No';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);    
                }
              }
        }
        //system.debug('opp' + totalOpp);
        if(Trigger.IsUpdate) {
            LIST<Top_X_Designation__c> updateTopDesg = new LIST<Top_X_Designation__c>();
            LIST<Opportunity> upOpp = new LIST<Opportunity>();
            SET<ID> topId = new SET<ID>();            
            for(Top_X_Designation__c updateDesg : Trigger.new) {
                Id oldId = Trigger.oldMap.get(updateDesg.Id).OwnerId;
                Id newId = updateDesg.Id;
                system.debug('old' + oldId);
                system.debug('new' + newId);
                if(oldId != newId) {
                    topId.add(updateDesg.Id);
                    updateTopDesg.add(updateDesg);
                }
            }
            system.debug('set' +topId);
            LIST<Opportunity> listOpp = [SELECT OwnerId, Name, Handoff_Attached__c    FROM Opportunity
                                         WHERE Id IN:topId];
            system.debug('opp' + listOpp);
            for(Top_X_Designation__c dc : Trigger.new) {
                for(Opportunity oppUpdate :listOpp) {
                    if(dc.Id  == oppUpdate.Id) {
                        oppUpdate.Handoff_Attached__c = 'Yes';
                        upOpp.add(oppUpdate);
                    }
                        else 
                        {
                            oppUpdate.Handoff_Attached__c = 'No';
                            upOpp.add(oppUpdate);
                        }                    
                    }
                }
            if(upOpp != NULL && upOpp.size()>0) {
                update upOpp;
            }               
        }
 }
}

I am new to this field and would appreciate help to solve my problem.
Thanks
Vijay Zutshi