-1

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

This is what I have so far but I am not sure this is correct:

trigger contractStatus on Contract (after insert, after update)
{ 
    List<Account> accs = new List<Account>();
    Id accRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor Prospect').getRecordTypeId();


    for(Contract con : trigger.new)
    { 
        if (Con.Status == 'Activated' && accRecordTypeId == 'Vendor Prospect')
        {
            accRecordTypeId = 'Vendor';
        }
        else if (Con.Status == 'Activated' && accRecordTypeId == 'Client Prospect')
        {
            accRecordTypeId = 'Client';
        }
    }

    if (accs != null && !accs.isEmpty())
    {
        Database.update(accs);
    }
}

I realise that accRecordTypeId only deals with Vendor Prospect record type and not Client Prospect. But now I am unsure if this is the right way to go about verifying what the Account record type is so that the trigger can update it accordingly.

3
  • 1
    Hello @Nik welcome to the forum! There are lot of documentations, sample codes to learn. Try with them first and if you face any difficulty raise them here. Community will hep you!
    – highfive
    Aug 4 '15 at 8:32
  • Please elaborate your question. Have you written any code? Where are you facing an issue? Aug 4 '15 at 8:43
  • Hi and thanks highfive and @RuslanVekua, I have updated my question with the code I have written so far. Thanks!
    – Nik
    Aug 4 '15 at 9:03
0

If Vendor and Client are recordtype then get id as you did for 'Vendor Prospect' the recodtype, and add the recodtypeid to Con.recodtypeid. Like this :

trigger contractStatus on Contract (after insert, after update)
{ 
list<Account> acc = new List<Account>();
Account accrec = new Account();
Id accRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor Prospect').getRecordTypeId();
Id vendorRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();

Map<ID, id > accID = new Map<ID, Id>();
list<Contract> cont = new List<Contract>();
for(Contract con : trigger.new)
{ 
accID.put(con.id, con.AccountID);
cont.add(con);
}

Map<Id, Account> accs = new List<Id, Account>([SELECT RecordTypeId, name from Account where id = :accID.values()]);
ID accID;
for(Contract con : cont){
if (Con.Status == 'Activated' && accRecordTypeId == 'Vendor Prospect')
{
    accID = accID.get(con.id);
    accrec = accs.get(accID);
    accrec.RecordTypeId = vendorRecordTypeId;
}
else if (Con.Status == 'Activated' && accRecordTypeId == 'Client Prospect')
{
    accID = accID.get(con.id);
    accrec = accs.get(accID);
    accrec.RecordTypeId = clientRecordTypeId;
}
acc.add(accrec);

}

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

Practice having code in a apex class as trigger handler and call the handler from your trigger. http://www.sfdc99.com/2015/01/19/the-one-trigger-per-object-design-pattern/. In this code itself you can avoid if else (this will help you if more recordtype requirement comes) by having a interface in and calling different class from your code by setting recordtype name and class in custom setting.

4
  • Thank you @Anu, I'm getting an error with this code though: "Error: Compile Error: Incompatible element type Contract for collection of Account at line 19 column 9" Does this require Mapping?
    – Nik
    Aug 4 '15 at 10:27
  • @Nik Updated my code plz check.
    – Anu
    Aug 4 '15 at 10:46
  • Thanks so much @Anu. But I am getting an error "Error: Compile Error: list must have exactly 1 type argument at line 1 column 1"
    – Nik
    Aug 4 '15 at 15:55
  • I didnt tried this code so their may be some issues. I just code it on notepad. you need to look and fix it.
    – Anu
    Aug 5 '15 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.