• lp oklll
  • NEWBIE
  • 20 Points
  • Member since 2014

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

I have 3 account types A B C

When on case account A is chosen B and C populate automatically. The trigger seems to work for that.

The problem is arising when I am trying to populate a contact related to account A

I simply do not know how to traverse the relationship.

This is my code

Trigger PopulateResellerCase on Case (before insert,before update) {   for (Case NewCase : Trigger.new)      If (NewCase.Accountid != null)    {      Account AccountReseller = [SELECT Id,Reseller__c,APA__c,(SELECT id,Contact.FirstName FROM Account.Contacts)FROM Account                              WHERE Id = :NewCase.Accountid                             ];                                  NewCase.APA__c = AccountReseller.APA__c;      
 NewCase.Reseller__c = AccountReseller.Reseller__c;                             
   }   }

I am administrator on the path of learning code. I have tried the following options below but always get an error.
NewCase.contact = AccountReseller.contact.id;
NewCase.contact = AccountReseller.account.contactid;

But nothing seems to work.






 
Hi,

I am getting the error message System.QueryException: List has no rows for assignment to SObject
MY  TRIGGER is as follows:

trigger contactRecOwnerUpdate on Contact (before insert)
{
    // This trigger sets the owner of the contact record to be the same as the
    // owner of the account record
    //
    // Version 1.0    
    
    Map<String, Contact> contactMap = new Map<String, Contact>();
    
    for (Contact contact : Trigger.New) 
    {
        if(Trigger.isBefore)
        {
            if (System.Trigger.isInsert)
            {
                // Find the OwnerID of the account that the contact is going to be associated to
                Account contactAccount = [select OwnerId from Account where Id=:contact.AccountId];

                // Make the contact owner the same as the account
                contact.OwnerId = contactAccount.OwnerId;

                // Insert the OwnerId into the contact record
                contactMap.put(contact.OwnerId,contact);
            }
        }

    }
}


And MY Class  is below:

@isTest
private class TestApexTriggers
{
    static testMethod void apexTriggersUnitTest()
    {
        //Set up user
        Profile supportUser = [SELECT Id FROM Profile WHERE Name='U K Support User'];
        Profile supportUserTL = [SELECT Id FROM Profile WHERE Name='SENIOR MANAGER'];
        User nordicTL = [SELECT Id FROM User WHERE ProfileId=:supportUserTL.Id and Alias ='ball'];
        
        // The alias will change when the correct users are created
        User nordicAgent = [SELECT Id FROM User WHERE ProfileId=:supportUser.Id and Alias ='rbows'];

        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);

        // Get a list of all active record types within the system associated to Contacts
        List<RecordType> conRTypes = [Select Name, Id From RecordType where sObjectType='Contact'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> contactRecordTypes = new Map<String,String>{};
        for(RecordType conRT: conRTypes)
            contactRecordTypes.put(conRT.Name,conRT.Id);

        //Run As nordicTL
        System.RunAs(nordicTL)
        {
            // Test Reseller create
            System.debug('****** START Reseller Test *******');
            System.debug('Expected User: System Administrator');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account resellerAcc = new Account();
            
            resellerAcc.Name = 'Unit Test Reseller1';
            resellerAcc.RecordTypeId = accountRecordTypes.get('Reseller');
            resellerAcc.Reseller_s_Hub_PIN__c = '9999';
            resellerAcc.ShippingStreet = 'Test Street1';
            resellerAcc.ShippingCity = 'Test City1';
            resellerAcc.ShippingState = 'Test State1';
            resellerAcc.ShippingCountry = 'Denmark';
            resellerAcc.ShippingPostalCode = '4700';

            System.debug('******* Trying insert resellerAcc *******');
            insert resellerAcc;
            System.debug('******* Inserted resellerAcc *******');
            
            System.debug('****** END Reseller Test *******');
        }
        
        //Run As nordicTL
        System.RunAs(nordicAgent)
        {
            System.debug('****** START Home Test *******');
            
            System.debug('****** Creating a list of resellers in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> resellersList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Reseller')];

            System.debug('****** Creating a map of name, id for the retrieved resellers *******');
            // Create a map between the Reseller Name and Id for for easy retrieval
            Map<String,String> resellers = new Map<String,String>{};
            for(Account resAcc: resellersList)
                resellers.put(resAcc.Name,resAcc.Id);            

            // Test Home create
            System.debug('Expected User: Support User');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account homeAcc = new Account();
            
            homeAcc.Name = 'Unit Test Home1';
            homeAcc.RecordTypeId = accountRecordTypes.get('Home');
            homeAcc.Reseller__c = resellers.get('Unit Test Reseller1');
            homeAcc.ShippingStreet = 'Test Street1';
            homeAcc.ShippingCity = 'Test City1';
            homeAcc.ShippingState = 'Test State1';
            homeAcc.ShippingCountry = 'Denmark';
            homeAcc.ShippingPostalCode = '4700';
        
            System.debug('****** Trying insert homeAcc *******');
            insert homeAcc;
            System.debug('****** Inserted homeAcc *******');
            
            System.debug('****** END Home Test *******');

            // Test Contact create
            System.debug('****** START Contact Test *******');
            System.debug('****** Creating a list of Home accounts in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> homesList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Home')];

