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
CooldayCoolday 

How to add opportunity logic to this existing Apex class

I have an existing Apex class which sends a payload to the third party whenever an Account is created on Salesforce side. 
I have a flow which triggers the apex class whenever an Account is created or updated.
 The new requirement is we need to write additional logic whenever an opportunity (associated with the account) is created then also send a different payload to the third party.  
 
Should I Modify my flow or make changes in the class?
 
 This is my Apex code -  

public class RESTAPI {
    @InvocableMethod()
    public static void sendRequest(List<Id> ids) {
        List <Account> objAcc = new List<Account>();
        
        objAcc = [Select Id,Name,Email__c,RecordType.Name from Account where 
                  Id in :ids];
       
        for(Account acc : objAcc) {
            ObjectWrapper objWrap = new ObjectWrapper ();
            objWrap.Id                         =           acc.Id;
            ObjectWrapper .cls_attributes attr               =           new ObjectWrapper .cls_attributes();
            attr.companyName                            =           acc.Name;
            attr.email                                  =           acc.Email__c;
            attr.sfRecordType                           =           acc.RecordType.Name;
            objWrap.attributes                          =           attr;
            
                    
            String payload          =           JSON.serialize(objWrap);
        
            if(!System.isFuture()) {
                sendRequestAsync(payload,acc.Id);
               
            }
		}
    }
    @future(callout=true)
    private static void sendRequestAsync(String payload,Id accId) {
        
        Http http                       =       new Http();
        HttpRequest request2            =       new HttpRequest();
        List<API__c> lstCS      =       API__c.getall().values();   
        if (lstCS != null && lstCS.size() > 0) {
            system.debug('102');
            String username                 =       lstCS[0].username__c;
            String password                 =       lstCS[0].password__c;
            String endp                     =       lstCS[0].endpoint__c;
            Blob headerValue                =       Blob.valueOf(username + ':' + password);
            String authorizationHeader      =       'Basic ' + EncodingUtil.base64Encode(headerValue);
            
            request2.setHeader      ('Authorization', authorizationHeader);
            request2.setEndpoint    (endp);
            request2.setMethod      ('POST');
            request2.setHeader      ('Content-Type', 'application/json; charset=utf-8');
            
            request2.setBody(payload);

            HttpResponse response = http.send(request2);
            system.debug('The Response Body: '+response.getBody());
        }         
    }
Best Answer chosen by Coolday
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below apex class .
 
public class RESTAPI  {
    @InvocableMethod()
    public static void sendRequest(List<Id> ids) {
        List<ID> leadids= new List<ID>();
        List<ID> oppyids= new List<ID>();
        For(id recordids:ids){
            String recordid=recordids;
            
            if(recordid.startsWith('006')){
                oppyids.add(recordid);
            }
            else 
                leadids.add(recordid);
            
        }
        if(leadids.size()>0){
        List <Account> objAcc = new List<Account>();
        
        objAcc = [Select Id,Name,Email__c,RecordType.Name from Account where 
                  Id in :leadids];
       
        for(Account acc : objAcc) {
            ObjectWrapper objWrap = new ObjectWrapper ();
            objWrap.Id                         =           acc.Id;
            ObjectWrapper.cls_attributes attr  =           new ObjectWrapper.cls_attributes();
            attr.companyName                            =           acc.Name;
            attr.email                                  =           acc.Email__c;
            attr.sfRecordType                           =           acc.RecordType.Name;
            objWrap.attributes                          =           attr;
            
                    
            String payload          =           JSON.serialize(objWrap);
        
            if(!System.isFuture()) {
                sendRequestAsync(payload,acc.Id);
               
            }
		}
        }
        if(oppyids.size()>0){
         List <Opportunity> objOpp = new List<Opportunity>();
        
        objOpp = [Select Id,Name,Email__c from Opportunity where 
                  Id in :oppyids];
       
        for(Opportunity acc : objOpp) {
            ObjectWrapper objWraps = new ObjectWrapper ();
            objWraps.Id                         =           acc.Id;
            ObjectWrapper.cls_attributesoppy attrs  =           new ObjectWrapper.cls_attributesoppy();
            attrs.oppName                            =           acc.Name;
            attrs.oppEmail                           =           acc.Email__c;
            attrs.oppSFId                            =           acc.id;
            objWraps.attopp                      =           attrs;
            
                    
            String payload          =           JSON.serialize(objWraps);
        
            if(!System.isFuture()) {
                sendRequestAsync(payload,acc.Id);
               
            }
		}
        }
        
    }
    @future(callout=true)
    private static void sendRequestAsync(String payload,Id accId) {
        
        Http http                       =       new Http();
        HttpRequest request2            =       new HttpRequest();
        List<API__c> lstCS      =       API__c.getall().values();   
        if (lstCS != null && lstCS.size() > 0) {
            system.debug('102');
            String username                 =       lstCS[0].username__c;
            String password                 =       lstCS[0].password__c;
            String endp                     =       lstCS[0].endpoint__c;
            Blob headerValue                =       Blob.valueOf(username + ':' + password);
            String authorizationHeader      =       'Basic ' + EncodingUtil.base64Encode(headerValue);
            
            request2.setHeader      ('Authorization', authorizationHeader);
            request2.setEndpoint    (endp);
            request2.setMethod      ('POST');
            request2.setHeader      ('Content-Type', 'application/json; charset=utf-8');
            
            request2.setBody(payload);

            HttpResponse response = http.send(request2);
            system.debug('The Response Body: '+response.getBody());
        }         
    }
}

Wrapper:
 
public class ObjectWrapper {
    public String Id;  
    public cls_attributes attributes;
    public cls_attributesoppy attopp;
    public class cls_attributes {
        public String companyName;
        public String email;    
        public String sfRecordType;
}
    public class cls_attributesoppy {
        public String oppName;
        public String oppSFId;    
        public String oppEmail;
} 
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm which field do you need to send  to other system also can you confirm what is cls_attributes in you org?

Thanks,
 
CooldayCoolday

Hi ,

We are using cls_attributes from this wrapper class 

public class ObjectWrapper {
    public String Id;  
    public cls_attributes attributes;
    public class cls_attributes {
        public String companyName;
        public String email;    
        public String sfRecordType;
}
}
CooldayCoolday

Whenever an Opportunity will be created on SF we have to send these parameters to the third party-

{
    "type": "task",
    "Id": "0019D00000M2OcOYTD",
    "actions":[{
    "action": "ACTION",
    "attributes": {
        "oppSFId": "0069D00000LpHin999",
        "oppName": "OPP NAME",
        "oppEmail": "xyz@yopmail.com",
    }
    }]
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Do you need seperate apex class as it would be easy to handle? You need to create any way new flow for that beacuse the flow on Account is record trigered flow I guess.

Thanks,
 
CooldayCoolday
Yes It's a record triggered flow on Account for Insert and Update but I wanted to do this in the same class itself.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Thanks for confirming on it. I will modify the code accordinly. Can you create the similar flow for Opportunity as well.

Thanks,
 
CooldayCoolday
Sure
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below apex class .
 
public class RESTAPI  {
    @InvocableMethod()
    public static void sendRequest(List<Id> ids) {
        List<ID> leadids= new List<ID>();
        List<ID> oppyids= new List<ID>();
        For(id recordids:ids){
            String recordid=recordids;
            
            if(recordid.startsWith('006')){
                oppyids.add(recordid);
            }
            else 
                leadids.add(recordid);
            
        }
        if(leadids.size()>0){
        List <Account> objAcc = new List<Account>();
        
        objAcc = [Select Id,Name,Email__c,RecordType.Name from Account where 
                  Id in :leadids];
       
        for(Account acc : objAcc) {
            ObjectWrapper objWrap = new ObjectWrapper ();
            objWrap.Id                         =           acc.Id;
            ObjectWrapper.cls_attributes attr  =           new ObjectWrapper.cls_attributes();
            attr.companyName                            =           acc.Name;
            attr.email                                  =           acc.Email__c;
            attr.sfRecordType                           =           acc.RecordType.Name;
            objWrap.attributes                          =           attr;
            
                    
            String payload          =           JSON.serialize(objWrap);
        
            if(!System.isFuture()) {
                sendRequestAsync(payload,acc.Id);
               
            }
		}
        }
        if(oppyids.size()>0){
         List <Opportunity> objOpp = new List<Opportunity>();
        
        objOpp = [Select Id,Name,Email__c from Opportunity where 
                  Id in :oppyids];
       
        for(Opportunity acc : objOpp) {
            ObjectWrapper objWraps = new ObjectWrapper ();
            objWraps.Id                         =           acc.Id;
            ObjectWrapper.cls_attributesoppy attrs  =           new ObjectWrapper.cls_attributesoppy();
            attrs.oppName                            =           acc.Name;
            attrs.oppEmail                           =           acc.Email__c;
            attrs.oppSFId                            =           acc.id;
            objWraps.attopp                      =           attrs;
            
                    
            String payload          =           JSON.serialize(objWraps);
        
            if(!System.isFuture()) {
                sendRequestAsync(payload,acc.Id);
               
            }
		}
        }
        
    }
    @future(callout=true)
    private static void sendRequestAsync(String payload,Id accId) {
        
        Http http                       =       new Http();
        HttpRequest request2            =       new HttpRequest();
        List<API__c> lstCS      =       API__c.getall().values();   
        if (lstCS != null && lstCS.size() > 0) {
            system.debug('102');
            String username                 =       lstCS[0].username__c;
            String password                 =       lstCS[0].password__c;
            String endp                     =       lstCS[0].endpoint__c;
            Blob headerValue                =       Blob.valueOf(username + ':' + password);
            String authorizationHeader      =       'Basic ' + EncodingUtil.base64Encode(headerValue);
            
            request2.setHeader      ('Authorization', authorizationHeader);
            request2.setEndpoint    (endp);
            request2.setMethod      ('POST');
            request2.setHeader      ('Content-Type', 'application/json; charset=utf-8');
            
            request2.setBody(payload);

            HttpResponse response = http.send(request2);
            system.debug('The Response Body: '+response.getBody());
        }         
    }
}

Wrapper:
 
public class ObjectWrapper {
    public String Id;  
    public cls_attributes attributes;
    public cls_attributesoppy attopp;
    public class cls_attributes {
        public String companyName;
        public String email;    
        public String sfRecordType;
}
    public class cls_attributesoppy {
        public String oppName;
        public String oppSFId;    
        public String oppEmail;
} 
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer