Header Ads Widget

QUESTION AND ANSWERS OF SFDC


* There are two sObjects A and B.

* For A's Object user has access, For B he don't have access.

* If he write a Trigger on A's object which intern want to access B's Object What will happen?

Answer:

* Triggers run in the system context (unless you hand over to a class decorated as 'with sharing') so I'd expect the trigger to be able to access object B as long as the user's license type covers that object.



Question:

I have to call to an external webservice everytime a new account is created. My question is, what is the best way to do so?

I thought about creating an after create trigger that will invoke the method in the class that was generated from the WSDL import. Would that make sense?



If so, how can I import a class into a trigger to use its methods?

Is it the same with Http request or can i make the http request directly from the trigger?

Solution:

Currently in Salesforce we cannot make callout from trigger directly , You need to write a future class that will invoke the callout using wsdl generated classes. So in fact this call will be asynchronous call rather than realtime callout.

For example-  you have a wsdl generated class named "WSDlGeneratedClass" , And you will have to write a future method like -

public class future class

{

 @future(callout=true)

 public static void calloutMethod()

 {

  // write code to invoke "WSDlGeneratedClass" class methods to make callout

 }

}

Now this method you will need to call from trigger.

Yes its also with HTTP callout  (cannot make any kind of callout from trigger directly).



how to remove a value from list without using its index?

"list<string>options=new list<string>{'a','b','c','d','e','f','g','h'};"

from this list i want to remove "e" (lets assume that 'i have dont know its index')

Solution:

* Just add all list values to a SET.

* We can directly remove values from set using remove() method.

* After that add this set to a particular list.



How can we read and save body and attachments in Salesforce of email?

Trigger:

trigger sendEmail on Employee__c (after insert, after update)

{   

    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    for(Employee__c e : trigger.new)

    {  

        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

        attach.setContentType('application/pdf');

        attach.setFileName('Employee.pdf');

       

        String body;

         body = '<html><h1 style=\"text-align:center;\">Employee Information</h1><br/><br/><table align=\"center\"><tr><td>Employee Name</td><td>' + e.Name + '</td></tr><tr><td>Age</td><td>' + e.Age__c + '</td></tr><tr><td>State</td><td>' + e.State__c + '</td></tr><tr><td>City</td><td>' + e.City__c + '</td></tr></table></html>';

        System.debug('HTML is ' + body);

       

        attach.Body = Blob.toPDF(body);

               

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        mail.setToAddresses(new String[] { e.Email__c });

        mail.setSubject('PDF Generation');

        mail.setHtmlBody('PFA');

        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });    

       

        mails.add(mail); 

    }

    if(!mails.isEmpty())

    {

        Messaging.SendEmail(mails);

    }

}

How to bulkify trigger

The term bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiate Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch.

Here is an example of poorly written code that only handles one record:

    trigger accountTestTrggr on Account (before insert, before update)

   {

   

       //This only handles the first record in the Trigger.new collection

       //But if more than 1 Account initiated this trigger, those additional records

       //will not be processed

       Account acct = Trigger.new[0];

       List<Contact> contacts = [select id, salutation, firstname, lastname, email

                  from Contact where accountId = :acct.Id];       

    }

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Here is a sample of how to handle all incoming records:

    trigger accountTestTrggr on Account (before insert, before update)

   {    

       List<String> accountNames = new List<String>{};

     

       //Loop through all records in the Trigger.new collection

       for(Account a: Trigger.new){

          //Concatenate the Name and billingState into the Description field

          a.Description = a.Name + ':' + a.BillingState

       }       

    }

Notice how this revised version of the code iterates across the entire Trigger.new collection with a for loop. Now if this trigger is invoked with a single Account or up to 200 Accounts, all records will be properly processed.



             The Situation is I do have two Check boxes (Checkbox A & Checkbox B).And My condition is to allow any one of the check box is true ,not the two at a time.



If checkbox A is true , Check box B should be False  and IF Checkbox B is true , Check box  B should be False , Both should not be TRUE.



use the Validation rule to do this and display the error if both are checked

In Error Condition Formula

AND(checkboxA,checkboxB)

In ERROR MESSAGE:select only once checkbox



How will you handle 10001 SOQL problems in apex?

10001 error means “Too many query rows”

Public static  integer getTotalVotes()

   {

        integer totalIdea = [SELECT count() FROM Vote where Parent.Type='Idea' limit 501];

        return totalIdea ;

   }

Post a Comment

0 Comments