function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TinkuTinku 

Trigger on Account for creating Service Contract and Entitlement

I need to write a trigger to automatically create a service contract and entitlement, whenever a person account is created.

I have written the trigger, but the service contract and entitlement are being created for the same account every time.

The issue is with the cross-object reference. Can anyone give me any suggestions or ideas please.

trigger createEntitlement on Account (after insert, before update) 
{

    Set<Id> personAccountsId=New Set<Id>();   
    List<ServiceContract> newServiceContract=New List<ServiceContract>();
    List<Entitlement> newEntitlement=New List<Entitlement>();
    
    RecordType accRecType=[Select Id From RecordType Where Name=:'Subscriber' And SobjectType=:'Account' limit 1];
    
    for(Account pa:Trigger.New)
    {

        Account acct = [Select a.OwnerId, a.id, a.Name from Account a where a.RecordTypeId=:accRectype.Id limit 1];
        
        if(pa.RecordTypeId==accRecType.Id && pa.JSC_Membership_Type__c=='Premium')
        {
            ServiceContract sc=New ServiceContract(OwnerId=pa.OwnerId, Name='New Premium Upgrade', AccountId=acct.ID );
            newServiceContract.Add(sc);
            database.insert(newServiceContract);
        }
        if(pa.RecordTypeId==accRecType.Id && pa.JSC_Membership_Type__c=='Premium')
        {   
            Entitlement entl=New Entitlement (Entitlement_owner__c=acct.OwnerId, Name='New Premium Upgrade', AccountId=acct.ID );
            newEntitlement.Add(entl);
            database.insert(newEntitlement);
        }
                
     }
   
   
}
bob_buzzardbob_buzzard

I think the lines that are causing this are below:

 

 

 Account acct = [Select a.OwnerId, a.id, a.Name from Account a where a.RecordTypeId=:accRectype.Id limit 1];
        
        if(pa.RecordTypeId==accRecType.Id && pa.JSC_Membership_Type__c=='Premium')
        {
            ServiceContract sc=New ServiceContract(OwnerId=pa.OwnerId, Name='New Premium Upgrade', AccountId=acct.ID );

 The first line is red stands a good chance of always retrieving the same Account, as you haven't specified any criteria over and above the record type and you are only retrieving a single record.  When you create the ServiceContract, you use acct.id to specify the owning account.  If you want to create the service contract on the accounts in the trigger, you'd use pa.id.

 

 

 

TinkuTinku

Thank you Bob_buzzard. by using pa.id the service contracts are being created for respective accounts.

But it is creating duplicates because i have used database.insert()

 

If i use database.update(), it gives me this error when saving the account.

 

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []:

 

What id should i be specifying at the update call[ ] ??

bob_buzzardbob_buzzard

So are you looking to update an existing contact if one exists?  In that case you'll need to execute some SOQL to pull back a matching service contract, if you find one, change the details and update it, if you don't, create a new one and insert it.