Trigger – ABSYZ https://absyz.com Salesforce Gold Consulting and Implementation Partner Fri, 27 Nov 2020 10:41:24 +0000 en-US hourly 1 https://absyz.com/wp-content/uploads/2020/06/cropped-favicon-1-1-32x32.png Trigger – ABSYZ https://absyz.com 32 32 Send ICS calendar invitations for Outlook meetings https://absyz.com/send-ics-calendar-invitations-for-outlook-meetings/ https://absyz.com/send-ics-calendar-invitations-for-outlook-meetings/#comments Wed, 18 Dec 2019 10:13:40 +0000 http://blogs.absyz.com/?p=10675

Here we are going to send the meeting invites from salesforce using ICS calendar to our outlook. In general ICS is the  iCalendar/.ics/.ical file and it is used to store the calendar information. Whenever you export anything a outlook, google, etc., calendar  it will automatically save as .ics file.

New and updated meetings are sent to participants using an email notification from particular person. This requires diligent effort from all participants to update their local calendars manually.  The idea is to automatically generate an ICS file with the meeting details and attach it to the notification email whenever an meeting is scheduled or rescheduled. This would allow participants to see the appointment into their calendars.

Let’s assume we have Meeting object in our Org with the following fields

 

  • Subject  – text field
  • Meeting Leader – email field
  • Objective of meeting – picklist field
  • start – Date/Time field
  • End – Date/Time field

Now we are trying to send the meeting invite after inserting or updating the record in salesforce. For that i was using the trigger whenever the record in inserted or updated.

Check the below code to send the ICS calendar invite..!!

 

[sourcecode language="java"]
 trigger meeting on Meeting__c (after Insert,after Update) {
    list<Meeting__c> meetList= new list<Meeting__c>();
    for(Meeting__c meet:trigger.new){
        meetList.add(meet);
    }
    AP_Meeting.sendInvite(meetList);
 }

[/sourcecode]

Below is the helper class..!!

 