            System.debug('****** Creating a map of name, id for the retrieved homes *******');
            // Create a map between the Home Account Name and Id for for easy retrieval
            Map<String,String> homes = new Map<String,String>{};
            for(Account homAcc: homesList)
                homes.put(homAcc.Name,homAcc.Id);            

            Contact homeCon = new Contact();
            
            homeCon.AccountId = homes.get('Unit Test Home1');
            homeCon.RecordTypeId = contactRecordTypes.get('Consumer');
            homeCon.Salutation = 'Mr1';
            homeCon.FirstName = 'Apex1';
            homeCon.LastName = 'Unit-Test1';

            System.debug('****** Trying insert homeCon *******');
            insert homeCon;
            System.debug('****** Inserted homeCon *******');
            
            System.debug('****** END Contact Test *******');            
        }
    }
}


 
This is my trigger but I am getting an error Trigger has hit a max query size:50001

The following trigger is trying to populate a lookup field APA on child account. This value is based on the value in another field call APA_Name__c which is populated as a text field on child account. I have 3 accounts of APA . . Please can someone help on this.

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

  // Get a list of all active record types within the system associated to Accounts
        List<Account> APAList = [Select Name, Id From Account];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> NewAPA = new Map<String,String>{};
                  
            for(Account APAAcc: APAList)
                NewAPA.put(APAAcc.Name,APAAcc.Id); 

  for (Account NewAccount : Trigger.new)
  {
  If(NewAccount.APA_Name__c == 'Global Heat Source')
      {
     
      NewAccount.APA__c = NewAPA.get('Global Heat Source');
       
       } 
  else If(NewAccount.APA_Name__c == 'Forrest')
      {
     
      NewAccount.APA__c = NewAPA.get('Forrest');
      }
  else If(NewAccount.APA_Name__c == 'Keepmoat')
      {
     
      NewAccount.APA__c = NewAPA.get('Keepmoat');
      }
   }
   }
Hi,

I have 3 account types A B C

When on case account A is chosen B and C populate automatically. The trigger seems to work for that.

The problem is arising when I am trying to populate a contact related to account A

I simply do not know how to traverse the relationship.

This is my code

Trigger PopulateResellerCase on Case (before insert,before update) {   for (Case NewCase : Trigger.new)      If (NewCase.Accountid != null)    {      Account AccountReseller = [SELECT Id,Reseller__c,APA__c,(SELECT id,Contact.FirstName FROM Account.Contacts)FROM Account                              WHERE Id = :NewCase.Accountid                             ];                                  NewCase.APA__c = AccountReseller.APA__c;      
 NewCase.Reseller__c = AccountReseller.Reseller__c;                             
   }   }

I am administrator on the path of learning code. I have tried the following options below but always get an error.
NewCase.contact = AccountReseller.contact.id;
NewCase.contact = AccountReseller.account.contactid;

But nothing seems to work.






 
Hi,

I am getting the error message System.QueryException: List has no rows for assignment to SObject
MY  TRIGGER is as follows:

trigger contactRecOwnerUpdate on Contact (before insert)
{
    // This trigger sets the owner of the contact record to be the same as the
    // owner of the account record
    //
    // Version 1.0    
    
    Map<String, Contact> contactMap = new Map<String, Contact>();
    
    for (Contact contact : Trigger.New) 
    {
        if(Trigger.isBefore)
        {
            if (System.Trigger.isInsert)
            {
                // Find the OwnerID of the account that the contact is going to be associated to
                Account contactAccount = [select OwnerId from Account where Id=:contact.AccountId];

                // Make the contact owner the same as the account
                contact.OwnerId = contactAccount.OwnerId;

                // Insert the OwnerId into the contact record
                contactMap.put(contact.OwnerId,contact);
            }
        }

    }
}


And MY Class  is below:

@isTest
private class TestApexTriggers
{
    static testMethod void apexTriggersUnitTest()
    {
        //Set up user
        Profile supportUser = [SELECT Id FROM Profile WHERE Name='U K Support User'];
        Profile supportUserTL = [SELECT Id FROM Profile WHERE Name='SENIOR MANAGER'];
        User nordicTL = [SELECT Id FROM User WHERE ProfileId=:supportUserTL.Id and Alias ='ball'];
        
        // The alias will change when the correct users are created
        User nordicAgent = [SELECT Id FROM User WHERE ProfileId=:supportUser.Id and Alias ='rbows'];

        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);

        // Get a list of all active record types within the system associated to Contacts
        List<RecordType> conRTypes = [Select Name, Id From RecordType where sObjectType='Contact'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> contactRecordTypes = new Map<String,String>{};
        for(RecordType conRT: conRTypes)
            contactRecordTypes.put(conRT.Name,conRT.Id);

        //Run As nordicTL
        System.RunAs(nordicTL)
        {
            // Test Reseller create
            System.debug('****** START Reseller Test *******');
            System.debug('Expected User: System Administrator');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account resellerAcc = new Account();
            
            resellerAcc.Name = 'Unit Test Reseller1';
            resellerAcc.RecordTypeId = accountRecordTypes.get('Reseller');
            resellerAcc.Reseller_s_Hub_PIN__c = '9999';
            resellerAcc.ShippingStreet = 'Test Street1';
            resellerAcc.ShippingCity = 'Test City1';
            resellerAcc.ShippingState = 'Test State1';
            resellerAcc.ShippingCountry = 'Denmark';
            resellerAcc.ShippingPostalCode = '4700';

            System.debug('******* Trying insert resellerAcc *******');
            insert resellerAcc;
            System.debug('******* Inserted resellerAcc *******');
            
            System.debug('****** END Reseller Test *******');
        }
        
        //Run As nordicTL
        System.RunAs(nordicAgent)
        {
            System.debug('****** START Home Test *******');
            
            System.debug('****** Creating a list of resellers in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> resellersList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Reseller')];

            System.debug('****** Creating a map of name, id for the retrieved resellers *******');
            // Create a map between the Reseller Name and Id for for easy retrieval
            Map<String,String> resellers = new Map<String,String>{};
            for(Account resAcc: resellersList)
                resellers.put(resAcc.Name,resAcc.Id);            

            // Test Home create
            System.debug('Expected User: Support User');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account homeAcc = new Account();
            
            homeAcc.Name = 'Unit Test Home1';
            homeAcc.RecordTypeId = accountRecordTypes.get('Home');
            homeAcc.Reseller__c = resellers.get('Unit Test Reseller1');
            homeAcc.ShippingStreet = 'Test Street1';
            homeAcc.ShippingCity = 'Test City1';
            homeAcc.ShippingState = 'Test State1';
            homeAcc.ShippingCountry = 'Denmark';
            homeAcc.ShippingPostalCode = '4700';
        
            System.debug('****** Trying insert homeAcc *******');
            insert homeAcc;
            System.debug('****** Inserted homeAcc *******');
            
            System.debug('****** END Home Test *******');

            // Test Contact create
            System.debug('****** START Contact Test *******');
            System.debug('****** Creating a list of Home accounts in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> homesList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Home')];

            System.debug('****** Creating a map of name, id for the retrieved homes *******');
            // Create a map between the Home Account Name and Id for for easy retrieval
            Map<String,String> homes = new Map<String,String>{};
            for(Account homAcc: homesList)
                homes.put(homAcc.Name,homAcc.Id);            

            Contact homeCon = new Contact();
            
            homeCon.AccountId = homes.get('Unit Test Home1');
            homeCon.RecordTypeId = contactRecordTypes.get('Consumer');
            homeCon.Salutation = 'Mr1';
            homeCon.FirstName = 'Apex1';
            homeCon.LastName = 'Unit-Test1';

            System.debug('****** Trying insert homeCon *******');
            insert homeCon;
            System.debug('****** Inserted homeCon *******');
            
            System.debug('****** END Contact Test *******');            
        }
    }
}


 
This is my trigger but I am getting an error Trigger has hit a max query size:50001

The following trigger is trying to populate a lookup field APA on child account. This value is based on the value in another field call APA_Name__c which is populated as a text field on child account. I have 3 accounts of APA . . Please can someone help on this.

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

  // Get a list of all active record types within the system associated to Accounts
        List<Account> APAList = [Select Name, Id From Account];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> NewAPA = new Map<String,String>{};
                  
            for(Account APAAcc: APAList)
                NewAPA.put(APAAcc.Name,APAAcc.Id); 

  for (Account NewAccount : Trigger.new)
  {
  If(NewAccount.APA_Name__c == 'Global Heat Source')
      {
     
      NewAccount.APA__c = NewAPA.get('Global Heat Source');
       
       } 
  else If(NewAccount.APA_Name__c == 'Forrest')
      {
     
      NewAccount.APA__c = NewAPA.get('Forrest');
      }
  else If(NewAccount.APA_Name__c == 'Keepmoat')
      {
     
      NewAccount.APA__c = NewAPA.get('Keepmoat');
      }
   }
   }