Skip to main content Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

#Apex63 discussing

The company I am working on this project for is in Phoenix, AZ.  

 

We are going to be sending out SMS appt reminders to people located around the US. I have an API to handle finding their timezone based on zip code. I am using 

zipcodeapi.com

 to bring in information that includes the Timezone abbreviation.  

I haven't put together the code/formula to account for timezone changes when sending out the reminders yet, but that isn't my issue.  

 

My issue is the when everyone else goes back and forth from DST to Standard Time Phoenix doesn't. They are on Mountain time now and Pacific time during DST.  

 

I figure that if I can get the DST changes automated via api, then I can switch my offset formula to calculate when the change event comes through. Basically I can have 2 sets of formulas; One that, for example, adds 2 hours to an East Coast appt time in winter. Then when the DST event occurs my code can point to the other set of formulas that adds 3 hours to an appt time during DST, using a boolean. 

 

I don't want to get all of the other parts figured out and then have this be a bigger issue than I initially planned for.  

 

Does anyone have any ideas about tackling the AZ issue?  

 

Huge thanks in advance!  

 

#Salesforce Developer

 

 

#API  #Flow  #Apex

0/9000

I have an apex class which calls the tooling api. The error code is 302. How can I authenticate it as if it's run by system. The apex class gets called by agentforce 

 

#Salesforce Developer  #Agentforce  #Salesforce  #Apex

1 answer
  1. Feb 24, 10:19 AM

    Hi @Arya Kaujalgi

     

    To set up a Named Credential and External Credential for authenticating Agentforce, follow these steps:

    1. Create an External Credential:

    • In Setup, search for "Named Credentials" in the Quick Find box and select Named Credentials.
    • Click on the External Credentials tab.
    • Click New and enter the required details.
    • For Label and Name, use your Salesforce Commerce Cloud instance name (e.g., SFCC).
    • Set the Authentication Protocol to No Authentication.
    • Save your changes.

    2. Create and Authenticate an OAuth External Credential:

    3. Create a Named Credential:

    For authenticating an Apex class calling the Tooling API with a 302 error:

    • You can use the Headless Passwordless Login Flow for private clients. This involves configuring the Headless Passwordless Login API via an Experience Cloud site to log the user in. Here's how:

    1. Complete Prerequisites for Headless Identity:

    2. Configure Experience Cloud Settings for Headless Passwordless Login:

    • In your Experience Cloud settings, enable Require authentication to access this API.
    • Include an access token issued to an internal integration user when making the initial request to the "services/auth/headless/init/passwordless/login" endpoint.

    3. Implement the Flow:

    • Your app sends a headless POST request to the passwordless login endpoint on your Experience Cloud site.
    • Salesforce will return a success message with a request identifier.
    • Salesforce sends an OTP to the user.
    • The user enters the OTP into your app.
    • Your app then initializes the Authorization Code and Credentials Flow, sending a request to the authorization endpoint to exchange the request ID and OTP for an authorization code.
    • Salesforce will return a 302 redirect to a preconfigured URL with the authorization code.
    • Your server-side callback handler extracts the authorization code and sends a POST request to the token endpoint.
    • Salesforce will validate the request and return an access token.

    For more detailed instructions, refer to the "Headless Passwordless Login Flow for Private Clients" guide (https://help.salesforce.com/s/articleView?id=sf.remoteaccess_headless_passwordless_login_private_clients.htm&language=en_US

    ). 

     

     Hope this helps. Thanks!