[sourcecode language="java"]
public class AP_Meeting{
    public static void sendInvite(list<Meeting__c> meetingList) {
        list<string> UserEmailList = new list<string> ();
        string title;
        string meetingTitle;

        for(Meeting__c meet:meetingList){
            UserEmailList.add(meet.MeetingLeader__c);
            meetingTitle=meet.name+': '+meet.Subject__c;
            title=meet.Subject__c;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(UserEmailList);
            mail.setPlainTextBody('This to inform that we have meeting related to particular problem');
            mail.setSubject(title);
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.filename = 'meeting.ics';
            attach.ContentType = 'text/calendar; charset=utf-8; method=REQUEST'; //PUBLISH';//
            attach.inline = true;

            /***************************/
            attach.body = invite(UserEmailList, meet.MeetingLeader__c, 'Harish Lingam', meet.Subject__c, meet.start__c, meet.end__c, meet.createdDate, system.now(), meet.ObjectiveOfMeeting__c, meet.LastModifiedDate);
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {
                attach
                    });
            Messaging.SendEmailResult[] er = Messaging.sendEmail(new Messaging.Email[] {
                mail
                    });
        }
    }

    public static Blob invite(List<String> emailsList, String Organiser, string name, string subject, dateTime startDate, dateTime endDate, dateTime createdDate, string description, dateTime lastmodifiedDat) {
        String txtInvite = '';
        string startdateTime;
        string enddateTIme;
        string createdDateTime;
        string lastmodifiedDatTime;

        startdateTime = startDate.formatGMT('yyyyMMdd\'T\'HHmmss\'Z\'');
        enddateTIme = endDate.formatGMT('yyyyMMdd\'T\'HHmmss\'Z\'');
        createdDateTime = createdDate.formatGMT('yyyyMMdd\'T\'hhmmss\'Z\'');
        lastmodifiedDatTime = lastmodifiedDat.formatGMT('yyyyMMdd\'T\'hhmmss\'Z\'');

        txtInvite += 'BEGIN:VCALENDAR\n';
        txtInvite += 'PRODID:-//Microsoft Corporation//Outlook 16.0 MIMEDIR//EN\n';
        txtInvite += 'VERSION:2.0\n';
        txtInvite += 'CALSCALE:GREGORIAN\n';
        txtInvite += 'METHOD:REQUEST\n';
        txtInvite += 'REPLAY:ACCEPTED\n';
        txtInvite += 'BEGIN:VEVENT\n';
        txtInvite += 'ATTENDEE\n';
        txtInvite += 'CN=' + subject + '\n';
        for (String email: emailsList) {
            txtInvite += 'ATTENDEE:' + email + '\n';
        }
        txtInvite += 'X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n';
        txtInvite += 'X-WR-RELCALID:{0000002E-9CDF-9CE8-AD4C-66FC0A5A25F7}\n';
        txtInvite += 'CLASS:PUBLIC\n';
        txtInvite += 'CREATED:' + createdDateTime+'\n';
        txtInvite += 'DTEND:' + enddateTIme+'\n';
        txtInvite += 'DTSTART:' + startdateTime+'\n';
        txtInvite += 'LAST-MODIFIED:' + lastmodifiedDatTime+'\n';
        txtInvite += 'ORGANIZER;CN=' + name + ':mailto:' + Organiser + '\n';
        txtInvite += 'RSVP=TRUE\n';
        txtInvite += 'ROLE=REQ-PARTICIPANT\n';
        txtInvite += 'PARTSTAT=NEEDS-ACTION\n';
        txtInvite += 'CN=' + subject + ':mailto:' + Organiser + '\n';
        txtInvite += 'LOCATION:Skype\n';
        txtInvite += 'PRIORITY:5\n';
        txtInvite += 'SEQUENCE:0\n';
        txtInvite += 'SUMMARY:\n';
        txtInvite += 'STATUS:NEEDS-ACTION\n';
        txtInvite += 'LANGUAGE=en-us:\n';
        txtInvite += 'TRANSP:OPAQUE\n';
        txtInvite += 'UID:4036587160834EA4AE7848CBD028D1D200000000000000000000000000000000\n';
        txtInvite += 'X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><META NAME="Generator" CONTENT="MS Exchange Server version 08.00.0681.000"><TITLE></TITLE></HEAD><BODY><!-- Converted from text/plain format --></BODY></HTML>\n';
        txtInvite += 'X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n';
        txtInvite += 'X-MICROSOFT-CDO-IMPORTANCE:1\n';
        txtInvite += 'BEGIN:VALARM\n';
        txtInvite += 'TRIGGER:-PT15M\n';
        txtInvite += 'ACTION:DISPLAY\n';
        txtInvite += 'STATUS:CONFIRMED\n';
        txtInvite += 'DESCRIPTION:Reminder\n';
        txtInvite += 'END:VALARM\n';
        txtInvite += 'END:VEVENT\n';
        txtInvite += 'END:VCALENDAR';
        return Blob.valueOf(txtInvite);
     }
 }
[/sourcecode]

Now lets create a record in the org.  fill the information needed…!!

Now it will directly create a meeting calendar invite in outlook with the details provided. Make sure the all the details filled whatever it is we have used in the code.

 

m5-e1576651434409.png

 

m2

Now we can check calendar for invite.

Now if you want to reschedule the meeting, for that we need to update the record, by updating Start and End time. Now we have rescheduled the meeting to the next day and now the calendar is updated,

 

m6.png

So now you will receive new email with the updated calendar info. Now check the calendar for the updated information.

 

m4

Finally you can add the email template in what ever format you need it, you can also send to the bunch of the people by adding them to the public groups.

 

]]>
https://absyz.com/send-ics-calendar-invitations-for-outlook-meetings/feed/ 1
Einstein Intent and Einstein Sentiment Analysis on Facebook Posts https://absyz.com/einstein-language-facebook-integration/ https://absyz.com/einstein-language-facebook-integration/#comments Thu, 01 Mar 2018 06:58:09 +0000 https://teamforcesite.wordpress.com/?p=8594

Einstein Language

Einstein Language Is used to build natural language processing into your apps and unlock insights within text. Einstein Language contains two NLP services:

Einstein Intent

—Categorize unstructured text into user-defined labels to better understand what users are trying to accomplish. Leverage the Einstein Intent API to analyze text from emails, chats, or web forms to

  • Determine which products prospects are interested in, and send customer inquiries to the appropriate sales person.
  • Route service cases to the correct agents or departments, or provide self-service options.
  • Understand customer posts to provide personalized self-service in your communities.
Einstein Sentiment

