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 Help

Trigger which creates Entitlements and also updates a field on Account. Since it is updating a field on Account, i cannot use (After Insert, After Update). When i use (before insert, before update) It gives me this error:

Apex trigger createEntitlement caused an unexpected exception, contact your administrator: createEntitlement: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account Name]: [Account Name]: Trigger.createEntitlement:

I need some ideas on how to change the logic in this trigger to create the new Entitlement.

trigger createEntitlement on Account (before insert, before update) 
{
DateTime dt=System.now();

    set<Id> PersonAccountsId = new set<Id>();
    List<ServiceContract> newServiceContract=New List<ServiceContract>();
    List<Entitlement> newEntitlement=New List<Entitlement>();

    map<string, String> ServiceContractMap = new map<string,string>();
    map<string, String> EntitlementMap = new map<string,string>();
   
    RecordType accRecType=[Select Id From RecordType Where Name=:'Subscriber' And SobjectType=:'Account' limit 1];

if(Trigger.IsInsert )
{
       List<string> accIdList = new List<String>();
       Map<String,String> accConMap = new map<String,String>();
   
    for(Account acc : trigger.New)
   {
     if(acc.RecordTypeId ==  accRecType.Id)   
       { 
       accIdList.add(acc.Id);
       acc.Premium_Upgrade_Date__c=dt.date();
       }    

    }
    if(accIdList.size() > 0 )
 {

   for(Account pa:Trigger.New)
    {       
        ServiceContract sc; Entitlement entl;
        if(pa.RecordTypeId==accRecType.Id && pa.JSC_Membership_Type__c=='Premium')
        {
            sc=New ServiceContract(OwnerId=pa.OwnerId, Name='JSA', AccountId=pa.ID);
            sc.StartDate = dt.date();
            newServiceContract.Add(sc);
            database.insert(newServiceContract);     
            
            entl=New Entitlement (Entitlement_owner__c=pa.OwnerId, Name='JSA', AccountId=pa.ID,ServiceContractId=sc.Id, SlaProcessId='552R00000004CGs');
            entl.StartDate = dt.date();
            newEntitlement.Add(entl);            
           database.insert(newentitlement); <-- error here
            

        }
                             
    }

 } 

}

}
Damien_Damien_
You may have other problems but move that line you got the error on.  Each iteration of the loop you keep inserting the same list.

for(Account pa:Trigger.New)
    {       
        ServiceContract sc; Entitlement entl;
        if(pa.RecordTypeId==accRecType.Id && pa.JSC_Membership_Type__c=='Premium')
        {
            sc=New ServiceContract(OwnerId=pa.OwnerId, Name='JSA', AccountId=pa.ID);
            sc.StartDate = dt.date();
            newServiceContract.Add(sc);
            database.insert(newServiceContract);     
            
            entl=New Entitlement (Entitlement_owner__c=pa.OwnerId, Name='JSA', AccountId=pa.ID,ServiceContractId=sc.Id, SlaProcessId='552R00000004CGs');
            entl.StartDate = dt.date();
            newEntitlement.Add(entl);            
          
            

        }
insert newentitlement;

 

 

    }

TinkuTinku

Yes. It still does not solve my problem. any suggestions please.

Damien_Damien_

The only other idea I could suggest is changing the AccountID =id to Account = id;

 

 

AccountId=pa.ID --> Account = pa.ID

 

I had similar issues before such as:

 

property__c.id = current.id //and it worked when I went

 

property__c = current.id

 

If this doesn't work I'm out of suggestions.

TinkuTinku

When i give: 

Account = pa.ID -->instead of AccountId = pa.ID

It gives me this error while saving the trigger:

 

Error: Compile Error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type)