• jrosser1.39050288155405E12
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 16
    Replies
I have a trigger and a test class to upsert account team and account team share when a new records is added/edited on a custom object.

The trigger works, the test class failes on the accountshare piece, if I comment that out it works fine for the account team.  But the user gets added with read only.

Without changing system wide sharing rules is there a way to bypass this with a trigger?

Thank you,
I am writing a Test class for a trigger and when I run it I get the following error, it is referencing a line in the trigger, but I am not sure how to address this.

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTeamADD: execution of BeforeInsert

caused by: System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Trigger.AccountTeamADD: line 76, column 1: []
I am trying to figure out the right approach for writing a class for this trigger.  These objects are not really related.

TSG__c is a customer Object (Trigger object)
User to get the User.ID
Account to get the Account.ID
AccountTeamMember insert record
AccountShare insert record.

Please see trigger.

trigger AccountTeamADD on TSG__c (before update,before insert) {

//list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
    
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
// Hold User Info
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();

//Loop Through all records in the Trigger.new Collection

For(TSG__c T: Trigger.new)
{
    ss.add(T.email_address__c);
    aa.add(T.P21_CompanyCustomer_ID__c);
}


//Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];

    System.debug('@@@@--'+TheUser.size());
   
//Check to Make sure a record was retuned  
if (!TheUser.isEmpty()) {

     //Loop Through the Trigger (New Records)

     For (TSG__c T: Trigger.new) {
    
             For (Account acct: TheAcct) {
            
             For (User email: TheUser) {
        
             if (t.email_address__c == email.email && T.P21_CompanyCustomer_ID__c ==acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
            
                 //Insert into Account Team Here;
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.Id;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.Id;
                    acctMembers.add(ca);
                   
                //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                

                
                             }

                        }

                    }

                }
                     upsert acctMembers;
                     upsert acctSharingRules;              
    
              
            }
        }
I created a trigger on Custom Object TSG__c to insert a record into the account team when the record is updated.  The trigger works fine, now I am just trying to write the class.

I am inserting records for 200 Accounts, 200 Users and 200 AccountTeamMemebers and referencing the ID's from Accounts,Users as the Account Team is being created, but I do not know how to tie it all back to gether.

Any Ideas?

@isTest
private class addAccountTeam {

    @isTest
    static void test_checkTSGAccounTeamInsert() {
   

   
    // Create the Acounts
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 200; i++) {
            Account newAcc = new Account(Name='Test' + i,P21_CompanyCustomer_ID__c = '1-10009' + i );
            accList.add(newAcc);
        }
        insert accList;
       
   // create the TSGs
        List<TSG__c> tsgList = new List<TSG__c>();
        for (Integer i = 0; i < 200; i++) {
            TSG__c newTSG = new TSG__c(Name='Test' + i,email_address__c ='Test' + i + '@Tri-ed.com',
                                P21_CompanyCustomer_ID__c = accList[i].P21_CompanyCustomer_ID__c);
            tsgList.add(newTSG);
        }
        insert tsgList; 
       
        // create the Users
        List<User> userList = new List<User>();
        for (Integer i = 0; i< 200; i++) {
            // observe the same name for Opp and TSG
            USer newUser = new User(lastname = 'Test',Alias = 'Test',TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'eu_ES',EmailEncodingKey = 'ISO-8859-1',
            ProfileId = '00ei0000000TYak',LanguageLocaleKey = 'en_US',
            userName='Test' + i + '@Tri-ed.com',email='Test' + i + '@Tri-ed.com');
            userList.add(newUser);
        }
        insert userList;
       
       
        //Create the AccountTeam
        List<AccountTeamMember> teamList = new List<AccountTeamMember>();
        For (Integer i = 0; i< 200; i++) {
        AccountTeamMember newTeam = new AccountTeamMember(AccountId = accList[i].ID,TeamMemberRole = 'Technical Sales',UserId = userList[i].ID);
        teamList.add(newTeam);
        }
        insert teamList;
       
        //Create the AccountTeamShare
        List<AccountShare> shareList = new List<AccountShare>();
        For (Integer i = 0; i< 200; i++) {
        AccountShare newShare = new AccountShare(AccountId = accList[i].ID,UserOrGroupId = userList[i].ID,CaseAccessLevel = 'Edit',AccountAccessLevel = 'Edit',OpportunityAccessLevel = 'Edit');
        shareList.add(newShare);
        }
        insert shareList;

    
         //Check to Make Sure records Match Up.
     teamList = [Select AccountID,UserId,TeamMemberRole from AccountTeamMember order by ID];
     accList = [Select ID,P21_CompanyCustomer_ID__c from Account order by ID];
    
     For (AccountTeamMember team: teamList) {
       
                 For (Account acct: accList) {
       
            System.assertEquals(team.AccountID,acct.ID);
       
        }
         }
}
}
I have a trigger on opportunites that will add a field from a custom object (TSG__c)  if a match is found on .P21_Customer_ID.  The issue is that when multiple records are being updated through an upsert (Informatica Cloud) all of those opportunites are getting updated with the same field value.

Below is my trigger, please let me know where I went wrong.

Thanks again.


TRIGGER populate_TSG on Opportunity (before insert,before update) {

Set<string> ss = New Set<string>();
List<TSG__c> TheTSG=new LIST<TSG__c>();  
   
//Loop through all records in the Trigger.new collection
FOR(Opportunity O: Trigger.new)
{
    ss.Add(O.P21_Customer_ID__c );
}

TheTSG = [SELECT ID from TSG__c WHERE P21_CompanyCustomer_ID__c IN: ss];
   
    System.debug('@@@@--'+TheTSG.size());

    IF (TheTSG.size() >0)
    {
        For (Opportunity O: Trigger.new)
        {
            For(TSG__c tsg:TheTSG)
            {
               
           
            O.p21_account_number__c = tsg.ID;
            System.debug('@@@@--'+tsg.ID);
           
            }
        }
    }
}
We are using the web to case feature and are able to populate P21_Customer_ID (custom field, this is the customer number in our ERP system). However we are not able to populate the account name on the case which assings the case to the account in sales force.  This is my first time wrtting a trigger in Apex.  This is what I came up with so far.

Basically I want to take the P21_Customer_ID entered on the case, look up the account name from the account table and add it to the case.

trigger populate_customer_name on Case (after insert) {
Case TheCase = trigger.new[0];
if(TheCase.P21_Customer_ID != null) {
Account theAccount = [Select Name from Account
where Account.P21_CompanyCustomer_ID__c = TheCase.P21_Customer_ID];
TheCase.AccountId = theAccount.Name;
update TheCase;
}
}

Thank you,
I have a trigger and a test class to upsert account team and account team share when a new records is added/edited on a custom object.

The trigger works, the test class failes on the accountshare piece, if I comment that out it works fine for the account team.  But the user gets added with read only.

Without changing system wide sharing rules is there a way to bypass this with a trigger?

Thank you,
I am writing a Test class for a trigger and when I run it I get the following error, it is referencing a line in the trigger, but I am not sure how to address this.

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTeamADD: execution of BeforeInsert

caused by: System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Trigger.AccountTeamADD: line 76, column 1: []
I am trying to figure out the right approach for writing a class for this trigger.  These objects are not really related.

TSG__c is a customer Object (Trigger object)
User to get the User.ID
Account to get the Account.ID
AccountTeamMember insert record
AccountShare insert record.

Please see trigger.

trigger AccountTeamADD on TSG__c (before update,before insert) {

//list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
    
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
// Hold User Info
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();

//Loop Through all records in the Trigger.new Collection

For(TSG__c T: Trigger.new)
{
    ss.add(T.email_address__c);
    aa.add(T.P21_CompanyCustomer_ID__c);
}


//Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];

    System.debug('@@@@--'+TheUser.size());
   
//Check to Make sure a record was retuned  
if (!TheUser.isEmpty()) {

     //Loop Through the Trigger (New Records)

     For (TSG__c T: Trigger.new) {
    
             For (Account acct: TheAcct) {
            
             For (User email: TheUser) {
        
             if (t.email_address__c == email.email && T.P21_CompanyCustomer_ID__c ==acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
            
                 //Insert into Account Team Here;
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.Id;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.Id;
                    acctMembers.add(ca);
                   
                //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                

                
                             }

                        }

                    }

                }
                     upsert acctMembers;
                     upsert acctSharingRules;              
    
              
            }
        }
I created a trigger on Custom Object TSG__c to insert a record into the account team when the record is updated.  The trigger works fine, now I am just trying to write the class.

I am inserting records for 200 Accounts, 200 Users and 200 AccountTeamMemebers and referencing the ID's from Accounts,Users as the Account Team is being created, but I do not know how to tie it all back to gether.

Any Ideas?

@isTest
private class addAccountTeam {

    @isTest
    static void test_checkTSGAccounTeamInsert() {
   

   
    // Create the Acounts
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 200; i++) {
            Account newAcc = new Account(Name='Test' + i,P21_CompanyCustomer_ID__c = '1-10009' + i );
            accList.add(newAcc);
        }
        insert accList;
       
   // create the TSGs
        List<TSG__c> tsgList = new List<TSG__c>();
        for (Integer i = 0; i < 200; i++) {
            TSG__c newTSG = new TSG__c(Name='Test' + i,email_address__c ='Test' + i + '@Tri-ed.com',
                                P21_CompanyCustomer_ID__c = accList[i].P21_CompanyCustomer_ID__c);
            tsgList.add(newTSG);
        }
        insert tsgList; 
       
        // create the Users
        List<User> userList = new List<User>();
        for (Integer i = 0; i< 200; i++) {
            // observe the same name for Opp and TSG
            USer newUser = new User(lastname = 'Test',Alias = 'Test',TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'eu_ES',EmailEncodingKey = 'ISO-8859-1',
            ProfileId = '00ei0000000TYak',LanguageLocaleKey = 'en_US',
            userName='Test' + i + '@Tri-ed.com',email='Test' + i + '@Tri-ed.com');
            userList.add(newUser);
        }
        insert userList;
       
       
        //Create the AccountTeam
        List<AccountTeamMember> teamList = new List<AccountTeamMember>();
        For (Integer i = 0; i< 200; i++) {
        AccountTeamMember newTeam = new AccountTeamMember(AccountId = accList[i].ID,TeamMemberRole = 'Technical Sales',UserId = userList[i].ID);
        teamList.add(newTeam);
        }
        insert teamList;
       
        //Create the AccountTeamShare
        List<AccountShare> shareList = new List<AccountShare>();
        For (Integer i = 0; i< 200; i++) {
        AccountShare newShare = new AccountShare(AccountId = accList[i].ID,UserOrGroupId = userList[i].ID,CaseAccessLevel = 'Edit',AccountAccessLevel = 'Edit',OpportunityAccessLevel = 'Edit');
        shareList.add(newShare);
        }
        insert shareList;

    
         //Check to Make Sure records Match Up.
     teamList = [Select AccountID,UserId,TeamMemberRole from AccountTeamMember order by ID];
     accList = [Select ID,P21_CompanyCustomer_ID__c from Account order by ID];
    
     For (AccountTeamMember team: teamList) {
       
                 For (Account acct: accList) {
       
            System.assertEquals(team.AccountID,acct.ID);
       
        }
         }
}
}
I have a trigger on opportunites that will add a field from a custom object (TSG__c)  if a match is found on .P21_Customer_ID.  The issue is that when multiple records are being updated through an upsert (Informatica Cloud) all of those opportunites are getting updated with the same field value.

Below is my trigger, please let me know where I went wrong.

Thanks again.


TRIGGER populate_TSG on Opportunity (before insert,before update) {

Set<string> ss = New Set<string>();
List<TSG__c> TheTSG=new LIST<TSG__c>();  
   
//Loop through all records in the Trigger.new collection
FOR(Opportunity O: Trigger.new)
{
    ss.Add(O.P21_Customer_ID__c );
}

TheTSG = [SELECT ID from TSG__c WHERE P21_CompanyCustomer_ID__c IN: ss];
   
    System.debug('@@@@--'+TheTSG.size());

    IF (TheTSG.size() >0)
    {
        For (Opportunity O: Trigger.new)
        {
            For(TSG__c tsg:TheTSG)
            {
               
           
            O.p21_account_number__c = tsg.ID;
            System.debug('@@@@--'+tsg.ID);
           
            }
        }
    }
}
We are using the web to case feature and are able to populate P21_Customer_ID (custom field, this is the customer number in our ERP system). However we are not able to populate the account name on the case which assings the case to the account in sales force.  This is my first time wrtting a trigger in Apex.  This is what I came up with so far.

Basically I want to take the P21_Customer_ID entered on the case, look up the account name from the account table and add it to the case.

trigger populate_customer_name on Case (after insert) {
Case TheCase = trigger.new[0];
if(TheCase.P21_Customer_ID != null) {
Account theAccount = [Select Name from Account
where Account.P21_CompanyCustomer_ID__c = TheCase.P21_Customer_ID];
TheCase.AccountId = theAccount.Name;
update TheCase;
}
}

Thank you,