• Goutham
  • NEWBIE
  • 25 Points
  • Member since 2017

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 1
    Questions
  • 8
    Replies
i have to write trigger for,
When Opportunity ‘Close Date’  is populated, should not be less than current date and time. If it is less, show an appropriate error msg.
how to do this?

 
we have to define a map ..., for that key is account id and values are account related contacts
Hi All,

I am new to Salesforce, so please help me with this requirement.
I have a Status update field in support request and we have very frequest to-and-fro comments between us and the clients.
The requirement is to add Current date whenever this field is updated.
Eg :
Updation done by user on 1 Sep 2017 :
"9/1/17 - <<Comments by user>>"

Further updation done by user on 4 Sep 2017 :
"9/1/17 - <<Comments by user>>
9/4/17 - <<Additional comments by user>>"

Please guide.
 
i have to write trigger for,
When Opportunity ‘Close Date’  is populated, should not be less than current date and time. If it is less, show an appropriate error msg.
how to do this?

 
Hello All,
I have written trigger on opportunity, which will display error message if user try to chnage the opportunity stage and for same opportunity there will be Task available with Status not equal to 'Complted'

Trigger
-------------------
trigger ShowErrorMsgOnOpportunity on Opportunity (after update) {
    if(checkRecursive.runOnce()){
        List<Opportunity> countOpenTask = [Select Id, Name, 
                                           (Select Id, Status, WhoId 
                                            From OpenActivities 
                                            Where Status != 'Completed')OppoTask 
                                           From Opportunity 
                                           where Id IN: Trigger.new
                                          ];
        for(Opportunity op : countOpenTask){
            if(op.OpenActivities.size() != 0){
                Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...');
            }   
        }
        update countOpenTask;
    }
}


And Test Class is
-----------------------
 
@isTest
public class ShowErrorMsgOnOpportunity_Test {
    
    @isTest
    private static void WhenThereAreNoTaskRelatedToOpportunity_ShouldThroughError(){
        String errorMsg = 'Please Close Open Activities to update...';
        
        Test.startTest();
        try{
            
            Account acc = new Account(Name = 'Test Account');
            insert acc;
            
            Opportunity opp = new Opportunity();
            opp.Name = 'Test Opportunity';
            opp.StageName = 'Prospecting';
            opp.CloseDate = Date.today();
            opp.AccountId = acc.Id;
            insert opp;
            
            Task tsk = new Task(WhatId = opp.Id, Subject = 'Test Subject', Status = 'In Progress');
            insert tsk;
            
            update opp;
        }
        
        catch(Exception exc){
            System.assertEquals(exc.getMessage(), errorMsg);
        }
        Test.stopTest();
    }
}

I m not able to cover " if(op.OpenActivities.size() != 0){ Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...'); } ". Please let me know where I am getting wrong.