—Classify the sentiment of text into positive, negative, and neutral classes to understand the feeling behind text. You can use the Einstein Sentiment API to analyze emails, social media, and text from chat to:

  • Identify the sentiment of a prospect’s emails to trend a lead or opportunity up or down.
  • Provide proactive service by helping dissatisfied customers first or extending promotional offers to satisfied customers.
  • Monitor the perception of your brand across social media channels, identify brand evangelists, and monitor customer satisfaction.

 

Lets suppose I have a Facebook Page for an E-Commerce site. If any user sharing their feedback through posts or comments on the page, We can retrieve the post and comments to Salesforce and find Intent of the post and the Sentiment of the comments.

This can be achieved by following these steps:

step 1. Create a Facebook page.

step 2. Create a Facebook app using Facebook Developers account.

To Connect with Facebook we Should use Graph API, To use Graph API we need to Create a Facebook Developers account using Facebook credentials,

To create a Facebook app

  • login to https://developers.facebook.com/
  • click on My Apps -> Add a New App.

Enter basic information and make the App Live, If the app is not Live we can’t use it for Communicating with Facebook.

Now click on Tools & Support and go to Graph API Explorer, here we can generate Access token and test the app.

Steps to create Access token:
  • select you app from Application drop down.
  • click on Get Token and select Get User Access Token.
  • select the permissions required and click Get Access Token.
  • To get Page Access token click on Get Token and select you Page from Page Access Tokens.
  • Now you can see your page name on the drop down list, click on that and select Request publish_pages.

 

step 3. Create an Object on Salesforce to save posts and their comments.

Create an object with fields to store posts, comments, Sentiment, Intent.

step 4. Create remote site setting on Salesforce.

Go to Remote Site Settings -> New Remote Site

enter site name, remote site URL as https://graph.facebook.com , check Active checkbox and Save.

step 5. Make a callout to Facebook to retrieve posts and comments.

In Einstein_Facebook class we are getting post and comments from Facebook, and extracting posts and comments from returned JSON and creating records.

 

[sourcecode language=”java”]
public class Einstein_Facebook {

public static void sendRequest(){

//getting accesstoken from custom lable.
String accessToken = Label.Facebook_Access_Token;
HttpRequest request = new HttpRequest();
request .setEndpoint(‘https://graph.facebook.com/v2.12/391648194596048?fields=posts{message,comments,type}&access_token=’+accessToken);
request .setMethod(‘GET’);
//Making request
Http http = new Http();
HTTPResponse response= http.send(request );
//getting post and comments in response.
String data=response.getBody();

//getting post and comments from JSON rceived in response.
List line=data.split(‘,’);
List posts=new List();
List postAndComments=new List();
for(String l:line){
posts.add(l.substringBetween(‘{“message”:”‘,'”‘));
string substring=l.substringBetween(‘”message”:”‘,'”‘);
if(substring!=null)
postAndComments.add(substring);
}
//creating a Map of posts and comments.
Map PostComments =new Map();
for(String p:posts){
if(p!=null){
List empty=new List();
PostComments.put(p,empty);
}
}
String key;
Listcomments=new List();
for(string messages : postAndComments){

//checking if the message exists in posts and adding as key.
if(messages!=null){
if(PostComments.containsKey(messages)){
key = messages ;
}

//else adding to comments to corresponding post.
else{
comments=PostComments.get(key);
comments.add(messages);
PostComments.put(key, comments);
}
}
}

//querying all the Posts and comments
List existingPosts = [SELECT id,Comments__c,Posts__c FROM Einstein_Facebook__c];
List listDelete = new List();
for(Einstein_Facebook__c Post:existingPosts){

//checking if post already exists and adding to a list.
if(PostComments.containsKey(Post.Posts__c)){
listDelete.add(Post);
}
}

//deleting the list.
delete listDelete;

List  PostCommentList=new List();
//Iterating through Map to create records.
for(string post : PostComments.keySet()){
for(string comments : PostComments.get(post)){
Einstein_Facebook__c Facebookcomment=new Einstein_Facebook__c();
Facebookcomment.Posts__c=post;
Facebookcomment.Comments__c=comments ;
PostCommentList.add(Facebookcomment);}
}

//Inserting List of  posts and comments.
insert PostCommentList;

}

}
[/sourcecode]

step 6. Write a trigger to get Intent and sentiment of post and comments.

Trigger to fire after insert on Facebook posts and comments:

[sourcecode language=”java”]

trigger EinsteinFB_Trigger on Einstein_Facebook__c (after insert) {    For(Einstein_Facebook__c post : Trigger.New)

{

String  postId = post.Id;

Einstein_Handler.getProbabilityforFB(postId);

}

}

[/sourcecode]

Handler for the trigger:

Here we are using EinsteinVision_Admin EinsteinVision_Sentiment classes to get Intent and Sentiment of posts and comments which we explained in detail in our previous blog on Einstein Intent and Einstein Sentiment.

[sourcecode language=”java”]
global class Einstein_Handler{

@future(callout=true)
Public static void getProbabilityforFB(String body){
Einstein_Facebook__c fd=[select Posts__c, Comments__c from Einstein_Facebook__c where id=:body];

String intentLabel = EinsteinVision_Admin.getPrediction(fd.Posts__c);
String sentimentLabel = EinsteinVision_Sentiment.findSentiment(fd.Comments__c);

fd.Feedback_Type__c=intentLabel;
fd.Sentiment__c=sentimentLabel;
update fd;
}

}
[/sourcecode]

This is a report to show analysis of Facebook post and comments.

facebook report

]]>
https://absyz.com/einstein-language-facebook-integration/feed/ 4
Einstein Intent Analysis Using Einstein Language on Salesforce Chatter https://absyz.com/einstein-intent-analysis-using-einstein-language-on-salesforce-chatter/ https://absyz.com/einstein-intent-analysis-using-einstein-language-on-salesforce-chatter/#comments Tue, 27 Feb 2018 06:05:51 +0000 https://teamforcesite.wordpress.com/?p=8589

Einstein Intent

Einstein Intent helps to categorize unstructured data into labels using its Intent API for a better understanding of what user is trying to accomplish. In Salesforce, Einstein Intent API allows understanding the customer inquiries that is easy to automatically route leads, escalate service and personalize marketing. This is helpful in prioritizing customer service inquiries. For example, Einstein Language comes into Salesforce where Einstein Intent and Einstein Sentimental analysis are in use by including them in chatter. Here in Salesforce Chatter, the post where the Einstein Intent and the comments where Einstein Sentiment can be applied to the real world example of e-commerce that is discussed below.

For a better understanding of Einstein Intent, you can refer the Intent basics trailhead (https://trailhead.salesforce.com/en/modules/einstein_intent_basics). The steps to access Einstein Intent are:

  1. Create Dataset
  2. Train the Dataset
  3. Predict
Create Dataset:

Create a .csv file with a column of sample data and another column as labels as shown below. You can create your own dataset and use the downloadable link for training data. To know more about how to create a downloadable link you can refer our blog. The more data you give the more Einstein is getting trained to be accurate. Here an e-commerce example is used with the labels named: Billing, Password Help, Shipping Info, Order Change and Sales Opportunity.

csvfile

Train the Dataset:

We give the downloadable link in the URL field and train dataset. In detail, refer the blog for training the data in a step by step process to create component where models are created to predict the text. After you follow the steps, go to setup -> apex classes and replace the code because we are calling the Intent API to train the data. You find the classes like this.

class

In the class, EinsteinVision_PredictionService.apex the intent is passed to the API language added to the URL. Here the change the endpoint URL as

[sourcecode language=”java”]
private static String BASE_URL = ‘https://api.einstein.ai/v2’;
private String DATASETS = BASE_URL + ‘/language/datasets’;
private String LABELS = ‘/labels’;
private String EXAMPLES = ‘/examples’;
private String TRAIN = BASE_URL + ‘/language/train’;
private String MODELS = ‘/language/models’;
private String PREDICT = BASE_URL + ‘/language/intent’;
private String API_USAGE = BASE_URL + ‘/apiusage’;
private static String OAUTH2 = BASE_URL + ‘/oauth2/token’;
public String data;
[/sourcecode]

Change the URL value as ‘text-intent’ in all the methods

[sourcecode language=”java”]
EinsteinVision_HttpBodyPartDatasetUrl parts = new EinsteinVision_HttpBodyPartDatasetUrl(url,’text-intent’);
[/sourcecode]

And the parameter to pass is only a string so that change the argument name where ever necessary to pass the model id and data.

[sourcecode language=”java”]
public EinsteinVision_PredictionResult METHOD (String modelId, String data) {
EinsteinVision_HttpBodyPartPrediction parts = new EinsteinVision_HttpBodyPartPrediction(modelId, data);
[/sourcecode]

In the EinsteinVision_HttpBodyPartPrediction.apex class change the parameter to pass.

[sourcecode language=”java”]
public EinsteinVision_HttpBodyPartPrediction(String modelId, String data) {
this.modelId = modelId;
this.data = data;
}
[/sourcecode]

Now the apex class is ready to use for Einstein Intent. The sample data can be downloaded from this link and the same can be specified to train the data (http://einstein.ai/text/case_routing_intent.csv).

intenttrain.PNG

Predict:

Here when I give a text, Einstein Intent will find the probability which type of department it belongs to and categorizes the data. Pass the CSV file name that you have saved it or refer the dataset name after training that is shown in the above image.

[sourcecode language=”java”]
@AuraEnabled
public static String getPrediction(String Comments) {
EinsteinVision_PredictionService service = new EinsteinVision_PredictionService();
EinsteinVision_Dataset[] datasets = service.getDatasets();
String Prob;
for (EinsteinVision_Dataset dataset : datasets) {
if (dataset.Name.equals(‘case_routing_intent.csv’)) {
EinsteinVision_Model[] models = service.getModels(dataset);
EinsteinVision_Model model = models.get(0);
EinsteinVision_PredictionResult result = service.predictBlob(model.modelId, Comments);
EinsteinVision_Probability probability = result.probabilities.get(0);
Prob = result.probabilities.get(0).label;
}
}
return Prob;
}
[/sourcecode]

The component to get the data from the user and display the probability.

[sourcecode language=”html”]
<aura:component controller=”EinsteinVision_Admin” implements=”force:appHostable, flexipage:availableForAllPageTypes, flexipage:availableForRecordHome, force:hasRecordId, forceCommunity:availableForAllPageTypes, force:lightningQuickAction” access=”global”>
<aura:attribute name=”CsvURL” type=”String”/>
<aura:attribute name=”Probablity” type=”Object”/>
<center>
<lightning:card >
<aura:set attribute=”title”>
<p style=”font-family:serif;font-size: 40px;”>User Feedback</p>
</aura:set>
<div style=”width:40%;”>
<ui:inputText class=”textbox” aura:id=”select” placeholder=”Enter your comments”/><br/>
<br/>
<lightning:button onclick=”{!c.extractfile}”>search</lightning:button><br/>
<br/>
</div>

<p style=”font-family:serif;font-size: 20px;”><ui:outputTextArea value=”{!v.Probablity}”/></p>
</lightning:card>
</center>
</aura:component>
[/sourcecode]

Pass the data form user to apex controller using controller.js and return the response.

[sourcecode language=”java”]
({
extractfile : function(component, event, helper) {
var action = component.get(“c.getPrediction”);
var text = component.find(“select”).get(“v.value”);

action.setParams({
Comments: text
});
action.setCallback(this, function(response) {
component.set(“v.waiting”, false);
var state = response.getState();
if (state === ‘ERROR’) {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
return alert(errors[0].message);
}
} else {
return console.log(“Unknown error”);
}
}
var result = response.getReturnValue();
component.set(“v.Probablity”,result);
});
component.set(“v.waiting”, true);
$A.enqueueAction(action);
}
})
[/sourcecode]

The intent of the text that we get is the output shown below.

intent

 

Salesforce Chatter

The real-time example where we can make use of Einstein Language on Salesforce is the Chatter. Here we are posting a message to calculate the  Intent and the comments to the post made by the users to calculate the Sentiment (Positive, Neutral and Negative). When a user posts a message on Chatter and a comment is given to it, a trigger is fired to Intent and Sentiment methods. I have a created a custom object to store the posts and comments in the custom fields. On after insert trigger, we feed the text to the apex methods to get the type and sentiment of the text.

 

Trigger to fire after insert on FeedComment:

[sourcecode language=”java”]
trigger Einstein_ChatterComment on FeedComment (after insert) {
String Body;
For(FeedComment comment : Trigger.New)
{
Body = comment.Id;

}
Einstein_ChatterHandler.getProbability(Body);
}
[/sourcecode]

Handler for the trigger:

[sourcecode language=”java”]
global class Einstein_ChatterHandler {
@future(callout=true)
Public static void getProbability(String body){
FeedComment fd=[select CommentBody, FeedItemId from FeedComment where id=:body];
FeedItem fi = [select Body from FeedItem where id=:fd.FeedItemId];

String intentLabel = EinsteinVision_Admin.getPrediction(fi.Body);
String sentimentLabel = EinsteinVision_Sentiment.findSentiment(fd.CommentBody);

Einstein__c ein = new Einstein__c(Chatter_Message__c=fi.body, Comment__c=fd.CommentBody, Feedback_type__c=intentLabel, Emotion__c=sentimentLabel);
insert ein;
}
}
[/sourcecode]

Finally, we create reports to view the analyzed data as shown below. The report is grouped by sentiment for each department.

report.PNG

Einstein Language can be implemented on social media to know the customer view that will be our upcoming blog. Please feel free to contact us for any doubts and queries.

]]>
https://absyz.com/einstein-intent-analysis-using-einstein-language-on-salesforce-chatter/feed/ 7
Roll-up Summary without a Master Detail Relationship https://absyz.com/roll-up-summary-without-a-master-detail-relationship/ https://absyz.com/roll-up-summary-without-a-master-detail-relationship/#comments Wed, 16 Mar 2016 13:02:34 +0000 https://teamforcesite.wordpress.com/?p=352

Roll-up Summary is a feature of salesforce enabled only after having a master detail relationship between objects. Rollup Summary is a feature where, It gives us the options of having SUM, MIN, MAX of a field in all the child records or COUNT the number or child records. This is stored as a field in the Master side Object of the two objects in a master detail relationship.

As Salesforce.com has limitations on almost everything available we have limits for Rollup Summaries as well!! We can configure a maximum of 25 Rollup Summaries  .The maximum number of master detail relationships on an object can be two.

To cater such rollup requirements we need to have a master detail relationship between objects . But we cannot just go ahead configuring a master detail relationship on a requirement just for the feature ROLLUP. There are several other features of master detail relationship to be kept in mind,

– The relationship field is required on all detail records.
– The ownership and sharing of a detail record are determined by the master record.
– When a user deletes the master record, all detail records are deleted.

Moreover, A standard object cannot be the child/detail in a Master-Detail relationship. An example is that you cannot create a Master-Detail relationship from the Asset object because the Asset cannot be the child in a Master-Detail relationship.

So, I came up with a trigger that can meet the functionalities available in a Rollup without having a Master Detail. All you need is a lookup relationship between the objects.

COUNT :

So let’s say you have a classroom object and a desk object. Where we have to count the number of desks in a classroom . We write a trigger after having a look up between them.

We basically get all the parent record with the count__c field and load them in a map. This works fine for insert, update and delete.

-When a child record is inserted, the count in the map is increased for the specified parent.

-When a child record is updated, three scenarios can happen:

  1. The lookup field is changed from one parent to another
  2. There is no value in the lookup field and is updated to a value
  3.  The lookup value is nullified basically the parent is removed.

To meet all scenarios a code is written that works accordingly.

-When a child is deleted, the count is reduced in the map for the respective parent.

In the end the values in the map are updated to the database.

 

——————————————————————

[sourcecode language=”java”]

trigger deskMaster on Desk__c (after delete,after update, after insert, after undelete) {
list&amp;lt;classroom__c&amp;gt; ct = new list&amp;lt;classroom__c&amp;gt;();
set&amp;lt;id&amp;gt; classroomIDs = new set&amp;lt;id&amp;gt;();
if(trigger.isInsert){
for(Desk__c deskItem : Trigger.new){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isDelete){
for(Desk__c deskItem : Trigger.old){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isUnDelete){
for(Desk__c deskItem : Trigger.new){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isUpdate){
for(Desk__c deskitem : trigger.new){
if(trigger.oldmap.get(deskitem.id).clasroom__c!=deskitem.classroom__c){
classroomIDs.add(deskItem.Classroom__c);
classroomIDs.add(trigger.oldmap.get(deskitem.id).classroom__c);
}
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), classroom__c FROM desk__c where classroom__C IN :classroomIDs GROUP BY classroom__c ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get(‘classroom__c’);
Integer count = (INTEGER)ar.get(‘expr0’);
classroom__c cust1 = new classroom__c(Id=custid);
cust1.count__c = count;
ct.add(cust1);
}
update ct;
}
[/sourcecode]

——————————————————————

This can be edited according for the SUM, MIN, MAX functionalities. The structure will remain the same, you just have to change the logic inside.

Happy Coding!!

]]>
https://absyz.com/roll-up-summary-without-a-master-detail-relationship/feed/ 7