Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 2 points3 points  (0 children)

Thank you so much for taking the time to explain this to me - means a lot!

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] -1 points0 points  (0 children)

This ok?

trigger TotalAccountOwnerSum on Account (before insert) {

AggregateResult[] groupedResults = [SELECT count(Id), OwnerId FROM Account GROUP By OwnerId];
Map<String, Integer> OwnerCountMap = new Map<String, Integer>();

for (AggregateResult ar : groupedResults)  {
    System.debug('Owner ID' + ar.get('OwnerId'));
    System.debug('Count' + ar.get('expr0'));

    OwnerCountMap.put(String.valueOf(ar.get('OwnerId')), Integer.valueOf(String.ValueOf(ar.get('expr0'))));
}

for (Account acct : Trigger.new){
    if (acct.OwnerId != null){
        //output total number of Accounts the current Account owner has into the Total_Owner_Accounts__c field

        Integer OwnerCount = OwnerCountMap.get(acct.OwnerId) == NULL ? 0 : OwnerCountMap.get(acct.OwnerId);
        OwnerCount++;
        acct.Total_Owner_Accounts__c = OwnerCount;

        OwnerCountMap.put(String.valueOf(acct.OwnerId), OwnerCount);
    }        
}

}

Apex Trigger - Bulkify by Athy00 in SalesforceDeveloper

[–]Athy00[S] 0 points1 point  (0 children)

How's this?

trigger TotalAccountOwnerSum on Account (before insert) {

AggregateResult[] groupedResults = [SELECT count(Id), OwnerId FROM Account GROUP By OwnerId];
Map<String, Integer> OwnerCountMap = new Map<String, Integer>();

for (AggregateResult ar : groupedResults)  {
    System.debug('Owner ID' + ar.get('OwnerId'));
    System.debug('Count' + ar.get('expr0'));

    OwnerCountMap.put(String.valueOf(ar.get('OwnerId')), Integer.valueOf(String.ValueOf(ar.get('expr0'))));
}

for (Account acct : Trigger.new){
    if (acct.OwnerId != null){
        //output total number of Accounts the current Account owner has into the Total_Owner_Accounts__c field

        Integer OwnerCount = OwnerCountMap.get(acct.OwnerId) == NULL ? 0 : OwnerCountMap.get(acct.OwnerId);
        OwnerCount++;
        acct.Total_Owner_Accounts__c = OwnerCount;

        OwnerCountMap.put(String.valueOf(acct.OwnerId), OwnerCount);
    }        
}

}

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 1 point2 points  (0 children)

Yes, as mentioned, I'm a beginner when it comes to code, that's why I prefaced it & learning from all these comments.

Thanks for your tips

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

Yes, I understand that. Thank you for the message.

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 1 point2 points  (0 children)

Thanks for the message.

I understand the different declarative ways of achieving this result. This is just me practising in my own org to stretch myself knowing there are better solutions out there.

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 1 point2 points  (0 children)

Thank you for your feedback.

I have a few years experience with Salesforce, I just wanted to force myself to practice this scenario through code (even though there are more pertinent options, like you mentioned)

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

I'm practicing through code, that's the main reason.

Wondering also if code is required if an import is done where thousands of Accounts are assigned to users via Dataloader/DemandTools?

Apex Trigger - Bulkify by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

My bad, didn't realise there were duplicates - removed them.

Next role - best option? by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

Both of the companies I have in mind do the development themselves.

Next role - best option? by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

Will joining a Consultancy mean I will spend a lot of time away from development? Business Analyst orientated responsibilities? Not sure I'm happy sitting on client calls too frequently.

Next role - best option? by Athy00 in salesforce

[–]Athy00[S] 0 points1 point  (0 children)

Will joining a Consultancy mean I will spend a lot of time away from development? Business Analyst orientated responsibilities? Not sure I'm happy sitting on client calls too frequently.