0/9000
4 answers
  1. Feb 24, 7:43 AM

    Hello@sunil yadav

    Please try,

    trigger CalculateNoofContact on Contact (after Insert,after Update,After Delete,after Undelete) {

    List<Account> acList=new List<Account>();

    set<Id> cid=new set<Id>();

    if(trigger.isAfter){

    if(trigger.isInsert||trigger.isUpdate||trigger.isUndelete){

    for(Contact c:trigger.new){

    cid.add(c.AccountId);

    }

    }

    if(trigger.isUpdate||trigger.isUpdate||trigger.isDelete){

    for(Contact c:trigger.old){

    cid.add(c.AccountId);

    }

    }

    }

    List<Account> ac=[select Id,Contact_Count__c,(select Id from Contacts) from Account where Id=:cid];

    for(Account a: ac){

    Integer count=a.Contacts.size();

    a.Contact_Count__c=count;

    acList.add(a);

    }

    update acList;

    }

0/9000
1 answer
0/9000
I have requirement to upload csv file by salesforce Admin , the data in this csv file is not realted to one object on sakesfroce and coming from another system, read this file row by row , make some logic for each row , as an example , search of records by email address , if there is lead or contact record with the same email address , create new multiple standard and custom objects (parents and childs) , if no , just update that lead record. 

Can this requirement be implemented by salesforce Flow , if so , please help me how to design that flow ? if not , can that be implemented by salesforce Apex ? 
2 answers
  1. Apr 26, 2023, 7:06 AM
    Yes, the requirement to upload a CSV file, read it row by row, and perform logic on each row can be implemented using Salesforce Flow or Apex.

    Using Salesforce Flow:

    Create a new flow in Salesforce Flow Builder.

    Add a "Record Create" element to create new parent and child objects for standard and custom objects as needed.

    Add a "Record Update" element to update the existing Lead record if no matching records are found.

    Add a "Record Lookup" element to search for Lead or Contact records based on email address.

    Use the "Loop" element to iterate through each row in the CSV file.

    Add decision elements to check if a Lead or Contact record exists with the same email address, and based on the result, perform the desired logic.

    Use the "Import CSV" element to read and parse the CSV file.

    Map the fields from the CSV file to the corresponding fields in the objects being created or updated.

    Save and activate the flow.

    Using Salesforce Apex:

    Create an Apex class that implements the logic to read and process the CSV file.

    Use the Salesforce Apex CSV Parser library to parse the CSV file and retrieve the data.

    Loop through each row in the CSV file and perform the desired logic, such as querying for existing Lead or Contact records, creating new parent and child objects, and updating existing Lead records.

    Upload the CSV file using a Salesforce Visualforce page or a Salesforce Lightning Component.

    Invoke the Apex class from the Visualforce page or Lightning Component to start the processing of the CSV file.

    Save and deploy the Apex class and Visualforce page or Lightning Component.

    Create an Apex class that implements the logic to read and process the CSV file. You can use the Salesforce Apex CSV Parser library to parse the CSV file and retrieve the data.

     

    public class CSVUploadController {

    public static void processCSVFile(Id parentId, String csvFileBody) {

    // Parse the CSV file and retrieve the data

    List<Map<String, String>> csvData = CSVParser.parseCSV(csvFileBody);

    // Loop through each row in the CSV file

    for (Map<String, String> row : csvData) {

    // Retrieve email address from the row

    String email = row.get('Email');

    // Query for existing Lead or Contact records with the same email address

    List<Lead> leads = [SELECT Id, Email FROM Lead WHERE Email = :email];

    List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE Email = :email];

    if (!leads.isEmpty()) {

    // Update existing Lead record

    Lead lead = leads[0];

    // Perform the desired logic for updating Lead record

    // ...

    } else if (!contacts.isEmpty()) {

    // Create new parent and child objects for custom objects

    // Perform the desired logic for creating custom objects

    // ...

    } else {

    // Create new parent and child objects for standard objects

    // Perform the desired logic for creating standard objects

    // ...

    }

    }

    }

    }

    Upload the CSV file using a Salesforce Visualforce page or a Salesforce Lightning Component. You can create a Visualforce page with an HTML form to allow Salesforce Admins to upload the CSV file, or you can create a Lightning Component with a file upload component for the same purpose.

    Invoke the Apex class from the Visualforce page or Lightning Component to start the processing of the CSV file. Call the processCSVFile method from the Apex class and pass in the parent object's Id and the body of the CSV file as parameters.

    <!-- Visualforce Page Example -->

    <apex:page controller="CSVUploadController">

    <apex:form enctype="multipart/form-data">

    <apex:inputFile value="{!csvFile}" />

    <apex:commandButton value="Upload CSV" action="{!uploadCSV}" />

    </apex:form>

    </apex:page>

    <!-- Lightning Component Example -->

    <!-- File upload component to upload the CSV file -->

    <lightning:input type="file" label="CSV File" accept=".csv" onchange="{!c.handleFileUpload}" />

    <!-- Controller method to handle file upload -->

    handleFileUpload: function(component, event, helper) {

    var fileInput = component.find("fileInput").getElement();

    var file = fileInput.files[0];

    var reader = new FileReader();

    reader.onload = function() {

    var csvFileBody = reader.result;

    var parentId = // Retrieve the parent object's Id;

    // Call the Apex class to process the CSV file

    var action = component.get("c.processCSVFile");

    action.setParams({ parentId: parentId, csvFileBody: csvFileBody });

    action.setCallback(this, function(response) {

    // Handle response

    });

    $A.enqueueAction(action);

    };

    reader.readAsText(file);

    }

    If you find my answer helpful, please mark it as the best answer. Thanks!
