0

here I'm calling below class from the trigger and getting response from external system as well whether my regNo is blacklisted or not, but after saving the record how we can update the status basis the callout response from the future method

below are my class and trigger code, please assist on the same.

Your response is highly appreciated

Also do let me know, incase if we need to upsert mass records from dataloader, how we can handle the same

Thank You!

Apex Class:

public class BlackListCheck 
{
    @future(callout=true)
    public static void getInSysBlackListCheck(Map<Id, string> accBlacklistMap)
    {
         list<Account> blackListedAccs = [SELECT Name,id,new_regNo__C FROM Account WHERE Id 
                                                                     IN:accBlacklistMap.KeySet()];
         string accessTokenFromDPT = AccountCreditCheckService.getTokenFromCreditCheckDPTApi();
         try{
              if(accessTokenFromDPT == null)
              {
                    system.debug('Unable to get the access token');
              }
              else if(!blackListedAccs.isEmpty())
              {
                Map<id,Account > accToBeUpsert = new Map<id,Account>();
                for(Account acc:blackListedAccs)           
                {
                  String body = '        {'+
                                '        "customerIdType": "4",'+
                                '        "customerIdNo": ' + '"'+acc.new_regNo__c +'"'+ ','+
                                '        "system": "All" '+
                                '        }';
                  
                 system.debug(body);
                    
                 Http http = new Http();            
                 HttpRequest requestToinSys = new HttpRequest(); 

                 requestToinSys.setEndpoint(Blacklist_Credentials__c.getvalues('Staging Credentials').Blacklist_Endpoint__c);
                 requestToinSys.setMethod('POST');
                 requestToinSys.setHeader('Content-Type', ' application/json');
                 requestToinSys.setHeader('Authorization', 'Bearer '+accessTokenFromDPT);
                 requestTOinSys.setBody(body);
                 HttpResponse inSysResp = http.send(requestToinSys);
                 System.debug('@@@ inSysresponse :'+ inSysResp.getBody());
                 
                 JsonParser ctosCheckListTraverse = Json.createParser(inSysResp.getBody());
                 integer exInternalBlacklisted =0;
                 integer exInternalNotBlacklisted =0;
                 while(ctosCheckListTraverse.nextToken()!=null)
                 {
                    if(ctosCheckListTraverse.getCurrentToken() == JSONToken.FIELD_NAME && ctosCheckListTraverse.getText() =='status')
                    {
                       ctosCheckListTraverse.nextToken();
                       if(ctosCheckListTraverse.getText() == 'Success')
                       {
                        //Black Listed
                        exInternalBlacklisted+=1;
                       }
                       else if(ctosCheckListTraverse.getText() == 'Failed')
                       {
                        exInternalNotBlacklisted+=1;
                        }
                    }
                 }
                 if(exInternalBlacklisted>=1)
                 {
                     acc.Status__c='Blacklisted';
                 }
                 else if(exInternalNotBlacklisted>=2)
                 {
                      acc.Status__c='Pending'; 
                 }
                 accToBeUpsert.put(acc.id,acc);
               }
               if(!accToBeUpsert.isEmpty())
               {
                Upsert accToBeUpsert.values();
               }
              }
            }
            Catch(Exception ex)
            {
                 string errorMessage = ex.getMessage();
                 System.debug('Exception :' + ex.getMessage());         
            }
    }
}

Apex Trigger:

trigger BlacklistAccount on Account (before update, after update,after insert,before insert) 
{
 if((Trigger.isInsert && Trigger.isAfter)||(Trigger.isUpdate && Trigger.isAfter))
   {
     if(!System.isFuture() && !System.isBatch())
     {
        Map <id, string> AccregNos = new Map <id, string>();
        if(Trigger.isInsert && Trigger.isAfter)
        {           
            System.debug('Making future call to update account');
            for (Account acc : Trigger.New) 
            {
                if(acc.New_regNo__c!='' && acc.New_regNo__c!=null)
                {
                    AccregNos.put(acc.id, acc.New_regNo__c);
                    BlackListCheck.getInSysBlackListCheck(AccregNos);
                }
            }   
        }
        else if(Trigger.isUpdate && Trigger.isAfter)
        {
         System.debug('Making future call to update account1');
         for (Account acc : Trigger.new) 
         {
           System.debug(acc.New_regNo__c);
           System.debug(trigger.oldMap.get(acc.id).New_regNo__c);
           if(acc.New_regNo__c!='' && acc.New_regNo__c!=null && acc.New_regNo__c!=trigger.oldMap.get(acc.id).New_regNo__c)
           {
            AccregNos.put(acc.id, acc.New_regNo__c);
            BlackListCheck.getInSysBlackListCheck(AccregNos);
           }
         }
        }
        
    }  
}
}
2
  • What is happening on upsert?
    – devforce
    May 4, 2022 at 12:14
  • @devforce record getting saved, but need to be refreshed to get reflected the blacklist result in the account status
    – Harish
    May 4, 2022 at 17:34

1 Answer 1

0

Try Requery with AccountIds and update.

Refer: (The issue reported is NOT the same; but check "updateSERecord" method for the code pattern)

https://developer.salesforce.com/forums/?id=9062I000000IM1tQAG

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .