0

I have this class wherein if the Accountid is not equal to contactID it should update the enddate to today. the problem is that it doesn't update the enddate to today date

    public with sharing class AccountContactChangeClass {

public static void AccountContactChange (map<Id, Contact> acctChangeContactsMap){
List<Accountcontactrelation> acccn;
System.debug('Test1 = ' + acccn);
if(acctChangeContactsMap!= null){
List<Accountcontactrelation> acrn  = New List<Accountcontactrelation>([SELECT AccountId,ContactId,CreatedById,CreatedDate,CurrencyIsoCode,EndDate,Id,IsActive,IsDeleted,IsDirect,LastModifiedById,LastModifiedDate,Roles,StartDate,SystemModstamp FROM AccountContactRelation where contactid in : acctChangeContactsMap.keyset()]); 
   System.debug('Test2 = ' + acrn);
   for (Accountcontactrelation arnt : acrn){
       if(arnt.accountid != acctChangeContactsMap.get(arnt.contactid).account.id)
       {
       arnt.enddate = system.today();
       System.debug('Test5 = ' + arnt);
       acccn.add(arnt);
       System.debug('Test6 = ' + arnt);
       }

    }
    update acccn;
    System.debug('Test8 = ' + acccn);

}
}}
  • Hi Sony, what is the problem you are facing? Please explain. – Nitish Singhal May 25 '17 at 6:37
  • the enddate doesn't update, actually this is with a batch job, and it will run daily wherein if it found changes on the account from contact it will set the enddate to today in the accountcontact relation, – Sony PSP May 25 '17 at 6:39
2

You had a number of minor issues, outlined in your code, revised with comments.

public with sharing class AccountContactChangeClass {
    public static void AccountContactChange (map<Id, Contact> acctChangeContactsMap){
        // Always initialize your variables
        List<Accountcontactrelation> acccn = new List<AccountContactRelation>();
        // Don't have pointless debug messages --> // System.debug('Test1 = ' + acccn);

        // If the map was null, why did you bother calling this method?
        // if(acctChangeContactsMap!= null) {

        // The result of a query is already a list.
        // Also, you literally only need two fields. Do not query unused fields.
        List<Accountcontactrelation> acrn = 
            [SELECT AccountId, ContactId 
             FROM AccountContactRelation 
             where contactid in : acctChangeContactsMap.keyset()];
        for (Accountcontactrelation arnt : acrn) {
            // I strongly suspect that Account.Id is null. Use AccountId instead.
            // Do not use a relationship to get Id from a parent record.
            if(arnt.accountid != acctChangeContactsMap.get(arnt.contactid).AccountId) {
                arnt.enddate = system.today();
                acccn.add(arnt);
            }
        }
        update acccn;
    }
}

Your primary issue was that you were using Account.Id when you almost certainly meant to use AccountId.

1

Ok, there were some syntax mistakes in your code, and I have tried to fix them. Please try the below code.

public with sharing class AccountContactChangeClass {
public static void AccountContactChange (map<Id, Contact> acctChangeContactsMap){
    List<AccountContactRelation> acccn = List<AccountContactRelation>();
    System.debug('Test1 = ' + acccn); 
    if(acctChangeContactsMap!= null){ 
        List acrn = New List([SELECT AccountId,ContactId,CreatedById,CreatedDate,
                                CurrencyIsoCode,EndDate,Id,IsActive,IsDeleted,IsDirect,LastModifiedById,LastModifiedDate,Roles,
                                StartDate,SystemModstamp FROM AccountContactRelation 
                                where contactid in : acctChangeContactsMap.keyset()]); 
        System.debug('Test2 = ' + acrn); 
        for (Accountcontactrelation arnt : acrn){ 
            if(arnt.accountid != acctChangeContactsMap.get(arnt.contactid).accountId) { 
            arnt.enddate = system.today(); 
            System.debug('Test5 = ' + arnt); 
            acccn.add(arnt); 
            System.debug('Test6 = ' + arnt); 
            }
        }   
        update acccn;
        System.debug('Test8 = ' + acccn);
    } 
}}  

Now please pass the correct map into this class's method from your batch class, and try to test.

Hope this helps :)

  • Your code also has syntax errors. – sfdcfox May 25 '17 at 6:48
  • Hi sfdcfox, you are right :). I have edited the code, and tried saving in my org. It worked. Sony, please try – Nitish Singhal May 25 '17 at 7:13
  • Hi Both you are both correct. :) I already modified the code and it seems to be working. – Sony PSP May 25 '17 at 11:13
0

Thanks everyone!

public with sharing class AccountContactChangeClass { public static void AccountContactChange (map acctChangeContactsMap){ List acccn = new List(); List acrn = [SELECT AccountId, ContactId FROM AccountContactRelation where contactid in : acctChangeContactsMap.keyset()]; System.debug('Test2 = ' + acrn); for (Accountcontactrelation arnt : acrn){ if(arnt.accountid != acctChangeContactsMap.get(arnt.contactid).accountId) { arnt.enddate = system.today(); System.debug('Test5 = ' + arnt); acccn.add(arnt); System.debug('Test6 = ' + arnt); } }
update acccn; System.debug('Test8 = ' + acccn); } }

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.