Thursday, April 6, 2023

SALESFORCE DEVELOPMENT SCENARIO BASED QUESTIONS AND ANSWERS

 1. Whenever a record is inserted to the account automatically inserted to the contact?Ans:

trigger SCENARIO1 on Account (after insert){
    list<contact> c=new list<contact>();
    for(account a:trigger.new)
    {
        contact b=new contact();
        b.LastName=a.Name;
        b.AccountId=a.Id;
        c.add(b);
    }
    insert c;
}

2. Whenever a record is inserted to the contact automatically inserted to the account?Ans:

trigger scenario2 on Contact (after insert){
    if(Recursive.flag)
    {
        Recursive.flag=false;
        list<account>a=new list<account>();
        for(contact c:trigger.new)
        {
            account a1=new account();
            a1.Phone=c.Phone;
            a1.Name=c.LastName;
            a.add(a1);
        }
        insert a;
    }
}

Recursive Trigger fire:

Public class Recursive {
    Public static boolean flag=true;
}

3. Whenever a create opportunity object records updated total opportunities and total amount in account object?
Ans:

trigger scenario3 on Opportunity (after insert) {
    set<id>ids=new set<id>();
    for(Opportunity op:trigger.new)
    {
        ids.add(op.accountid);
    }
    list<account>ac=[select Total_opportunities__c,Total_Amount__c,(select id,Amount 
                     from Opportunities ) from account where id=:ids];
    for(account a:ac)
    {
        a.Total_opportunities__c=a.opportunities.size();
        decimal sum=0;
        for(opportunity p:a.opportunities)
        {
            sum=sum+p.amount;
        }
        a.Total_Amount__c=sum;
    }
    update ac;
}

4. Contact object whenever department equal to ‘CSE’ automatically before inserted email field?
Ans:

trigger scenario4 on Contact (before insert){
    for(contact c:trigger.new)
    {
        if(c.Department=='CSE')
        {
            c.Email='abc@gmail.com';
        }
    }
}

5. Whenever we modify input out object doctor name automatically update dropoff object text field no relationship?
Ans:

trigger SCENARIO5 on Inputout__c (after update){
    list<Dropoff1__c>d=[select id,name,Text__c from Dropoff1__c where Text__c='TusharSFDC'];
    string name;
    for(Inputout__c c:trigger.new)
    {
        name=c.Doctor_Name__c;
    }
    for(Dropoff1__c dp:d)
    {
        dp.Text__c=name;
    }
    update d;
}

6. Limit reached the records?
Ans:

trigger SCENARIO6 on Account (before insert,before update){
    integer count=0;
    list<account>a=[select id,name from account where createddate=today or lastmodifieddate=today];
    for(account ac:trigger.new)
    {
        count=a.size();
        ac.NumberofLocations__c=count;
        if(count>2)
        {
            ac.adderror('reached limit today');
        }
    }
}

7. Can not insert/update/delete that user account object records?
Ans:

trigger scenario7 on Account (before insert,before update,before delete) {
    user u=[select id,name from user where username='abc@gmail.com'];
    if(u.id==userinfo.getUserId())
    {
        if(trigger.isdelete)
        {
            for(account a:trigger.old)
            {
                a.adderror('cant delete record');
            }
        }
        if(trigger.isupdate)
        {
            for(account b:trigger.new)
            {
                b.adderror('can not update');
            }
        }
        if(trigger.isinsert)
        {
            for(account c:trigger.new)
            {
                c.adderror('can not insert');
            }
        }
    }
}

8. Already existing records display an error message?
Ans:

trigger scenario8 on Contact (before insert) {
    list<string>st=new list<string>();
    for(contact c:trigger.new)
    {
        list<contact>a=[select id,name,Email,lastname from contact where Email=:c.Email];
        if(a.size()>0)
        {
            c.Email.adderror('already existing');
        }
    }
}

OR For loop without query:

trigger duplicatetrigger on Inputout__c (before insert) {
    set<string>s=new set<string>();
    for(Inputout__c op:trigger.new)
    {
        s.add(op.Doctor_Name__c);
    }
    list<Inputout__c>d=[select id,Doctor_Name__c from Inputout__c where Doctor_Name__c=:s];
    set<string>dupids=new set<string>();
    for(Inputout__c don:d)
    {
        dupids.add(don.Doctor_Name__c);
    }
    for(Inputout__c c:trigger.new)
    {
        if(c.Doctor_Name__c!=null)
        {
            if(dupids.contains(c.Doctor_Name__c))
            {
                c.Doctor_Name__c.adderror('already existing record');
            }
        }
    }
}

9. Count of related contacts and accounts field display size?
Ans:Apex:

Public class RollupSummery {
    public static void increment(list<contact>con)
    {
        set<id>ids=new set<id>();
        for(contact c:con)
        {
            ids.add(c.accountid);
        }
        list<account>a=[select id,name,NumberOfEmployees,(select id,lastname from contacts)
                       from account where id=:ids];
        for(account ac:a)
        {
            ac.NumberOfEmployees=ac.contacts.size();
        }
        update a;
    }
}

Tigger:

trigger scenario9 on Contact (after insert) {
    rollupsummery.increment(trigger.new);
}

10. Whenever opportunity StageName =’Close Down’ automatically update the account field Rating=’hot’?
Ans:

trigger scenario10 on Opportunity (after insert,after update) {
    set<id>ids=new set<id>();
    list<account>ac=new list<account>();
    for(opportunity op:trigger.new)
    {
        ids.add(op.AccountId);
        ac=[select id,name,rating from account where id=:ids];
        if(op.StageName=='Closed won')
        {
            for(account a:ac)
            {
                a.Rating='hot';
            }
            update ac;
        }
    }
}

11. Whenever the account name is ‘TusharSDFC’ automatically update the contact all last names?Ans:

trigger scenario11 on Account (after update) {
    string names;
    list<contact>c=[select id,lastname,firstname from contact where lastname=:names ];
    for(account a:trigger.new)
    {
        names=a.name;
    }
    for(contact con:c)
    {
        con.lastname=names;
    }
    update c;
}

12.when ever an Opportunity created record amount field is calculated by account total field?Ans:

trigger scenario12 on Opportunity (after insert,after update,after delete) {
    set<id>ids=new set<id>();
    map<id,opportunity>opp=new map<id,opportunity>();
    Decimal oldVal;
    Decimal newVal;
    if(trigger.isinsert)
    {
        for(opportunity op:trigger.new)
        {
            ids.add(op.AccountId);
            opp.put(op.AccountId, op);
        }
        list<account> acc=[select id,Total_Amount__c from account where id=:ids];
        for(account a:acc)
        {
            if(a.Total_Amount__c==null )
            {
                a.Total_Amount__c=opp.get(a.Id).amount;
            }
            else
            {
                a.Total_Amount__c= a.Total_Amount__c+opp.get(a.Id).amount;
            }
        }
        update acc;
    }
    if(trigger.isUpdate)
    {
        for(opportunity op:trigger.new)
        {
            ids.add(op.AccountId);
            opp.put(op.AccountId, op);
            newVal=op.Amount;
        }
        for(Opportunity ops:trigger.old){
            oldVal=ops.Amount;
        }
        list<account> acc=[select id,Total_Amount__c from account where id=:ids];
        for(account a:acc)
        {
            if(a.Total_Amount__c==null )
            {
                a.Total_Amount__c=opp.get(a.Id).amount;
            }
            else
            {
                a.Total_Amount__c= a.Total_Amount__c+opp.get(a.Id).amount-oldVal;
            }
        }
        update acc;
    }
  }
}

13.when ever a create a lead object automatically converted Account, Contact, Opportunity?Ans:

trigger scenario13 on Lead (after insert) {
    list<account>acc=new list<account>();
    list<contact>con=new list<contact>();
    list<opportunity>op=new list<opportunity>();
    for(lead l:trigger.new)
    {
        account a=new account();
        a.Name=l.lastname;
        a.Phone=l.Phone;
        acc.add(a);
        contact c=new contact();
        c.LastName=l.Name;
        con.add(c);
        opportunity o=new opportunity();
        o.Amount=l.AnnualRevenue;
        o.CloseDate=system.today();
        o.StageName='closed won';
        op.add(o);
    }
    insert acc;
    insert con;
    insert op;
}

14. Whenever create a contact automatically update Opportunity fields?Ans:

trigger scenario14 on Contact (after insert) {
    list<opportunity>op=[select id,name,stagename,Description,amount from opportunity limit 50];
    for(contact c:trigger.new){
        for(opportunity o:op)
        {
            if(o.amount<5000||o.Amount==null)
            {
                o.amount=5000;
                o.Name=o.Name+'Mr';
                o.StageName='prospecting';
            }
            else{
                o.Amount=o.Amount+1000;
                o.Name=o.Name+'Dr';
            }
            update o;
        }
    }
}

15. Sending email outbound email message?Ans:

public class emailprogramme1 {
    public void myemails()
    {
        messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
        string[] toadd=new string[]{'abc@gmail.com'};
            string[] tocc=new string[]{'abcsfdc@gmail.com'};
                m1.setToAddresses(toadd);
        m1.setCcAddresses(tocc);
        m1.setSubject('accenture');
        m1.setPlainTextBody('this is interview call letter');
        messaging.email[] m2=new messaging.Email[]{m1};
            messaging.sendEmail(m2);
    }
}
//========execution:==========(CTRL+E)
emailprogramme1 v=new emailprogramme1(); v.myemails();

16. Outbound message pdf file?Ans:

public class emailprogramme2 {
    public void emailbody()
    {
        messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
        string[] toadd=new string[]{'abc@gmail.com'};
            m1.setToAddresses(toadd);
        m1.setSubject('Pdf file');
        m1.setPlainTextBody('THIS IS BILL OF TELEPHONE');
        messaging.EmailFileAttachment m2=new messaging.EmailFileAttachment();
        pagereference p=page.page1;
        blob body=p.getContentAsPDF();
        m2.setBody(body);
        m2.setFileName('jan-feb-march');
        messaging.EmailFileAttachment[] eft1=new messaging.EmailFileAttachment[]{m2};
            m1.setFileAttachments(eft1);
        messaging.Email[] m3=new messaging.Email[]{m1};
            messaging.sendEmail(m3);
    }
}
//========execution:==========(CTRL+E)
emailprogramme2 v=new emailprogramme2();
v.emailbody();

17. Sending an email template to the outbound message?Ans:

public class emailprogramme3 {
    public void emailmethod()
    {
        messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
        emailtemplate et=[select id from emailtemplate where name='doctor' ];
        m1.setTemplateId(et.Id);
        contact c=[select id,lastname,phone from contact where phone='999'];
        m1.setTargetObjectId(c.id);
        Inputout__c D=[select id,Doctor_Name__c from Inputout__c limit 1];
        m1.setWhatId(D.Id);
        messaging.Email[] m2=new messaging.Email[]{m1};
            messaging.sendEmail(m2);
    }
}
//=======Execution:======(CTRL+E)
emailprogramme3 v=new emailprogramme3();
v.emailmethod();

18. Sending an email trigger?Ans:

trigger sendingmailtrigger on Inputout__c (before insert) {
    for(Inputout__c i:trigger.new)
    {
        if(i.Check_box__c==true)
        {
            messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
            string[] toadd =new string[]{'abc@gmail.com'};
                m1.setToAddresses(toadd);
            m1.setSubject('accenture');
            m1.setPlainTextBody('this is interview call letter');
            messaging.Email[] mail1=new messaging.Email[]{m1};
                messaging.sendEmail(mail1);
        }
    }
}

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...