• Prangya Jena
  • NEWBIE
  • 30 Points
  • Member since 2019

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 9
    Replies
I am trying to write a trigger and helper class for an Account that has the Type as "Prospect".  If the Account is updated and has no related Opportunities, two new Opportunities are added to the Account.  I have not had any luck getting the code to work.

Here is my Trigger:

trigger ResComOpportunityTrigger on Account (after update) {
    if(Trigger.isAfter && Trigger.isUpdate){
        ResComOpportunityHelper.UpdateOpps(Trigger.new);
    }
}

Here is my helper class:

public with sharing class ResComOpportunityHelper {

    static public void UpdateOpps(List<Account> acctList) {

        List<Opportunity> updateOppsList = new List<Opportunity>();

        acctList = [
                SELECT Id, Name, OwnerId, (SELECT Name, Id FROM Opportunities)
                FROM Account
                WHERE Type = 'Prospect'
        ];

        for (Account accUp : acctList) {
            for (Opportunity opp : accUp.Opportunities) {
                if (opp.Name == null) {
                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = accUp.Id;
                    opp1.CloseDate = System.today().addMonths(3);
                    opp1.StageName = 'Prospecting';
                    updateOppsList.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = accUp.Id;
                    opp2.CloseDate = System.today().addMonths(3);
                    opp2.StageName = 'Prospecting';
                    updateOppsList.add(opp2);
                }
            }
        }
        insert updateOppsList;
    }
}

Any assistance would be appreciated!
What is the concept of Intranet and Internet in Salesforce?
Please explain.

Thanking in advance.
Hi All,

Do we have alias for Field names in SOQL as we have in SQL. 
I know we have alias for aggregate functions , but i have a need where i want alias for field names as two of my field API names are same.

Thanks in advance
Hi,

I have a requirement where I have to change the svg icon for the Lightning component. The icon is a customized icon.

Please check the image. Any help is appriciated.

User-added image
Can we give profile level access to a perticular report or report folder? So that those profile users will only able to edit those reports inside that folder. They shouldn't edit any other only view access should be there.
Hi All,
Do we have any tool for migration which will give me the option of choosing 'the orgs for migration' and 'date fields which will restrict my datas to be within that range' in UI. Means, Only data or metadata within those dates should be available for migration. Is salesforce provides any such tool for migration or any AppExchange products are there?

Please suggest. Thanks in advance.
I'm trying to update a field on ts2__Placement__c records that meet the criteria. I believe it's because I am doing a DML statement within a for loop. How do I avoid this and get the same results?
 
public class plc2WeekAudit implements Database.Batchable<sObject>, Database.Stateful {
    
    List<Change_Request__c> plcList = [SELECT Related_Placement__c FROM Change_Request__c WHERE End_Date__c != null AND Related_Placement__r.ts2__Status__c ='Active' AND Related_Placement__r.ts2__End_Date__c < LAST_N_MONTHS:1];
    Set<String> duplicatePlc = new Set<String>();

    public Database.QueryLocator start(Database.BatchableContext bc) {
        for(Change_Request__c item : plcList){
                duplicatePlc.add(item.Related_Placement__c);
        }
        System.debug('the size is : ' +duplicatePlc.size());
        System.debug(duplicatePlc);

        return Database.getQueryLocator('SELECT Id,ts2__Status__c FROM ts2__Placement__c WHERE Id IN :duplicatePlc');    
    }
    public void execute(Database.BatchableContext BC, List<ts2__Placement__c> returnedPlcs){
        System.debug(returnedPlcs);
        for(ts2__Placement__c plc : returnedPlcs){
            plc.ts2__Status__c = 'Inactive';
        }
        update returnedPlcs;
    }
    public void finish(Database.BatchableContext BC){
    }
}

 
Hi All,

Do we have alias for Field names in SOQL as we have in SQL. 
I know we have alias for aggregate functions , but i have a need where i want alias for field names as two of my field API names are same.

Thanks in advance
How can we get the names of fields when any record in inserted in any object using apex. I need field API name not value.
Suppose I had created a record in Account and inserted name. Then I need to know from apex that what is the field API name for the record I inserted.
Help me to figure out why when I try to update the Account without Opps it doesn't create two new Opps as per below logic.
Here is my helper class
public with sharing class AccountWithTwoDefaultOpp {
    public static void addTwoDefaultOpptToAcct(List<Account> acctList) {

        List<Opportunity> oppList = new List<Opportunity>();

        for (Account acct : acctList) {

            Opportunity opp = new Opportunity();
            opp.Name = acct.Name + ' Default Residential Opp';
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.today().addMonths(1);
            opp.AccountId = acct.Id;
            oppList.add(opp);

            Opportunity opp2 = new Opportunity();
            opp2.Name = acct.Name + 'Default Commercial Opp';
            opp2.StageName = 'Prospecting';
            opp2.CloseDate = System.today().addMonths(1);
            opp2.AccountId = acct.Id;
            oppList.add(opp2);
        }
        insert oppList;
    }
    public static void updateAccount(List<Account> acctList2) {

        List<Opportunity> oppList2 = new List<Opportunity>();

        Map<Id, Account> acctsTypeProspect = new Map<Id, Account>(
        [
                SELECT Id, Name, Type
                FROM Account
                WHERE Type = 'Prospect'
                AND Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN :acctList2
        ]);

        for (Account account : acctList2) {
            if(account.Type == 'Prospect'){
        }
            for (Opportunity opp : oppList2) {

                if (acctsTypeProspect.get(account.Id).Opportunities.isEmpty()) {

                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = account.Id;
                    opp1.CloseDate = System.today().addMonths(1);
                    opp1.StageName = 'Prospecting';
                    oppList2.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = account.Id;
                    opp2.CloseDate = System.today().addMonths(1);
                    opp2.StageName = 'Prospecting';
                    oppList2.add(opp2);
                }

            }
            if(oppList2.size()>0){
                insert oppList2;

            }
        }

    }
}
And Trigger 
trigger AddTwoOppToNewAccount on Account (after insert, after update) {
    if (Trigger.isInsert && Trigger.isAfter){
        AccountWithTwoDefaultOpp.addTwoDefaultOpptToAcct(Trigger.new);
    }

    if  (Trigger.isUpdate) {
        AccountWithTwoDefaultOpp.updateAccount(Trigger.new);
    }
}

 
I am trying to write a trigger and helper class for an Account that has the Type as "Prospect".  If the Account is updated and has no related Opportunities, two new Opportunities are added to the Account.  I have not had any luck getting the code to work.

Here is my Trigger:

trigger ResComOpportunityTrigger on Account (after update) {
    if(Trigger.isAfter && Trigger.isUpdate){
        ResComOpportunityHelper.UpdateOpps(Trigger.new);
    }
}

Here is my helper class:

public with sharing class ResComOpportunityHelper {

    static public void UpdateOpps(List<Account> acctList) {

        List<Opportunity> updateOppsList = new List<Opportunity>();

        acctList = [
                SELECT Id, Name, OwnerId, (SELECT Name, Id FROM Opportunities)
                FROM Account
                WHERE Type = 'Prospect'
        ];

        for (Account accUp : acctList) {
            for (Opportunity opp : accUp.Opportunities) {
                if (opp.Name == null) {
                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = accUp.Id;
                    opp1.CloseDate = System.today().addMonths(3);
                    opp1.StageName = 'Prospecting';
                    updateOppsList.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = accUp.Id;
                    opp2.CloseDate = System.today().addMonths(3);
                    opp2.StageName = 'Prospecting';
                    updateOppsList.add(opp2);
                }
            }
        }
        insert updateOppsList;
    }
}

Any assistance would be appreciated!
Can we give profile level access to a perticular report or report folder? So that those profile users will only able to edit those reports inside that folder. They shouldn't edit any other only view access should be there.
How can we upload pdf file to file object using apex?

Below is what I got from other website, but there is no folder name, so how can we identify folder name?
Also, I wanna know how to get parentId.
String jsonStr = '<Base64 String>';
Attachment attach = new Attachment();
attach.contentType = 'application/pdf';
attach.name = 'myfile.pdf';
attach.parentId = '0010H00002DW2Ds';
attach.body = EncodingUtil.base64Decode(jsonStr);
insert attach;

Additionally, below is another code that I got to upload file.
ContentVersion imageData;
List<String> imageContent;
        
imageContent = getImgdata();
imageData = new ContentVersion();
imageData.ContentLocation = 'S';
imageData.PathOnClient = 'sampleData.png';
imageData.Title = imageContent[0];
imageData.VersionData = EncodingUtil.base64Decode(imageContent[1]);
insert imageData;

What is the differencen between first sample and second sample to upload file?
 
Thank you in advance.
 
I have two object
1. Contacts__c
2. Sales_VP_By_Territory__c

On the Contacts__c object, I have two fields
1. Sales VP (Picklist / read only)
2. Territory (Picklist)

On the Sales_VP_By_Territory__c, I have two fields. This object is hidden from users
1. Sales VP
2. Territory

On the Contacts__c object, I would like the user to select the Territory. Once they select one and save the record, a Trigger will fire and store the current record Territory in a variable and then execute a SOQL query on the Sales_VP_By_Territory object and get the Sales_VP__c field value and store it in a variable. Then it will go back and populate the Contact__c records field Sales_VP__c.

Here is my code, but the issue is, it keeps telling me 
"GetSalesVP: data changed by trigger for field Sales VP: bad value for restricted picklist field: (Sales_VP_By_Territory__c:{Sales_VP__c=Brandon Nelson, Id=a0z3a000007e8LSAAY})"

If you notice in my SOQL query, I'm not pulling ID so why is it trying to insert it?
 
trigger GetSalesVP on Contact__c (before insert, before update) {
   	
    String territory;
    String getSalesVP;
    
    for(Contact__c c : Trigger.new) {
        territory = c.Territory__c;
        
	List<Sales_VP_By_Territory__c> getSalesVP = [SELECT Sales_VP__c FROM Sales_VP_By_Territory__c WHERE territory__c = :territory]; 
          
    c.Sales_VP__c = String.valueOf(getSalesVP);
        
    }  
}

 
On lead object, I have a trigger that will create two contacts, now I want to catch those two created Id's and create a record in Relationship__c object. On relationship object I have two fields Contact1__C and contact2__c, I want to pass two newly created contact id's after inserting in trigger to Contact1__c and Contact2__c fields in Relationship Object.

Can anyone help me out in this issue, my code is below.
 
trigger CreateContact on Lead (after insert) {
    
    List<Contact> conInsertList = new List<Contact>();
    List<npe4__Relationship__c> relation = new List<npe4__Relationship__c>();
    List<String> listEmail = new List<String>();
    
    for (Lead em : Trigger.new) {
        if(em.Email != null){
            listEmail.add(em.Email);
        }
    }
    
    List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
    String cemail;
    for(Contact ce : cem){
        cemail = ce.Email;
    }
    
    for(Lead ld : Trigger.new) {
        if (ld.Email != cemail && ld.Parent_Fill__c == false) {
            
            Contact cnt = new Contact();
            cnt.FirstName = ld.FirstName;
            cnt.LastName = ld.LastName;
            cnt.Email = ld.Email;
            conInsertList.add(cnt);
        }
        else{
            if(ld.Email != cemail && ld.Parent_Fill__c == true ){
                Contact cnt1 = new Contact();
                cnt1.FirstName = ld.Parent_First_Name__c;
                cnt1.LastName = ld.Parent_Last_Name__c;
                cnt1.Email = ld.Parent_Email__c;
                conInsertList.add(cnt1);
                Contact cnt2 = new Contact();
                cnt2.FirstName = ld.FirstName;
                cnt2.LastName = ld.LastName;
                cnt2.Email = ld.Email;
                conInsertList.add(cnt2);
            }
        }
    }
    
    if(conInsertList.size()>0){
        INSERT conInsertList;
        List<Id> conInsert = new List<Id>();
        for(Contact con : conInsertList){
            conInsert.add(con.Id);
        }
    }
}