0/9000
I recently updated VS Code, but when I relauched it and tried to authorize an org I get an error saying the follwing:

Command 'SFDX: Authorize an Org' resulted in an error (command 'sfdx.force.auth.web.login' not found)

I believe I have the Salesforce CLI added to my path. If anyone is able to help I would greatly appreciate it. Thank you
9 answers
  1. Jun 24, 2021, 6:42 PM
    I ended up downloading the previous version of VS Code and there is no error anymore.
0/9000
I want to export a Salesforce report to excel .xlsx format and attach it to Files via Apex, how to achieve that?
4 answers
  1. Feb 19, 1:15 PM

    @Mohit Burman It's a wrong solution. I tried and got this error Invalid type: XSSFWorkbook. XSSFWorkbook is not available in salesforce, it's for java.

0/9000

Hi, I need help updating the API version for some of my Apex Classes. I am not using any Apex classes in the org don't really know how to use Apex and I am a solo Admin. The reason I am doing this is because the latest release failed and lists this at the reason.      

 

I got this message from Salesforce:   We were unsuccessful in enabling the ICU locale formats release update in your production org. The enablement failed because this org contains one or more Apex classes, Apex triggers, or Visualforce pages that are using an API version less than 45.0. We will try to enforce this release update again in the next release.      

 

I believe I updated the ICU Local formats successfully, but need help updating the Apex classes. I have identified the Apex classes that need updating but when I edit them and update the Api Version it does not save. I think the one I am trying to update is part of the NPSP installed packages. Do I need to do something in the "Select to Add Installed Package" pull down and add it to Nonprofit Success Pack?      

 

I found another similar question -  is this something I don't need to worry about?   API Version 23.0 Visualforce pages | Salesforce Trailblazer Community     

 

Thank you, Community!    

 

#Nonprofit

 

 

#Apex

0/9000
1 answer
  1. Jan 5, 2023, 2:31 PM
    try with below query 

     

    Select id, Name from opportunity Where ContactId IN(Select id from Contact where LeadSource !='Web' )

    If this helps, Please mark it as best answer.

    Thanks!!
0/9000
Hello,

In my org some of emails sent to email2case address do not create cases. We can reproduce it and traced smtp logs on the server which sends to SF. They sent successfully.

On SF side debug log was collected and it showed "Too many SOQL queries: 101" when such email was sent.

We suspect that it might be caused by some apex class. Could you point me out - how to find what specific class or trigger is causing this?

Thanks
6 answers
0/9000