function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ManjusrinuManjusrinu 

creating and updating Records

Hi ,
I have custom object called Campaignaccount__c ,with lokkup related to account called(Related account) ,when i creating new Record ,if account doesnt have oppournities with the related Campaigaccount record it should create a new oppournity record with the camapignaccount Record name , I nedd to perform this with bulk insertions .
I have the following code for only one Record and its working fine .
Can anyone help me how to perform this for bulk records  ?



Helper class : 
public class CampaignaccountTriggerhelper {
    string accountName ;
    public void insertUpdateRecords (List<Campaignaccount__c> campaignRecords ) {
        set<Id> camapignAccounts = new set<Id>();
        for(Campaignaccount__c cmp  : campaignRecords) {
            if(cmp.RelatedAccount__c != null) {
                accountName = cmp.Name;                
                camapignAccounts.add(cmp.RelatedAccount__c);
            } 
        }
        List<Opportunity> oppRecords = new List<Opportunity>([select Name,    Campaigncount__c,
                                                              AccountId from Opportunity
                                                              where AccountId in :camapignAccounts AND 
                                                              Name =: accountName  ] );
        if(oppRecords.size() >0 ) {
            for(Opportunity opp : oppRecords ) {
                 opp.Campaigncount__c = 0;
                opp.Campaigncount__c = opp.Campaigncount__c + 1; 
            }
update oppRecords ;
        } 
        if(oppRecords.size() == 0) {
            Opportunity a = new Opportunity(
                Name=accountName ,
                Campaigncount__c = 0,
                CloseDate = system.today() ,
                StageName = 'Closed Lost');
                 insert a;
        }   
    }  
}


Trigger :
Trigger CampaignaccountTrigger on Campaignaccount__c (after insert) {
CampaignaccountTriggerhelper myCamp = new CampaignaccountTriggerhelper();
    myCamp.insertUpdateRecords (Trigger.New); 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Manjusrinu,

Can you elaborate the usecase with an example to check further and respond back?

Thanks.
ManjusrinuManjusrinu
Hi, The task is that i have a custom object called Campaignaccount__c with lookup field related to account . When i create a new campaign account record (with some account name ) , it should check if that account is having any opportunity with the same name(campainAccount name ) , if exists i should update the campaign count on opportunity with value 1 . If doesn't exists i should create a new opportunity with the Campaign Account(Name) . I can do this by passing statically ,but for bulkifying it is not working .. Can you suggest me how to workout ?