You need to sign in to do that
Don't have an account?
Coolday
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()); } }
Can you try the below apex class .
Wrapper:
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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,
Hi ,
We are using cls_attributes from this wrapper class
Whenever an Opportunity will be created on SF we have to send these parameters to the third party-
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,
Thanks for confirming on it. I will modify the code accordinly. Can you create the similar flow for Opportunity as well.
Thanks,
Can you try the below apex class .
Wrapper:
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,