0

I need to write a trigger so that when a new Contract (standard object) is created and activated e.g Contract.Status = Activated, the related Account record types update like this:

Vendor Prospect -> Vendor Or Client Prospect -> Client

Here is my code so far:

trigger contractStatus on Contract (after insert, after update)
{ 
    List <Account> accs = new List <Account>();
    List <Account> newAcc = new List <Account>();
    List <Contract> cons = new List <Contract>();
    Id accRecordTypeIdVP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor-Prospect').getRecordTypeId();
    Id accRecordTypeIdCP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client-Prospect').getRecordTypeId();
    Id vendorRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
    Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();

    for(Contract con : trigger.new)
    { 
        if (Con.Status == 'Activated')
        {
            cons.add(con);
        }

        for(Account acc :accs)
        {

            if(acc.RecordTypeId == accRecordTypeIdVP)
            {
                acc.RecordTypeId = vendorRecordTypeId;
            }
            else if(acc.RecordTypeId == accRecordTypeIdCP)
            {
                acc.RecordTypeId = clientRecordTypeId;
            }

            newAcc.add(acc);

        }

        if (accs.size() > 0)
        {
            Database.update(accs);
        }
    }
}

The problem is, is that now when I test it (Create a Contract with status = activated) and check the account recordtype, the record type does not change. If somebody could help me figure out what I am doing wrong I would greatly appreciate it. Thanks.

6
  • 2
    Where are you populating values in accs variable?
    – San
    Aug 4, 2015 at 17:31
  • Did I miss adding a SOQL statement when I created accs? @San Would this be right? List<Account> accs = new List<Account>([SELECT RecordTypeId, name from Account]); or do I need a WHERE clause as well?
    – Nik
    Aug 4, 2015 at 17:36
  • Put where clause to get the accounts whose id is in contract (trigger. New)
    – San
    Aug 4, 2015 at 17:43
  • Check for neeAcc variable size and upsert that.
    – San
    Aug 4, 2015 at 17:46
  • Sorry, I don't know what exactly to put in the WHERE clause. As for the newAcc I have added this to my code instead: if(newAcc.size() > 0){Database.upsert(newAcc);}
    – Nik
    Aug 4, 2015 at 18:04

0

You must log in to answer this question.

Browse other questions tagged .