+ Start a Discussion
Andries.NeyensAndries.Neyens 

Email2Case - Web2Case Remove automatic linking of Account/Contact to Case

Hi all,

I'm trying to remove the automatic linking of an PersonAccount to a Case with triggers, but no success:

Insert Before Case trigger: The link is not yet put into the account lookup field.
Insert After Case trigger: the automatic linking is available, so I try to remove it (with a future call ofcourse)

And what happens, it is linked again.

Same is happening if I do this via the Salesforce gui, edit the case, remove the account lookup and save: link is put back again

Vinit_KumarVinit_Kumar
Can you post your Trigger code ??
Andries.NeyensAndries.Neyens
Forgot to mention, the code is working in Unit tests if I attach the wrong lookup myself


....
if (trigger.isAfter && trigger.IsInsert) {  
  
    
     List<Case> casesToValidate = new List<Case>();
     for(Case cse : (List<Case>)Trigger.new){     
      if (cse.AccountId!=null)
       casesToValidate.add(cse);     
     }  
     if (casesToValidate.size() > 0)
      ValidateBrandOfConnectedAccounts(casesToValidate);     // will contstruct a Set of Case ID,s with some business logic and call UnlinkWrongConnectedCases
      
    }
...

@future (callout=false)
  private static void UnlinkWrongConnectedCases(Set<Id> cseIdSet){
   if (cseIdSet==null || cseIdSet.size()==0) return;    //bail out
     
   List<Case> cseToUpdate = new List<Case>();
   for(Id i:cseIdSet){
    cseToUpdate.add(new Case(Id=i, AccountId=null));    //unlink the account
   }
  update cseToUpdate;
  }
Vinit_KumarVinit_Kumar
Code looks good to me...the way I would approach is to go through the Debug logs and see what's happening there.

Anothier thing which I noticed is any particular reason you have marked the method to run in future coz future method runs Asynchronously and we can't predict when it is going to execute. 
Andries.NeyensAndries.Neyens
The future method is needed because I'm trying to fix it in the After Insert, the state of the case is read only...

Debug logs are not saying anything, seems that salesforce is doing stuff just after the AFTER Update trigger...very weird


Vinit_KumarVinit_Kumar
You can still update the record in after events,you just need to create a new instance of each sObject record you want to work with and add it to a new list for update purposes.Something like below :-

...assuming after insert trigger on account...
List<Account> accts = new List<Account>();
for (Account a: Trigger.new) {

  if (some condition) {
    ...take action on a...
    Account acctToUpdate = new Account(id=a.Id, ...other updates...);
    accts.add(acctToUpdate);
  }
}

update accts;

So,I don't thihnk you need a future method here  as you are actually doing the same.
Andries.NeyensAndries.Neyens
Forgot to mention the solution to this problem!

The Case has 2 lookup fields:

AccountId and ContactId,

For personAccount, those 2 fields are filled in with the same id.
So the solution was to blank out both fields instead of just one

cheers,
 
Diego LacerdaDiego Lacerda
Thank you!!! Andries.Neyens